Skip to main content
Version: API v2 Preview

Replace, Insert, and Delete Text

The examples assume an open pdf session. Every operation returns TextEditResponse; inspect it before saving.

Replace text

This replaces at most five whole-word occurrences and lets PDFDancer recompose supported text units.

from pdfdancer import TextLayoutProfile, TextReplaceRequest

response = pdf.text().replace(
TextReplaceRequest.literal("Old product", "New product")
.whole_words(True)
.max_matches(5)
.reflow_when_supported(TextLayoutProfile.DEFAULT)
.build()
)

Replacement text inherits the source appearance. Add font, size, color, or spacing overrides only when the replacement should look different.

Insert relative to existing text

Anchor insertion inherits appearance from the text at the insertion caret.

from pdfdancer import TextInsertRequest, TextLayoutProfile

response = pdf.text().insert(
TextInsertRequest.after("Invoice total", " (including tax)")
.reflow_when_supported(TextLayoutProfile.BODY_TEXT)
.build()
)

Use before, after, beforeRegex, or afterRegex to choose the anchor caret.

Insert at coordinates

Coordinate insertion has no source appearance to inherit, so font and size are required.

response = pdf.text().insert(
TextInsertRequest.at(1, 72, 720, "Reviewed")
.font("Helvetica-Bold")
.size(12)
.build()
)

Delete text

from pdfdancer import TextDeleteRequest

response = pdf.page(2).text().delete(
TextDeleteRequest.regex(r"Confidential\s+draft")
.case_sensitive(False)
.build()
)

Deletion is not redaction. It removes selected text but does not define the security guarantees of a redaction workflow.

Accept or reject the result

For required edits, accept the result only when the expected number of matches changed, errors is empty, and any warnings are acceptable. See Text Layout when text length changes.