Posting Forms in PDF via Python
Contents
[
Hide
]
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.
- Create a
FormEditorobject. - Add a submit button to the target page.
- Set the submission URL and button coordinates.
- 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.
- Open the PDF with ap.Document().
- Create a submit action.
- Set the target URL and submission flags.
- Create a button field and bind its click action.
- Add the button to the form.
- 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)