ReplaceTag
Page manipulation
Description
Performs a literal byte replacement in the decoded content stream of the selected page, using the ANSI representation of Tag and NewValue and returning the number of non-overlapping substitutions
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 Long
DLL
int DLReplaceTag(int InstanceID, const wchar_t* Tag, const wchar_t* NewValue);
int DLReplaceTagA(int InstanceID, const char* Tag, const char* 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 values
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
The search covers raw decoded content bytes, including operators and resource names; choose unique ASCII placeholders and replacement bytes that preserve valid PDF syntax
Only original occurrences are replaced: a replacement equal to or containing the tag completes normally, and an empty tag performs no substitutions
Placeholders split across operators or represented through PDF escapes or font-specific character codes may not match; annotation contents, form field values, and embedded files are outside this operation
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.PageCount do
begin
PDF.SelectPage(I);
PDF.ReplaceTag('{{customer-name}}', CustomerName);
end;