Skip to content

fix: Content returns the text of form XObjects - #97

Open
anaelorlinski wants to merge 2 commits into
ledongthuc:masterfrom
anaelorlinski:fix-content-form-xobjects
Open

anaelorlinski wants to merge 2 commits into
ledongthuc:masterfrom
anaelorlinski:fix-content-form-xobjects

Conversation

@anaelorlinski

Copy link
Copy Markdown

Content() had no case for Do, so text drawn inside a form XObject (form-field appearances, embedded diagrams, headers placed as forms) was missing. Do now interprets a form's content stream in a saved graphics state, with the form's /Matrix concatenated to the CTM and its own /Resources (or those of the painting stream; PDF 32000-1:2008 §8.10). Image XObjects are ignored as before; nesting is bounded by maxFormDepth since a form can paint itself.

Refs #67 (which covers GetPlainText; this is the Content() side). Tests build small PDFs in memory, like buildContentPDF.

Content had no case for the Do operator, so the text drawn inside a form
XObject (form-field appearances, embedded diagrams, headers placed as
forms) was missing from the page.

Do now interprets a form XObject's content stream in a saved graphics
state, with the form's /Matrix concatenated to the CTM and the form's
own /Resources (or, without them, those of the content stream painting
it; PDF 32000-1:2008 §8.10). Image XObjects are ignored as before.
Nesting is bounded by maxFormDepth since a form can paint itself.

Refs ledongthuc#67
Comment thread page.go
// }
return

case "Do": // paint an XObject; the text drawn by a form XObject is part of the page

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some time users want to read text in XObject directly. So I think it's better to define XObject as a seperate struct, like pdf.Page.

Address review on ledongthuc#97: users sometimes want to read the text inside an
XObject directly, not only as a side effect of rendering the page.

Add an exported XObject type that mirrors Page: Page.XObjects /
Page.XObject reach a page's XObjects by name, XObject.XObjects /
XObject.XObject reach nested ones, XObject.IsForm reports whether it is a
form, and XObject.Content interprets a form's content stream (with its
/Matrix applied) to return the text and rectangles it draws.

Content's form handling is factored into a shared interpretContentStream
helper so Page.Content and XObject.Content run the same interpreter. The
Do operator recurses through it, keeping the existing behaviour: a form
paints in a saved graphics state with its /Matrix concatenated to the
CTM, a form without /Resources uses those of the painting stream, image
XObjects add no text, and nesting is bounded by maxFormDepth.
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