Skip to content

Commit cb6d88f

Browse files
improve wording and formatting
1 parent 99938f4 commit cb6d88f

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

docs/user/forms.md

+31-27
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,36 @@ parameter is `True` by default for legacy compatibility, but this flags the PDF
4242
Viewer to recompute the field's rendering, and may trigger a "save changes"
4343
dialog for users who open the generated PDF.
4444

45-
## A note about form fields and annotations
45+
## Some notes about form fields and annotations
46+
4647
PDF forms have a dual-nature approach about the fields:
47-
* within the root object, an `/AcroForm` structure exists.
48+
49+
* Within the root object, an `/AcroForm` structure exists.
4850
Inside it you could find (optional):
49-
- some global elements (Fonts, Ressources,...)
50-
- some global flags (like `/NeedAppearances` (set/cleared with `auto_regenerate` parameter in `update_form_field_values()`) that indicates if the reading program should re-render the visual fields upon document launch)
51-
- `/XFA` that houses a form in XDP format (very specific XML that describes the form rendered by some viewers) ; the `/XFA` form overrides the page content.
52-
- `/Fields` that houses array of indirect references that references TopMost(roots) _Field_ Objects
53-
* within the page `/Annots`, you will spot `/Widget` annotations that defines the visual renderings.
51+
52+
- some global elements (Fonts, Resources,...)
53+
- some global flags (like `/NeedAppearances` (set/cleared with `auto_regenerate` parameter in `update_form_field_values()`) that indicates if the reading program should re-render the visual fields upon document launch)
54+
- `/XFA` that houses a form in XDP format (very specific XML that describes the form rendered by some viewers); the `/XFA` form overrides the page content
55+
- `/Fields` that houses an array of indirect references that reference the upper _Field_ Objects (roots)
56+
57+
* Within the page `/Annots`, you will spot `/Widget` annotations that define the visual rendering.
5458

5559
To flesh out this overview:
56-
* the core specific properties of a fields are :
57-
- `/FT` : Field Type (Button, Text, Choice, Signatures)
58-
- `/T` : Partial Field Name (see PDF Reference for more details)
59-
- `/V` : Field Value
60-
- `/DV` : Default Field Value (used when reseting a form for exemple)
61-
* in order to streamline readability, _Field_ Objects and _Widget_ Objects can be fused housing all properties.
62-
* Field can be organised hierarchically, meaning one field can be placed under another. in such instances, the `/Parent` will stock an IndirectObject providing Bottom-Up links and `/Childs` is an array carrying IndirectObjects for Top-Down navigation ; _Widget_ Objects are still required for visual rendering ; to call upon them, use *full qualified field name* (with all the individual names of the parent objects are seperated by `.`)
63-
For instance 2 (visual) fields both called _city_ but attached below _sender_ and _receiver_ ; the data full names will be _sender.city_ and _receiver.city_
64-
* When a field is repeated on multiple pages, the Field Object will have many _Widget_ Objects in `/Childs`. These objects are pure _widgets_, containing no _field_ specific data
65-
* if Fields stores only hidden values, No _Widget_ are required.
66-
67-
In _pypdf_ fields are extracted from the `/Fields` array
60+
61+
* The core specific properties of a field are:
62+
- `/FT`: Field Type (Button, Text, Choice, Signatures)
63+
- `/T`: Partial Field Name (see PDF Reference for more details)
64+
- `/V`: Field Value
65+
- `/DV` : Default Field Value (used when resetting a form for example)
66+
* In order to streamline readability, _Field_ Objects and _Widget_ Objects can be fused housing all properties.
67+
* Fields can be organised hierarchically, id est one field can be placed under another. In such instances, the `/Parent` will have an IndirectObject providing Bottom-Up links and `/Childs` is an array carrying IndirectObjects for Top-Down navigation; _Widget_ Objects are still required for visual rendering. To call upon them, use the *fully qualified field name* (where all the individual names of the parent objects are seperated by `.`)
68+
69+
For instance take two (visual) fields both called _city_, but attached below _sender_ and _receiver_; the corresponding full names will be _sender.city_ and _receiver.city_.
70+
* When a field is repeated on multiple pages, the Field Object will have many _Widget_ Objects in `/Childs`. These objects are pure _widgets_, containing no _field_ specific data.
71+
* If Fields stores only hidden values, no _Widgets_ are required.
72+
73+
In _pypdf_ fields are extracted from the `/Fields` array:
74+
6875
```python
6976
from pypdf import PdfReader
7077

@@ -78,20 +85,17 @@ from pypdf.constants import AnnotationDictionaryAttributes
7885

7986
reader = PdfReader("form.pdf")
8087
fields = []
81-
for page in reader.pagesP:
88+
for page in reader.pages:
8289
for annot in page.annotations:
8390
annot = annot.get_object()
8491
if annot[AnnotationDictionaryAttributes.Subtype] == "/Widget":
8592
fields.append(annot)
8693
```
8794

88-
However, while similar, there are some very important differences between the two above blocks of code. Most importantly, the first block will return a list of Field objects, where as the second will return more generic dictionary-like objects. The objects lists will *mostly* reference the same object in the underlying PDF, meaning you'll find that `obj_taken_fom_first_list.indirect_reference == obj_taken_from _second_list.indirect_reference`. Field objects are generally more ergonomic, as the exposed data can be access via clearly named properties. However, the more generic dictionary-like objects will contain data that the Field object does not expose, such as the Rect (the widget's position on the page). So, which to use will depend on your use case.
89-
90-
However, it's also important to note that the two lists do not *always* refer to the same underlying PDF objects. For example, if the form contains radio buttons, you will find that `reader.get_fields()` will get the parent object (the group of radio buttons) whereas `page.annotations` will return all the child objects (the individual radio buttons).
95+
However, while similar, there are some very important differences between the two above blocks of code. Most importantly, the first block will return a list of Field objects, whereas the second will return more generic dictionary-like objects. The objects lists will *mostly* reference the same object in the underlying PDF, meaning you'll find that `obj_taken_fom_first_list.indirect_reference == obj_taken_from _second_list.indirect_reference`. Field objects are generally more ergonomic, as the exposed data can be accessed via clearly named properties. However, the more generic dictionary-like objects will contain data that the Field object does not expose, such as the Rect (the widget's position on the page). Therefore the correct approach depends on your use case.
9196

97+
However, it's also important to note that the two lists do not *always* refer to the same underlying PDF object. For example, if the form contains radio buttons, you will find that `reader.get_fields()` will get the parent object (the group of radio buttons) whereas `page.annotations` will return all the child objects (the individual radio buttons).
9298

93-
__Caution:
94-
Remember that fields are not stored in pages: If you use `add_page()` the field structure is not copied.
95-
It is recommended to use `.append() with the proper parameters`__
99+
__Caution: Remember that fields are not stored in pages: If you use `add_page()` the field structure is not copied. It is recommended to use `.append()` with the proper parameters instead.__
96100

97-
In case of missing _field_ objects in `/Fields`, `writer.reattach_fields()` will parse page(s) annotations and will reattach them. This fix can not guess intermediate fields and will not report fields using the same _name_
101+
In case of missing _field_ objects in `/Fields`, `writer.reattach_fields()` will parse page(s) annotations and will reattach them. This fix can not guess intermediate fields and will not report fields using the same _name_.

0 commit comments

Comments
 (0)