Adding Javascript actions to existing PDF file

[ ]

The PdfContentEditor class present under com.aspose.pdf.facades package provides the flexibility to add Javascript actions to a PDF file. You can create a link with the serial actions corresponding to execute a menu item in the PDF viewer. This class also provides the feature to create additional actions for document events.

First of all, an object is drawn in the Document, in our example a Rectangle. And set the action createJavaScriptLink to the Rectangle. After you may save your document.

 public static void AddingJavascriptActions() {
        PdfContentEditor editor = new PdfContentEditor();
        // create Javascript link
        java.awt.Rectangle rect = new java.awt.Rectangle(50, 750, 50, 50);
        String code = "app.alert('Welcome to Aspose!');";
        editor.createJavaScriptLink(code, rect, 1, java.awt.Color.GREEN);
        // save the output file"JavaScriptAdded_output.pdf");