Posting Forms in PDF via Python

Add Submit Button with FormEditor

The following code snippet demonstrates how to add a submit button to a PDF form using the FormEditor class in Aspose.PDF for Python via .NET. The button is configured to submit form data to a specified URL when clicked.

  1. Create a FormEditor object.
  2. Add a submit button to the target page.
  3. Set the submission URL and button coordinates.
  4. Save the updated PDF.
import aspose.pdf as ap

def add_submit_button(input_file_name, output_file_name):
    editor = ap.facades.FormEditor(input_file_name, output_file_name)
    editor.add_submit_btn(
        "submitbutton", 1, "Submit", "http://localhost/testing/show", 100, 450, 150, 475
    )
    editor.save()

Add Custom Submit Action

The following code snippet explains how to create a custom submit button in a PDF form using Aspose.PDF for Python via .NET. The button is configured to send form data to a specified URL when clicked.

  1. Open the PDF with ap.Document().
  2. Create a submit action.
  3. Set the target URL and submission flags.
  4. Create a button field and bind its click action.
  5. Add the button to the form.
  6. Save the updated PDF.
import aspose.pdf as ap

def add_submit_action(input_file_name, output_file_name):
    document = ap.Document(input_file_name)

    submit_action = ap.annotations.SubmitFormAction()
    submit_action.url = ap.FileSpecification("http://localhost:3000/submit")
    submit_action.flags = (
        ap.annotations.SubmitFormAction.EXPORT_FORMAT
        | ap.annotations.SubmitFormAction.SUBMIT_COORDINATES
    )

    rect = ap.Rectangle(10, 10, 100, 40)
    submit_button = ap.forms.ButtonField(document.pages[1], rect)
    submit_button.partial_name = "SubmitButton"
    submit_button.value = "Submit"
    submit_button.actions.on_release_mouse_btn = submit_action

    document.form.add(submit_button, 1)
    document.save(output_file_name)