ReplaceTag
Page manipulation
Description
Performs a literal find-and-replace on the text content of the currently selected page. Every occurrence of Tag in the page's text-showing operators is replaced with NewValue and the operator stream is rewritten in place. Returns the number of substitutions performed.
This is a content-stream level operation that does not relate to tagged PDF (structure tree) tagging — use BeginTag/EndTag for that. ReplaceTag is intended for form-letter style templates where placeholder tokens (for example {{customer-name}}) are pre-drawn into a template PDF and then filled in by code.
Syntax
Delphi
function TPDFlib.ReplaceTag(const Tag, NewValue: WideString): Integer;ActiveX
Function PDFlib::ReplaceTag(Tag As String, NewValue As String) As LongDLL
int DLReplaceTag(int InstanceID, wchar_t * Tag, wchar_t * NewValue);Parameters
| Tag | The literal string to search for in the page's text content. The match is case-sensitive and applies only to the currently selected page — call once per page when processing a multi-page template. |
|---|---|
| NewValue | The replacement string. May be longer or shorter than Tag; the surrounding layout is not reflowed, so very large substitutions can run past the original column. |
Return value
The number of times Tag was found and replaced on the selected page. Zero means no occurrences were found (or no page is selected).
Remarks
Only text inside text-showing operators (Tj, TJ, ', ") is searched. Placeholders that have been kerned or split across multiple operators may not match — keep template placeholders short and draw them as a single string. The function does not search annotation contents, form field values, or embedded files.
Choose placeholder syntax that is unlikely to occur in real content, such as {{name}}, <<Customer>>, or ~~SKU~~.
Example
// Fill in a single-page invoice template
PDF.LoadFromFile('invoice-template.pdf', '');
PDF.SelectPage(1);
PDF.ReplaceTag('{{customer-name}}', 'Alice Smith');
PDF.ReplaceTag('{{invoice-no}}', 'INV-2026-0042');
PDF.ReplaceTag('{{total}}', '$1,234.56');
PDF.SaveToFile('invoice-INV-2026-0042.pdf');
// Multi-page template: iterate over pages
for I := 1 to PDF.GetPageCount do
begin
PDF.SelectPage(I);
PDF.ReplaceTag('{{customer-name}}', CustomerName);
end;