How can I create custom annotations using PDFNet?

429 views
Skip to first unread message

Support

unread,
Feb 14, 2008, 3:53:02 PM2/14/08
to PDFTron PDFNet SDK
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,
it just
contains a phone number. I used a custom plugin to insert an invisible
link
into the file, on top of the phone number and the result is the
"After" file.
Notice how the cursor's shape changes when it hovers around the phone
number.

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
following snippet:

// Assuming PDFNet v4 API:
Obj my_annot = pdfdoc.CreateIndirectDict();
my_annot.PutName("Subtype", "Phone");
my_annot.PutName("Type", "Annot");
my_annot.PutRect("Rect", 266, 116, 430, 204);
my_annot.PutBool("MyEntry", "1-800-3934-3093");
...
page.AnnotPushBack(new Annot(my_annot));
// 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/
Pages/Kids/0/Annots/0

Support

unread,
Feb 14, 2008, 5:29:19 PM2/14/08
to PDFTron PDFNet SDK
Q: Thanks for your help! This is my scenario. A PDF file is created by
an OCR
package that generates PDF files with the text already under the
image.
Thus the number 1-800-555-1212 will already be present in the PDF file
before
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
the
phone number and the link. I used a plugin that I had custom written
to add
a link above the preexistent phone number. What I need is to replicate
the
functionality of that plugin programmatically, using PDFTron. I will
somehow
determine the coordinates of the phone number's enclosing rectangle.

--------
A: The last line should actually be

my_annot.PutString("MyEntry", "1-800-3934-3093");

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.

Reply all
Reply to author
Forward
0 new messages