Text Manipulation
All text drawn by the creator package goes through StyledParagraph. UniPDF v5
removed the older Paragraph component and Creator.NewParagraph along with it,
so c.NewStyledParagraph() is the only way to place text. The guides in this
section cover styling it, and separately, editing text that is already in a PDF.
Chunks and styles
A styled paragraph is a list of TextChunk values, each with its own TextStyle.
Append adds a chunk and returns it, which is how per-chunk styling is done:
p := c.NewStyledParagraph()
chunk := p.Append("bold and blue")
chunk.Style.Font = boldFont
chunk.Style.FontSize = 14
chunk.Style.Color = creator.ColorBlue
if err := c.Draw(p); err != nil {
return err
}SetText is Reset followed by Append: it discards every existing chunk and
returns the single one it created. Reach for it when replacing content, not when
adding to it.
The paragraph-level setters cover the whole paragraph. SetFont, SetFontSize,
SetFontColor and SetStyle each write the paragraph’s default style and then
walk the chunks that already exist, applying the change to all of them. Call order
relative to SetText therefore does not matter, and a per-chunk style set earlier
will be overwritten. Note the name: the color setter is SetFontColor. There is no
SetColor on a styled paragraph.
Assigning chunk.Style = someTextStyle replaces the entire style, including the
font and color. Setting one field, as in chunk.Style.FontSize = 14, leaves the
rest of the style alone. Mixing the two in one paragraph is the usual cause of a
chunk losing its font.
Writing direction is detected per chunk rather than configured. Any chunk whose text is right-to-left is reordered and reversed automatically, so Hebrew and Arabic need a font with the glyphs and nothing else. See right to left text.
Editing text in an existing PDF
Two unrelated approaches exist, and they are not interchangeable.
| Approach | What it does |
|---|---|
contentstream parse and rewrite | Walks the text-showing operators yourself and edits the string operands. |
extractor.Editor | Matches a regular expression against extracted text and patches the underlying string objects. |
The content stream approach is what simple and advanced
cover. You get full control and no dependency on text extraction, at the cost of
handling font encoding and operator boundaries yourself. The extractor.Editor
approach is higher level and is covered in
search and replace.
Neither reflows the page. Both substitute glyphs in place and leave the positioning operators as they were, so a replacement of a different width will overlap the text after it or leave a gap.
Where to look
| Guide | Covers |
|---|---|
| Simple search and replace | Literal replacement in the text-showing operators. |
| Advanced search and replace | Encoding-aware replacement across operator boundaries. |
| Drop caps | Enlarged first character or word, traditional and inline. |
| Gradient text | Filling glyphs with a linear or radial gradient. |
| Right to left text | Hebrew, Arabic and other RTL scripts. |
For text placed in a table cell see tables, and for reading text out of a PDF rather than writing it see extraction.