阅读文本 / Reading text
This paper uses synthetic samples to demonstrate the processing method. The code has been verified using examples in an isolated environment; this does not guarantee effectiveness for all real files or products. Before processing important files, please retain the original copies and verify them against the boundaries and acceptance criteria outlined in this document.
Which problem does this paper address first?
The values of AcroForm fields and the text rendered on the page come from different sources. Below, we construct text fields using PyMuPDF and then read them with pypdf; this process does not involve XFA, nor does it claim that field values necessarily match the visual appearance. When no form is present, do not directly index using keys that are not in the dictionary.
self-contained synthetic example
dependencies are listed in the import section. The code constructs inputs in memory; runtime logs are provided separately and do not constitute a guarantee for all real-world files.
from io import BytesIO
import pymupdf
from pypdf import PdfReader, PdfWriter
with pymupdf.open() as doc:
page = doc.new_page()
widget = pymupdf.Widget()
widget.field_name = "applicant_name"
widget.field_type = pymupdf.PDF_WIDGET_TYPE_TEXT
widget.field_value = "Alice"
widget.rect = pymupdf.Rect(50, 50, 200, 80)
page.add_widget(widget)
raw = doc.tobytes()
fields = PdfReader(BytesIO(raw)).get_form_text_fields()
assert isinstance(fields, dict)
assert fields["applicant_name"] == "Alice"
blank = PdfWriter()
blank.add_blank_page(width=200, height=200)
out = BytesIO()
blank.write(out)
assert not PdfReader(BytesIO(out.getvalue())).get_form_text_fields()
When you use
page.extract_text()
When extracting a filled-out PDF form, the returned result often consists only of static labels (such as “Name:” and “Date:”), while the user’s actual input disappears without a trace. This is not due to a faulty parser; rather, it stems from the design of the PDF specification itself: form field values are stored in separate
/AcroForm
In the structure, rather than within the page’s content flow. To retrieve this data, one must invoke a dedicated form interface and treat field extraction as a separate validation step.
Storage isolation of form data from page content
The PDF form system has a dual nature. The page content stream is responsible for rendering static text and graphics, while interactive fields are defined in the document’s root object.
/AcroForm/Fields
in the array. Each field object contains
/T
(name),
/V
(Actual value) and other attributes, whose visual presentation is determined by the page’s
/Widget
annotation carrier. This means that even if a field visually overlaps with a piece of text, its data remains entirely outside the scope of that page’s text extraction.
This separation leads to two common misconceptions: one is the belief that
extract_text()
should contain all visible characters; second, it assumes that traversing the page’s comments can fully restore the form state. In reality,
get_fields()
returns a logical field structure, which may include hierarchical relationships or radio button groups with shared values, whereas page annotations only reflect local rendering instances. The two differ in how they reference underlying objects; direct mixing can lead to data loss or duplication.
synthesis example: Correct reading and boundary handling
The following example uses a memory-based PDF to simulate a form scenario, demonstrating how to safely extract field values and handle exceptional cases.
Note: This code is for instructional demonstration and has not been executed in a real production environment; readers are required to prepare a structurally compliant PDF for verification on their own.
Dependency note: Installation required
pypdf>=3.0.0
. In the example, the two PDF files must be prepared by the reader using the method described above; the code does not include the generation logic. The assertions only specify the API’s expected behavior and do not indicate that they have been verified on any specific PDF.
Acceptance criteria for field extraction and the boundaries of manual review
Note: The code demonstration only validates the API call logic; the end-to-end performance depends on the compliance of the PDF generator. When encountering XFA, encrypted, or corrupted
/AcroForm
During structuring, it should be routed to the manual review process; automation must not be enforced.
Rendering Traps and Flattening Strategies When Writing to Forms
When filling out the form,
update_page_form_field_values()
is enabled by default
auto_regenerate=True
, which will trigger the PDF reader to re-render the field appearance, potentially causing a “Save Changes” prompt to appear upon opening. In production environments, this should always be set to
False
, unless you explicitly need to update the visual presentation and can control the downstream viewer's behavior.
If it is necessary to convert the form into a purely static document (e.g., for archiving or pre-processing before translation), one may first set
flatten=True
writes the field value into the content stream, then invokes
remove_annotations(subtypes="/Widget")
Remove interactive elements. This operation is irreversible; be sure to keep the original copy. Note: Although the flattened text can be processed by
extract_text()
is captured, but its structured semantics have been lost; subsequent processing must treat it as a regular document.
References and Next Steps
pypdf Forms Documentation
Next step: Prepare a composite PDF containing multi-level fields and radio buttons, run the aforementioned validation logic in your locked dependency environment, record the correspondence between field names and actual values, and establish an internal acceptance baseline.
The specific handling decision for the exception
observes
What should be done
should not infer
field exists
reads the field value
Visual appearance, see also
returns no fields
as a formless state
does not directly index the key name
XFA file
separate recognition processing range
does not guarantee ordinary AcroForm code coverage
uses this checklist for document translation
file splitting, encryption, or attachment processing are part of delivery preparation; translation cannot replace these checks. Retain the original documents and the processing checklist, then assess the translated manuscript’s page order, hyperlinks, and content integrity.
OmniDoc (Product Entry)
provides an entry point for translating multi-format documents, and the official website highlights its OCR, terminology database, and bilingual alignment features. For teams that wish to start processing directly from files, this is a path worth evaluating with real-world samples. Please use de-identified files that you are authorized to upload, and verify the results against the checklist provided in this document; we do not claim that OmniDoc utilizes the open-source libraries or internal algorithms referenced herein, nor do we substitute examples for formal product performance testing.
- 1 -
Updated 2026-10-09