Skip to main content
Skip table of contents

How to rename named destinations in existing PDF files?

I would like the named destinations in all the documents to be the same.

I've been using named destinations in PDF files to open the PDF file at a specific location in the file. The team responsible for generating the PDF document uses a tool to automatically generated named destinations from book marks, so the named destinations tend to have names like 9_Glossary or Additional_Information. We've been asked to produce the same documents in multiple languages. I expect the we will be supplied PDF documents in multiple foreign languages with bookmarks in the same locations, but the names of the book marks will of course be in these other languages, and the automatically generated named destinations will be in the foreign language. I would like the named destinations in all the documents to be the same.

I can't be the first person to run into this problem, so I'm interested to see if others have dealt with this.

One thought that comes to might might be to rename the destinations in the foreign language documents. I have used iTextSharp to extract a list of named destinations. Is it possible to iTextSharp to rename those destinations? Ideally, I'd have a tool that my translator could use to match the named destination names in each language, then have the tool replace the named destination names.

Another thought is to maintain a database where the translation is performed in real time; the translator still has to match of named destination names, but they are stored in a database. The program that orders Adobe Reader to open would use the English version to look up the foreign language name and then use that to open the document.

Posted on StackOverflow on Nov 21, 2013 by Stephen Lewis

Please take a look at the example RenameDestinations for iText 7. It's doing more than you need, because the original document itself contains links to the named destinations and for those to keep working, we need to change the action that refers to the names we've changed.

This is the part that is relevant to you:

JAVA
PdfNameTree nameTree = pdfDoc.getCatalog().getNameTree(PdfName.Dests);
Map < PdfString, PdfObject > names = nameTree.getNames();

// Loop over all named destinations and rename its string values with new names
for (PdfString key: nameTree.getKeys()) {
  String oldName = key.toUnicodeString();
  PdfString newName = new PdfString("new" + oldName);

  names.put(newName, names.get(key));
  names.remove(key);
  renamed.put(oldName, newName);
}

Unlike iText 5, instead of manually walking the PDF catalog dictionary to find /Names > /Dests > /Names, you access the named‑destination system through the PdfNameTree API (Java/.NET).

This gives you a PdfNameTree whose getNames() method returns a clean map of PdfString keys to destination objects, replacing the old /Names array of pairs used in iText 5. You iterate over the destination names via the tree’s getKeys() method, create new names as needed, update the map by adding the new key and removing the old one, and finally mark the tree as modified so iText will regenerate the name tree structure when the document is saved.

Click How to rename named destinations in existing PDF files? if you want to see how to answer this question in iText 5.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.