public class ContentReplacer
extends java.lang.Object
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);
Constructor and Description |
---|
ContentReplacer()
Create a new ContentReplacer object, to which replacement rules will be added.
|
Modifier and Type | Method and Description |
---|---|
void |
addImage(Rect target_region,
Obj replacement_image)
Replace the image that best fits into 'target_region' with 'replacement_image'.
|
void |
addString(java.lang.String template_text,
java.lang.String replacement_text)
Any text of the form "[template_text]" will be replaced by "replacement_text".
|
void |
addText(Rect target_region,
java.lang.String replacement_text)
All text inside 'target_region' will be deleted and replaced with 'replacement_text'.
|
void |
destroy()
Frees the native memory of the object.
|
void |
process(Page page)
Apply the replacement instructions to the target page.
|
void |
setMatchStrings(java.lang.String start_str,
java.lang.String end_str)
Change the delimiters from '[' and ']' to arbitary strings.
|
public ContentReplacer() throws PDFNetException
PDFNetException
public void addImage(Rect target_region, Obj replacement_image) throws PDFNetException
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
public void addText(Rect target_region, java.lang.String replacement_text) throws PDFNetException
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
public void addString(java.lang.String template_text, java.lang.String replacement_text) throws PDFNetException
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
public void setMatchStrings(java.lang.String start_str, java.lang.String end_str) throws PDFNetException
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
public void process(Page page) throws PDFNetException
page
- - The page to apply the content replacement instructions to.PDFNetException
public void destroy()