Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3aaa776
MAINT: _writer: let _update_field_annotation return appearance stream
PJBrs Sep 14, 2025
db538ba
MAINT: _writer: refactor update_page_form_fields
PJBrs Sep 14, 2025
638f7d6
MAINT: Refactor code from _writer to _appearance_stream
PJBrs Sep 15, 2025
3706aba
MAINT: Turn the appearance stream code into a class
PJBrs Sep 15, 2025
fa72a70
MAINT: _appearance_stream: Make variables more clear and readable
PJBrs Sep 17, 2025
c10b691
MAINT: _appearance_stream: Rename font_height to font_size
PJBrs Sep 30, 2025
e179ab9
MAINT: _appearance_stream: More comments
PJBrs Sep 30, 2025
fedd0a4
MAINT: _writer.py: Make some variables more readable
PJBrs Oct 26, 2025
3842c9b
MAINT: _appearance_stream: Factor out generation of text appearance
PJBrs Sep 18, 2025
225621e
MAINT: _appearance_stream: Move y_offset calculation
PJBrs Sep 19, 2025
7add9e2
MAINT: _appearance_stream: Move multiline parsing
PJBrs Sep 19, 2025
30d3df8
MAINT: _appearance_writer: Don't pass default_appearance
PJBrs Sep 19, 2025
224a8e8
MAINT: _appearance_stream: Move font_resource parsing
PJBrs Sep 21, 2025
9e83e96
MAINT: _appearance_stream: Document all methods
PJBrs Sep 20, 2025
7c249d3
MAINT: _appearance_stream: Shorten some code
PJBrs Oct 26, 2025
9452698
ROB: _font: Always returns a FontDescriptor; fix typing
PJBrs Sep 13, 2025
dd832f9
ENH: _font: Add method to calculate text width
PJBrs Sep 14, 2025
06c430e
ENH: TextAppearanceStream: Add method to scale and wrap text
PJBrs Sep 24, 2025
ffa433d
ENH: TextAppearanceStream: Scale and wrap text
PJBrs Sep 24, 2025
2fea98c
ROB: TextAppearanceStream: Add default font resource
PJBrs Sep 25, 2025
5548f0e
ENH: TextAppearanceStream: Add right alignment and centering
PJBrs Sep 24, 2025
86d7c4a
MAINT: TextAppearanceStream: Don't use _default_fonts_space_width
PJBrs Sep 25, 2025
99b72c0
ENH: tests: _appearance_stream
PJBrs Sep 25, 2025
5f10527
ENH: docs: Add documentation about flattening a PDF form
PJBrs Sep 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/user/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ writer.update_page_form_field_values(
writer.pages[0],
{"fieldname": "some filled in text"},
auto_regenerate=False,
flatten=False,
)

with open("filled-out.pdf", "wb") as output_stream:
Expand All @@ -41,6 +42,12 @@ parameter is `True` by default for legacy compatibility, but this flags the PDF
processor to recompute the field's rendering, and may trigger a "save changes"
dialog for users who open the generated PDF.

If you want to flatten your form, that is, keeping all form field contents while
removing the form fields themselves, you can set `flatten=True` to convert form
field contents to regular pdf content, and then use
`writer.remove_annotations(subtypes="/Widget")` to remove all form fields. This
will result in a flattened pdf.

## Some notes about form fields and annotations

PDF forms have a dual-nature approach to the fields:
Expand Down
15 changes: 10 additions & 5 deletions pypdf/_font.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dataclasses import dataclass, field
from typing import Optional

from pypdf.generic import DictionaryObject

Expand Down Expand Up @@ -29,10 +28,16 @@ class FontDescriptor:
character_widths: dict[str, int] = field(default_factory=dict)

@classmethod
def from_font_resource(cls, pdf_font_dict: DictionaryObject) -> "Optional[FontDescriptor]":
def from_font_resource(cls, pdf_font_dict: DictionaryObject) -> "FontDescriptor":
from pypdf._codecs.core_fontmetrics import CORE_FONT_METRICS # noqa: PLC0415
# Prioritize information from the PDF font dictionary
font_name = pdf_font_dict.get("/BaseFont", "Unknown")
if font_name[1:] in CORE_FONT_METRICS:
return CORE_FONT_METRICS.get(font_name[1:])
font_name = pdf_font_dict.get("/BaseFont", "Unknown").removeprefix("/")
if font_name in CORE_FONT_METRICS:
return CORE_FONT_METRICS[font_name]
return cls(name=font_name)

def text_width(self, text: str) -> float:
"""Sum of character widths specified in PDF font for the supplied text."""
return sum(
[self.character_widths.get(char, self.character_widths.get("default", 0)) for char in text], 0.0
)
Loading
Loading