TUTORIALS
Creator/Template Tutorial:
Changing the action behind a button
As easy as 1, 2, 3...
Introduction
Form fields are dynamic elements similar to HTML form fields: textfields, checkboxes and buttons...
Each of these fields can be associated with a list of actions.
Consider, for instance, input action, value changes actions or
mouse-pressed actions; these are common in PDF.
This tutorial will show how jPDF Template can be used to easily change the URL associated with a submit button.
Let's first open the document and get hold of the template module:
// Open the document:
PdfDocument doc = new PdfDocument("docWithASubmitButton.pdf");
// Load the template module:
TemplateModule template = doc.getTemplateModule();
Create the action
In this example we will create a simple URL submit action,
but we could potentially use a Javascript action and write a real
program. jPDF supports most PDF actions. Just check the javadocs to see
how rich the API is.
// Now, let's create the action behind the button:
SubmitFormAction action = SubmitFormAction.createInstance(doc);
// We want the browser to open externally on a URL:
action.setAttribute(SubmitFormAction.ATT_EXCLUDE, true);
action.setAttribute(SubmitFormAction.ATT_HTMLFORMAT, true);
action.setURL(new URL("http://localhost:8080/path/to/myservlet"));
Assigning the action to the button
The last step is straightforward. We just need to assign the action to the given button and save the document.
// Get the submit button and assign the action:
ButtonField button = (ButtonField) template.getField("ROOT[0].#subform[0].SubmitButton1[0]");
button.setMousePressedAction(action);
doc.saveAs(new File("dst.pdf"));
doc.close();
|