1

This is my code to create a pdf from java with itext pdf library which i succeed it. My problem now is that i can not add javascript code either from html when onload called or neither from java code . i dont got any exception. what i am doing wrong?is it possible to be achieved?

public class App {
    public static final String src = "new.pdf";
    public static final String HTML = "test.html";

    public static void main(String[] args) throws IOException, DocumentException {


        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(src));
      //  writer.addJavaScript("<script>alert('asdas')</script>");
        document.open();
        document.setJavaScript_onLoad("yourFunction()");
        XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(HTML));
        document.close();
    }

}

<html>
    <head>
        <style>.col{padding:3px 20px 3px 20px}</style>
        <script type="text/javascript">
          function yourFunction(){
               alert('You clicked the bottom text');
           }
        </script>
    </head>
<body style="font-family:tahoma" onload="yourFunction()">
<div style="background:rgb(230,230,230); padding:5px ;border:1px solid black;">
    <b style="color:rgb(51,153,255)">Sample header</b>
    <img style="float:right" height="25px" width="80px" src="resources/images/itext.png"/>
</div>
<br/>
<table border='0' style='border-collapse: collapse;'>
    <tr>
        <td class="col">String 1</td>
        <td class="col">: 1234354545</td>
    </tr>
    <tr>
        <td class="col">String 32</td>
        <td class="col">: rere</td>
    </tr>
    <tr>
        <td class="col">String 3</td>
        <td class="col">: ureuiu</td>
    </tr>
    <tr>
        <td class="col">Date</td>
        <td class="col">: dfdfjkdjk</td>
    </tr>
</table>
<br/>
<br/>
<br/>
<hr/>
<br/>
Contact us
</body>
</html>

1 Answer 1

3

XMLWorker can't parse Javascript when generating a PDF file. XMLWorker will in fact ignore it, so trying to call it using the setJavaScript_onLoad() won't work, regardless of the function being in the HTML file. I'm not sure what you're trying to achieve here, but you could use Java functionality to evaluate the embedded Javascript. You just won't be able to run the script in the iText context.

If you need Javascript embedded in your PDF, I'd use iText to add it after parsing the HTML. This requires you to have the Javascript separately and adjusted to the provided API. You'll need to check the Javascript API to see what you can do in a PDF file. In general you'll add an "Additional Actions" dictionary to a page, a form, etc. to be able to run Javascript. You can check the PDF specification on how to structure the AA dictionary and which triggers are provided. But again, don't expect to be able to put Javascript for HTML in a PDF and expect it to work out of the box.

You'll also need to be a bit more specific on what you're trying to do in Javascript and when you are trying to do it for us to be a bit more specific in our answers.

Sign up to request clarification or add additional context in comments.

1 Comment

i saw this post and i thought that it was possible with older version javabeat.net/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.