Posting Forms in PDF via Java
Contents
[
Hide
]
Aspose.PDF for Java supports both facade-based and DOM-based submit button creation.
Add a submit button with FormEditor
- Create a FormEditor facade for the source PDF document.
- Add the configured submit button object through the FormEditor facade.
- Save the updated PDF document.
public static void addSubmitButton(Path inputFile, Path outputFile) {
FormEditor editor = new FormEditor();
editor.bindPdf(inputFile.toString());
try {
editor.addSubmitBtn("submitbutton", 1, "Submit", "http://localhost/testing/show",
100, 450, 150, 475);
editor.save(outputFile.toString());
} finally {
editor.close();
}
}
Add a submit action manually
- Open the source PDF Document.
- Create the SubmitFormAction and URL FileSpecification.
- Create the ButtonField on the target Page and assign the submit action.
- Save the updated PDF Document.
public static void addSubmitAction(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
SubmitFormAction submitAction = new SubmitFormAction();
submitAction.setUrl(new FileSpecification("http://localhost:3000/submit"));
submitAction.setFlags(SubmitFormAction.EXPORT_FORMAT | SubmitFormAction.SUBMIT_COORDINATES);
ButtonField submitButton = new ButtonField(document.getPages().get_Item(1), new Rectangle(10, 10, 100, 40));
submitButton.setPartialName("SubmitButton");
submitButton.setValue("Submit");
submitButton.getPdfActions().add(submitAction);
document.getForm().add(submitButton, 1);
document.save(outputFile.toString());
}
}