Skip to content

BUG: Generate an appearance stream for FreeText annotations - #4358

Open
Mluo2011 wants to merge 3 commits into
py-pdf:mainfrom
Mluo2011:fix/freetext-annotation-ap-stream
Open

Mluo2011 wants to merge 3 commits into
py-pdf:mainfrom
Mluo2011:fix/freetext-annotation-ap-stream

Conversation

@Mluo2011

@Mluo2011 Mluo2011 commented Nov 5, 2026

Copy link
Copy Markdown

Fixes #2372.

FreeText.__init__ set /DA, /DS, /C and /BS but never built an appearance
stream (/AP). Viewers that don't synthesize one — notably PDFium, which Chrome
uses — draw the annotation box but not the text.

This adds a _free_text_appearance_stream() helper that builds a normal appearance
stream (/AP /N) from the annotation's background, border, colour, size and
bold/italic settings, and add_annotation now stores it as an indirect object (a
stream can't sit directly inside the annotation dict).

Test tests/test_annotations.py::test_free_text__appearance_stream fails on the
unpatched code with KeyError: '/AP' and passes after; test_annotations.py is
52/52. Rendered with PDFium the text now shows.

Known limitations (happy to address if preferred):

  • the font= argument is drawn as standard Helvetica; text is not wrapped;
  • characters outside cp1252 are replaced with ?.

Prepared with help from an AI coding assistant; I reviewed and tested it locally.

FreeText annotations were written without an /AP entry, so viewers
that do not synthesize one (such as Chrome/PDFium) showed the box but
not the text. Build a normal appearance stream (/AP /N) from the
annotation's background, border, color, size and bold/italic settings,
and store it as an indirect object when the annotation is added.

Fixes py-pdf#2372
@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.81%. Comparing base (f5b3f07) to head (c373f52).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4358   +/-   ##
=======================================
  Coverage   98.81%   98.81%           
=======================================
  Files          59       59           
  Lines       12139    12174   +35     
  Branches     2310     2317    +7     
=======================================
+ Hits        11995    12030   +35     
  Misses         70       70           
  Partials       74       74           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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.

FreeText annotation not showing in chrome browser

1 participant