Skip to content
Search Replace

Search Replace

Search and replace both go through one type, extractor.Editor, built from a reader:

editor := extractor.NewEditor(reader)

The Editor holds the reader and nothing else. It extracts the pages you name, matches your pattern against their text, and maps each match back to the text marks that produced it. Search stops there and returns the offsets and bounding boxes. Replace goes one step further and rewrites the string operands of the underlying content stream operators, which is why replacement keeps the original fonts and glyph positions rather than laying the text out again.

Patterns are Go regular expressions, and page numbers are one-based. Matching is per page, so a phrase spanning a page break is not found.

Both depend on text marks, which only the layout extraction modes produce with byte offsets that line up with the extracted text. The Editor builds its extractor internally with the default mode, so there is nothing to configure, but it does mean the reading order the layout mode infers is the text you are matching against. See text extraction for what the modes do.

Where to look

GuideCovers
SearchMatch offsets and bounding boxes per page.
ReplaceEditing matched text in place, and where it breaks down.

If the goal is to remove text rather than change it, see redaction. If the goal is to add new text over the page, the creator package drawing onto an existing page is a better fit than either.

Last updated on