Create CheckBox Field

Contents
[ ]

PDF forms are widely used for collecting user input in documents such as applications, surveys, and agreements. A checkbox field enables users to select or deselect an option within a form.

Using Aspose.PDF for Python, developers can manipulate PDF forms programmatically. The FormEditor class provides methods to add, edit, and manage form fields within a PDF document.

  1. Load an existing PDF file.
  2. Call the ‘add_field()’ method with the ‘FieldType.CHECK_BOX’ parameter to create the checkbox and specify its position.
  3. Define the field name, caption, and position.
  4. Save the updated PDF document.
import sys
from os import path
import aspose.pdf.facades as pdf_facades

sys.path.append(path.join(path.dirname(__file__), ".."))

from config import set_license, initialize_data_dir


def create_checkbox_field(infile, outfile):
    """Create CheckBox field in PDF document."""
    pdf_form_editor = pdf_facades.FormEditor()
    pdf_form_editor.bind_pdf(infile)

    # Add CheckBox field to PDF form
    pdf_form_editor.add_field(
        pdf_facades.FieldType.CHECK_BOX,
        "checkbox1",
        "Check Box 1",
        1,
        240,
        498,
        256,
        514,
    )

    # Save updated PDF document with form fields
    pdf_form_editor.save(outfile)