Posting Forms in PDF via Java

Aspose.PDF for Java supports both facade-based and DOM-based submit button creation.

Add a submit button with FormEditor

  1. Create a FormEditor facade for the source PDF document.
  2. Add the configured submit button object through the FormEditor facade.
  3. 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

  1. Open the source PDF Document.
  2. Create the SubmitFormAction and URL FileSpecification.
  3. Create the ButtonField on the target Page and assign the submit action.
  4. 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());
    }
}