Search code examples
hyperlinkitextgoto

My iText RemoteGoto doesn't jump to the correct destination


In my iText document I'm writing some text about my entity which shall reference to an occurence in another PDF document. There are multiple ways of doing this with iText:

static String externalLinkPrefix = "external_entity_";
String externalFile = "anotherPdf.pdf";
Chunk entity = new Chunk("name");
// use the plain remoteGoto() method
chunk.setRemoteGoto(externalFile, externalLinkPrefix + entity.getIdentifier());
// or use the PdfAction
chunk.setAction(PdfAction.gotoRemotePage(externalFile, externalLinkPrefix + entity.getIdentifier(), false, true);

// later on create the destination in the other file
Paragraph entityReference = new Paragraph(new Chunk("name")
     .setLocalDestination(externalLinkPrefix + entity.getIdentifier());

However when I click the created link it takes me to the beginning of the second document signalling that it did not find the destination.

I double-checked wheter the link 'keys' are the same and they are.

What does iText or the PDF Reader hinder finding the destination?


Solution

  • There are two ways to describe a named destination in PDF:

    1. You define the destination using a PDF string (preferred since PDF 1.2),
    2. You define the destination using a PDF name (original in PDF 1.1).

    In your case, create a Names tree that stores the destination as a PDF string:

    enter image description here

    Do you see foo_section_2? That's clearly a PDF string. You are using the preferred way to define a named destination because setLocalDestination() is programmed that way in iText.

    However, you use a PdfAction.gotoRemotePage() method that isn't complete. Based on your PDF, I assume that you create a reference to a named destination using a PDF name (/foo_section_2):

    enter image description here

    You can see that /foo_section_2 is a name because it starts with a /. There is no named destination that is defined using a name in the target file, hence the destination can't be found.

    You should use the gotoRemotePage() method like this:

    PdfAction.gotoRemotePage(externalFile, externalLinkPrefix + entity.getIdentifier(),
        false, true)
    

    The third parameter of the gotoRemotePage() method should be false to make sure that you create a link to a named destination that uses a PDF string instead of a PDF name (which is probably what you're doing).

    For some reason, Acrobat thinks that grunddaten doesn't have any named destinations:

    enter image description here

    This is confirmed when I look at the Navigations Tab, but when I try to add a named destination, I get "There was a problem reading this document (15)." I have no clue as to what 15 refers to.

    enter image description here

    Running Preflight, I get more info:

    enter image description here

    This tells me that the NameTreeRoot isn't constructed correctly. I'll have to investigate what's wrong. It works when I create a document with named destinations, see RemoteGoto. I've just tested it and ran Preflight and it works perfectly. I have no idea what goes wrong in your file...