java.lang.Object | |
↳ | com.pdftron.pdf.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:
PDFDoc doc = new PDFDoc("../../TestFiles/BusinessCardTemplate.pdf");
doc.initSecurityHandler();
ContentReplacer replacer = new ContentReplacer();
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);
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
ContentReplacer()
Create a new ContentReplacer object, to which replacement rules will be added.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void |
addImage(Rect target_region, Obj replacement_image)
Replace the image that best fits into 'target_region' with 'replacement_image'.
| ||||||||||
void |
addString(String template_text, String replacement_text)
Any text of the form "[template_text]" will be replaced by "replacement_text".
| ||||||||||
void |
addText(Rect target_region, String replacement_text)
All text inside 'target_region' will be deleted and replaced with 'replacement_text'.
| ||||||||||
void |
close()
Frees the native memory of the object.
| ||||||||||
void |
destroy()
Frees the native memory of the object.
| ||||||||||
void |
process(Page page)
Apply the replacement instructions to the target page.
| ||||||||||
void |
setMatchStrings(String start_str, String end_str)
Change the delimiters from '[' and ']' to arbitary strings.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
java.lang.AutoCloseable
|
Create a new ContentReplacer object, to which replacement rules will be added. The same object can be used to 'Process' multiple pages.
PDFNetException |
---|
Replace the image that best fits into 'target_region' with 'replacement_image'.
Note: The best fit is the image that closest matches 'target_region'. For example if there are two images on the page, one taking up all of the 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 in 'target_region' will be replaced.
target_region | - The rectangle defining the area in which an image that best fits the rectangle will be replaced by 'replacement_image'. |
---|---|
replacement_image | - The 'SDF.Obj' of a 'PDF.Image' object. |
PDFNetException |
---|
Any text of the form "[template_text]" will be replaced by "replacement_text".
Note: 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".
template_text | - The text to remove. |
---|---|
replacement_text | - The new text that will appear in place of 'template_text'. |
PDFNetException |
---|
All text inside 'target_region' will be deleted and replaced with 'replacement_text'.
Note: 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.
target_region | - The rectangle defining the area in which all text will be replaced by 'replacement_text'. |
---|---|
replacement_text | - The new text that will replace the existing text in 'target_region'. |
PDFNetException |
---|
Frees the native memory of the object. This can be explicity called to control the deallocation of native memory and avoid situations where the garbage collector does not free the object in a timely manner.
PDFNetException |
---|
Frees the native memory of the object. This can be explicitly called to control the deallocation of native memory and avoid situations where the garbage collector does not free the object in a timely manner.
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.
page | - The page to apply the content replacement instructions to. |
---|
PDFNetException |
---|
Change the delimiters from '[' and ']' to arbitary strings.
Note: While empty strings are allowed as delimiters, a warning is displayed. Otherwise there are no restrictions. For example, after SetMatchStrings("{{", "}}"), AddString("TITLE", "Doctor") will replace any text consisting of "{{TITLE}}" with "Doctor". Similarly, after SetMatchStrings("Beginning...", "...ending."), AddString("TITLE", "Doctor") will replace "Beginning...TITLE...ending." with "Doctor".
start_str | - The starting delimiter string. |
---|---|
end_str | - The ending delimiter string. |
PDFNetException |
---|