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.
- Python
- TypeScript
- Java
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()
)
import {TextLayoutProfile, TextReplaceRequest} from 'pdfdancer-client-typescript';
const response = await pdf.text().replace(
TextReplaceRequest.literal('Old product', 'New product')
.wholeWords(true)
.maxMatches(5)
.reflowWhenSupported(TextLayoutProfile.DEFAULT)
.build()
);
import com.pdfdancer.common.request.TextLayoutRequest;
import com.pdfdancer.common.request.TextReplaceRequest;
var response = pdf.text().replace(
TextReplaceRequest.literal("Old product", "New product")
.wholeWords(true)
.maxMatches(5)
.reflowWhenSupported(TextLayoutRequest.Profile.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.
- Python
- TypeScript
- Java
from pdfdancer import TextInsertRequest, TextLayoutProfile
response = pdf.text().insert(
TextInsertRequest.after("Invoice total", " (including tax)")
.reflow_when_supported(TextLayoutProfile.BODY_TEXT)
.build()
)
const response = await pdf.text().insert(
TextInsertRequest.after('Invoice total', ' (including tax)')
.reflowWhenSupported(TextLayoutProfile.BODY_TEXT)
.build()
);
var response = pdf.text().insert(
TextInsertRequest.after("Invoice total", " (including tax)")
.reflowWhenSupported(TextLayoutRequest.Profile.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.
- Python
- TypeScript
- Java
response = pdf.text().insert(
TextInsertRequest.at(1, 72, 720, "Reviewed")
.font("Helvetica-Bold")
.size(12)
.build()
)
const response = await pdf.text().insert(
TextInsertRequest.at(1, 72, 720, 'Reviewed')
.font('Helvetica-Bold')
.size(12)
.build()
);
var response = pdf.text().insert(
TextInsertRequest.at(1, 72, 720, "Reviewed")
.font("Helvetica-Bold")
.size(12)
.build());
Delete text
- Python
- TypeScript
- Java
from pdfdancer import TextDeleteRequest
response = pdf.page(2).text().delete(
TextDeleteRequest.regex(r"Confidential\s+draft")
.case_sensitive(False)
.build()
)
const response = await pdf.page(2).text().delete(
TextDeleteRequest.regex('Confidential\\s+draft')
.caseSensitive(false)
.build()
);
var response = pdf.page(2).text().delete(
TextDeleteRequest.regex("Confidential\\s+draft")
.caseSensitive(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.