Skip to content

Implement PDF forms support in pdfgen.c#164

Draft
Copilot wants to merge 3 commits intomasterfrom
copilot/add-pdf-forms-support
Draft

Implement PDF forms support in pdfgen.c#164
Copilot wants to merge 3 commits intomasterfrom
copilot/add-pdf-forms-support

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 16, 2026

Summary

Adds interactive PDF forms (AcroForms) support to pdfgen, providing three new public API functions and all the infrastructure needed to produce valid, fillable PDF form fields.

New Public API

// Single-line text input field
int pdf_add_text_field(struct pdf_doc *pdf, struct pdf_object *page,
                       float x, float y, float width, float height,
                       const char *name, const char *value,
                       float font_size, uint32_t colour);

// Boolean checkbox
int pdf_add_checkbox(struct pdf_doc *pdf, struct pdf_object *page,
                     float x, float y, float width, float height,
                     const char *name, int checked);

// Radio button (grouped; call once per option)
int pdf_add_radio_button(struct pdf_doc *pdf, struct pdf_object *page,
                         float x, float y, float width, float height,
                         const char *radio_group_name,
                         const char *option_name, int selected);

A new enum (PDF_FIELD_TYPE_TEXT, PDF_FIELD_TYPE_CHECKBOX, PDF_FIELD_TYPE_RADIO, PDF_FIELD_TYPE_RADIO_BUTTON) is exposed in pdfgen.h.

Changes

pdfgen.c

  • OBJ_form_field — new object type added to the object enum.
  • form_field union member — stores field type, page, geometry, name/value strings, font properties, checked state, parent/kids pointers (for radio groups), and on/off appearance XObject references.
  • pdf_object.form_font — new pdf_doc field caching the Helvetica font used for AcroForm /DR and text-field /DA strings.
  • pdf_object_destroy — clears the kids flexarray for radio groups.
  • pdf_save_object — new OBJ_form_field case emitting correct PDF widget annotation dictionaries for all four subtypes (text, checkbox, radio group, radio kid).
  • OBJ_catalog — modified to emit an /AcroForm dictionary (with /Fields, /DR, /DA) when any form fields are present.
  • pdf_create_form_xobject — helper creating Form XObject appearance streams without adding them to any page's content stream list.
  • pdf_get_form_font — helper finding or creating a Helvetica Type1 font for use in form field rendering.
  • Checkboxes render a path-based checkmark (✓); radio buttons render a filled circle approximated by four cubic Bézier segments (control-point distance 0.5523 ≈ 4/3·(√2−1)).
  • Radio groups enforce single-selection: adding a second selected button to the same group returns an error.

tests/main.c

Added a new page 6 ("Form Page") demonstrating:

  • A text field with a default value
  • A checked checkbox and an unchecked checkbox
  • A radio button group with three options (one pre-selected)

tests/tests.sh

Updated expected page count from 5 to 6.

Copilot AI and others added 3 commits April 16, 2026 10:22
Agent-Logs-Url: https://github.com/AndreRenaud/PDFGen/sessions/14a57719-0d42-41fb-ac32-aec780f7dd08

Co-authored-by: AndreRenaud <2322636+AndreRenaud@users.noreply.github.com>
…le-selection validation

Agent-Logs-Url: https://github.com/AndreRenaud/PDFGen/sessions/14a57719-0d42-41fb-ac32-aec780f7dd08

Co-authored-by: AndreRenaud <2322636+AndreRenaud@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants