I need to replace certain text elements within a PDF file with new
content. Specifically, I will have a PDF file with placeholders in the
text e.g. $NAME and I want to replace them with actual values. The
process is similar to a mail merge. The PDF is for printing, so it is
important that the layering, colour models etc are preserved.
You can use PDFNet SDK (www.pdftron.com/net) to replace placeholders
on given page with new values.
There are couple of ways this can be implemented using PDFNet:
a) using AcroForms.
· You could use Acrobat (or some other PDF desktop software) to
create PDF templates that need to be filled out with variable text.
The fields can be created on top of any PDF document and you can
assign various properties (such as field name, border, etc).
· Once you have a PDF form, you can programmatically fill it out as
illustrated in the following samples:
- InteractiveForms: http://www.pdftron.com/net/samplecode.html#InteractiveForms
- FDF: http://www.pdftron.com/net/samplecode.html#FDF
· In case you would like to merge PDF forms to a static page, use
pdfdoc.FlattenAnnotations(true). For more information on ‘form flattening’
feature, please see: http://www.pdftron.com/net/faq.html#frm_01.
b) by editing static page content.
· In this case you would locate the replacement text by reading
‘PDF.Elements’ from the page using ElementReader class (for an
example, please see http://www.pdftron.com/net/samplecode.html#TextExtract).
· Once you find the text you can replace it with new content as
illustrated in ElementEdit sample (http://www.pdftron.com/net/
samplecode.html#ElementEdit). This sample shows how to replace text
color, but you can use the same approach to replace text content, or
even to replace the text with non-textual information (e.g. a new