Editing Hyperlink and Anchors in PDF using ITextSharp

What you want is quite possible. What you want will require you to work with the low-level PDF objects (PdfDictionary, PdfArray, etc).

And whenever someone needs to work with those objects, I always refer them to the PDF Reference. In your case, you’ll want to examine chapter 7 (particularly section 3) and chapter 12, sections 3 (doc-level navigation) and 5 (annotations).

Assuming you’ve read that, here’s what you need to do:

  1. Step through the annotation array of each page (in the original doc, before breaking it up).
    1. Find all the link annotations & their destinations.
    2. Build a new destination for that link corresponding to the new file.
    3. write that new destination into the link annotation.
  2. Write this page into a new PDF using PdfCopy (it’ll copy the annotations as well as the page content).

Step 1.1 isn’t simple. There are several different kinds of “local goto” annotation formats. You need to determine which page a given link points to. Some links might say the PDF equivalent of “next page” or “previous page”, while others will include a reference to a particular page. This will be an “indirect object reference”, not a page number.

To determine the page number from a page reference, you need to… ouch. Okay. The most efficient way would be to call PdfReader.GetPageRef(int pageNum) for each page in the original document and cache it in a map (reference->pageNum).

You can then build “remote goto” links by creating a remote goto PdfAction, and writing that into the link annotation’s “A” (action) entry, removing anything that was there before (probably a “Dest”).

I don’t speak C# very well, so I’ll leave the actual implementation to you.

Leave a Comment