new 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.
The following code replaces an image in a target region. This code also
replaces the text "[NAME]" and "[JOB_TITLE]" with "John Smith"
and "Software Developer" respectively. Notice the square braces ('[' and ']') on
the target strings in the original PDFDoc. These square braces are not included in
the actual function calls below, as they're implicitly added.
PDFDoc doc("../../TestFiles/BusinessCardTemplate.pdf"); doc.InitSecurityHandler(); ContentReplacer replacer; Page page = 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);
Extends
Methods
-
<static> create()
-
Create a new ContentReplacer object, to which replacement rules will be added. The same object can be used to 'Process' multiple pages.
Returns:
A promise that resolves to an object of type: "PDFNet.ContentReplacer"- Type
- Promise.<PDFNet.ContentReplacer>
-
addImage(target_region, replacement_image)
-
Replace the image that best fits into 'target_region' with 'replacement_image'.
Parameters:
Name Type Description target_region
PDFNet.Rect The rectangle defining the area in which an image that best fits the rectangle will be replaced by 'replacement_image'. replacement_image
PDFNet.Obj The 'SDF.Obj' of a 'PDF.Image' object. Returns:
- Type
- Promise.<void>
-
addString(template_text, replacement_text)
-
Any text of the form "[template_text]" will be replaced by "replacement_text".
Parameters:
Name Type Description template_text
string The text to remove. replacement_text
string The new text that will appear in place of 'template_text'. Returns:
- Type
- Promise.<void>
-
addText(target_region, replacement_text)
-
All text inside 'target_region' will be deleted and replaced with 'replacement_text'.
Parameters:
Name Type Description target_region
PDFNet.Rect The rectangle defining the area in which all text will be replaced by 'replacement_text'. replacement_text
string The new text that will replace the existing text in 'target_region'. Returns:
- Type
- Promise.<void>
-
destroy()
-
Destructor
- Inherited From:
Returns:
- Type
- Promise.<void>
-
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.
Parameters:
Name Type Description page
PDFNet.Page The page to apply the content replacement instructions to. Returns:
- Type
- Promise.<void>
-
setMatchStrings(start_str, end_str)
-
Change the delimiters from '[' and ']' to arbitary strings.
Parameters:
Name Type Description start_str
string The starting delimiter string. end_str
string The ending delimiter string. Returns:
- Type
- Promise.<void>
-
takeOwnership()
-
Take the ownership of this object, so that PDFNet.runWithCleanup won't destroy this object.
- Inherited From:
Returns:
- Type
- void