Class ContentReplacer
ContentReplacer is a utility class for replacing content (text and images) in existing PDF (template) documents.
Users can replace content in a PDF page using the following operations:
- Replace an image that exists in a target rectangle with a replacement image.
- Replace text that exists in a target rectangle with replacement text.
- Replace all instances of a specially marked string with replacement string.
PDFDoc doc("../../TestFiles/BusinessCardTemplate.pdf");
doc.InitSecurityHandler();
ContentReplacer replacer;
Page pg = doc.GetPage(1);
Image img = Image.Create(doc, "../../TestFiles/peppers.jpg");
replacer.AddImage(page.GetMediaBox(), img.GetSDFObj());
replacer.AddString("NAME", "John Smith");
replacer.AddString("JOB_TITLE", "Software Developer");
replacer.Process(page);
Implements
Inherited Members
Namespace: pdftron.PDF
Assembly: PDFTronDotNet.dll
Syntax
public class ContentReplacer : IDisposable
Constructors
ContentReplacer()
Create a new ContentReplacer object, to which replacement rules will be added. The same object can be used to 'Process' multiple pages.
Declaration
public ContentReplacer()
Methods
AddImage(Rect, Obj)
Replace the image that best fits 'target_region' with 'replacement_image'.
Declaration
public void AddImage(Rect target_region, Obj replacement_image)
Parameters
Type | Name | Description |
---|---|---|
Rect | target_region | The rectangle defining the area in which an image that best fits the rectangle will be replaced by 'replacement_image'. |
Obj | replacement_image | The 'SDF.Obj' of a 'PDF.Image' object. |
Remarks
The best fit is the image that closest matches 'target_region'. For example if there are two images on the page, one taking up the entire page, and the other smaller, and the smaller one has similar dimensions and position of 'target_region', then the smaller image would be replaced, not the larger. Furthermore, if 'target_region' encloses multiple images, then only the image with the largest area will be replaced.
AddString(string, string)
Any text of the form "[template_text]" will be replaced by "replacement_text".
Declaration
public void AddString(string template_text, string replacement_text)
Parameters
Type | Name | Description |
---|---|---|
string | template_text | The text to remove. |
string | replacement_text | The new text that will appear in place of 'template_text'. |
Remarks
Only text wrapped in '[' and ']' will be checked, and if it matches 'template_text', then 'template_text' and the surrounding square braces will be replaced by 'replacement_text'. For example AddString("TITLE", "Doctor") will replace any text consisting of "[TITLE]" with "Doctor".
AddText(Rect, string)
All text inside 'target_region' will be deleted and replaced with 'replacement_text'.
Declaration
public void AddText(Rect target_region, string replacement_text)
Parameters
Type | Name | Description |
---|---|---|
Rect | target_region | The rectangle defining the area in which all text will be replaced by 'replacement_text'. |
string | replacement_text | The new text that will replace the existing text in 'target_region'. |
Remarks
The 'replacement_text' will be styled in the same font/color/style that is used by the original text. If there are multiple font styles, the most prevalent style will be used. Also, the 'replacement_text' will wrap within the 'target_region', but if it is too long, the overflow text will not be visible, and no surrounding content will be affected.
Destroy()
Declaration
public void Destroy()
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Declaration
public void Dispose()
Dispose(bool)
Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
bool | disposing |
~ContentReplacer()
Releases all resources used by the ContentReplacer
Declaration
protected ~ContentReplacer()
Process(Page)
Apply the replacement instructions to the target page. Subsequent calls to 'Process' can be made on other pages, and it will apply the same rules.
Declaration
public void Process(Page page)
Parameters
Type | Name | Description |
---|---|---|
Page | page | The page to apply the content replacement instructions to. |