Q: How can I create custom annotations using PDFNet?
I am considering migrating from my current PDF library to PDFTron. The
objective of my operation is to programmaticaly insert two kinds of
items into existent PDF files.
(1) Fillable fields (text, radio buttons, check marks, etc.)
(2) Custom Annotation Types, more specifically custom links.
I have been doing part (1) for a while, and hence the question is
about part (2). I would like to know what is the PDFTron procedure to
insert such links.
Consider the attached files. The "Before" file is extremely simple,
contains a phone number. I used a custom plugin to insert an invisible
into the file, on top of the phone number and the result is the
Notice how the cursor's shape changes when it hovers around the phone
Question: Exactly what would I have to do with PDFTron in order to
convert the "Before" file into the "After" file?
A: Using PDFNet you can create custom annotations.
To create a custom annotation called 'phone' you could use the
// Assuming PDFNet v4 API:
Obj my_annot = pdfdoc.CreateIndirectDict();
my_annot.PutRect("Rect", 266, 116, 430, 204);
// in C++ use page.AnnotPushBack(Annot(my_annot));
Using CosEdit (http://www.pdftron.com/cosedit) you can find the
relevant annotation dictionary using the following path: trailer/Root/
Q: Thanks for your help! This is my scenario. A PDF file is created by
package that generates PDF files with the text already under the
Thus the number 1-800-555-1212 will already be present in the PDF file
PDFTron does any processing to it. Notice that the first PDF file I
sent had the
phone number but no annotation (link) and the second PDF file contains
phone number and the link. I used a plugin that I had custom written
a link above the preexistent phone number. What I need is to replicate
functionality of that plugin programmatically, using PDFTron. I will
determine the coordinates of the phone number's enclosing rectangle.
A: The last line should actually be
and not PutBool() ... I added that line just to illustrate the idea
that you can add any number of custom entries (e.g. storing the actual
phone number, contact info, etc). You can simply delete the lines you
don't need or add additional entries, if required.
To implement your solution (I assume) you would also need to search
and recognize all phone numbers of the page (in order to determine the
location/rectangle where the custom annotation should be created). For
this purpose, you can use pdftron.PDF.TextExtractor class as
illustrated in TextExtract sample project (www.pdftron.com/net/
samplecode.html#TextExtract). Using TextExtractor you can also obtain
a bounding box for each word or line of a given PDF page and this
information can be used to create the custom 'phone' annotations.