About JavaScript and JavaScript Actions in PDF
JavaScript is a scripting language developed by Netscape Communications. Adobe has enhanced JavaScript so that developers can use it to customize a PDF file easily. For example, modify its appearance, perform calculations in form fields, validate user data and so on.
JavaScript actions are used to execute the JavaScript codes embedded in PDF documents. This article will demonstrate how to add JavaScript actions to PDF in Java using Spire.PDF for Java library.
The following topics will be covered:
- Add Document Level JavaScript Action to PDF in Java
- Add Form Level JavaScript Action to PDF in Java
Add Dependencies
First of all, you need to add needed dependencies for including Spire.PDF for Java into your Java project. If you use maven, you can install the library’s jar from Maven repository by adding the following code to your project’s pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>5.2.3</version>
</dependency>
</dependencies>
For non-maven project, you can download Spire.PDF for Java from this link, extract the package and then import the jar under the lib folder into your project as a dependency.
Add Document Level JavaScript Action to PDF in Java
The document level JavaScript action is applied to an entire document when certain document event occurs. For example, you can add a document level JavaScript action which is performed when a PDF document is opened in a JavaScript-compatible viewer (such as Adobe or Foxit).
The following are the steps to do so:
- Initialize an object of PdfDocument class.
- Add a page using PdfDocument.getPages().add() method.
- Initialize an object of PdfJavaScriptAction class to create a JavaScript action. To the constructor of this class, pass the required JavaScript in the form of String.
- Assign PdfJavaScriptAction object to the document open action using PdfDocument.setAfterOpenAction() method.
- Save the result file using PdfDocument.saveToFile() method.
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.actions.PdfJavaScriptAction;
public class AddJavaScriptToDocument {
public static void main(String[] args){
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Add a page
PdfPageBase page = pdf.getPages().add();
//String javaScript = "app.alert(\"This is a document-level JavaScript example\")";
//Create a JavaScript action
String javaScript = "app.alert( {cMsg: 'This is an PDF document-level example', nIcon: 3, nType: 0, cTitle: 'Javascript Example'} );";
PdfJavaScriptAction javaScriptAction = new PdfJavaScriptAction(javaScript);
//Set the action to be performed when the document is opened
pdf.setAfterOpenAction(javaScriptAction);
//Save the result file
pdf.saveToFile("AddJavaScriptToDocument.pdf");
}
}
Output:
Add Form Level JavaScript Action to PDF in Java
The form level JavaScript is associated with a specific form field or fields, such as a button. This type of script is executed when an event occurs, such as a Mouse Down action.
The following example shows how to add a JavaScript action to a button field:
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.actions.PdfJavaScriptAction;
import com.spire.pdf.fields.PdfBorderStyle;
import com.spire.pdf.fields.PdfButtonField;
import com.spire.pdf.fields.PdfTextBoxField;
import com.spire.pdf.graphics.*;
import java.awt.geom.Rectangle2D;
import java.util.EnumSet;
public class AddJavaScriptToFormField {
public static void main(String[] args) throws Exception {
//Create a new PDFDocument instance
PdfDocument doc = new PdfDocument();
//Add a page
PdfPageBase page = doc.getPages().add();
//Create a PdfFont instance
PdfFont font = new PdfFont(PdfFontFamily.Times_Roman, 12f, EnumSet.of(PdfFontStyle.Regular));
//Create a PdfBrush instance
PdfBrush brush = PdfBrushes.getBlack();
float x = 80;
float y = 100;
float tempX = 0;
//Draw text into page
String text1 = "Name: ";
//Draw text into page
page.getCanvas().drawString(text1, font, brush, x, y);
tempX = (float) font.measureString(text1).getWidth() + x + 15;
//Create a PdfTextBoxField instance
PdfTextBoxField textbox = new PdfTextBoxField(page, "NameBox");
textbox.setBounds(new Rectangle2D.Float(tempX, y, 100, 15));
textbox.setBorderWidth(0.75f);
textbox.setBorderStyle(PdfBorderStyle.Solid);
//Add the textbox field to the field collection
doc.getForm().getFields().add(textbox);
//Create a PdfButtonField instance
PdfButtonField resetButton = new PdfButtonField(page, "resetButton");
resetButton.setBounds(new Rectangle2D.Float(150, 150, 50, 15));
resetButton.setText("Reset");
resetButton.setBackColor(new PdfRGBColor(181, 191, 203));
//Create a PdfJavaScriptAction instance
PdfJavaScriptAction scriptAction = new PdfJavaScriptAction("this.resetForm([\"NameBox\"])");
//Assign the PdfJavaScriptAction instance to the MouseDown action of the button
resetButton.getActions().setMouseDown(scriptAction);
//Add the button to the field collection
doc.getForm().getFields().add(resetButton);
//Save the result file
doc.saveToFile("AddJavaScriptActionToFormField.pdf");
}
}
Output:
Top comments (0)