Named destinations

This is a code example of iText PDF, discover more.

31st May 2016
iText PDF

Switch code for this example

RenameDestinations.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/20131610/renaming-named-destinations-in-pdf-files
  11.  *
  12.  * Searching for all the named destinations, with the purpose to rename them.
  13.  * Change the destination for all GoTo actions from Link annotations on the first page.
  14.  */
  15. package com.itextpdf.samples.sandbox.annotations;
  16.  
  17. import com.itextpdf.kernel.pdf.*;
  18. import com.itextpdf.samples.GenericTest;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20.  
  21. import org.junit.experimental.categories.Category;
  22.  
  23. import java.io.File;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26.  
  27. @Category(SampleTest.class)
  28. public class RenameDestinations extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/annotations/rename_destinations.pdf";
  30.     public static final String SRC = "./src/test/resources/pdfs/nameddestinations.pdf";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new RenameDestinations().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  41.         Map renamed = new HashMap();
  42.         PdfDictionary catalog = pdfDoc.getCatalog().getPdfObject();
  43.         PdfDictionary names = catalog.getAsDictionary(PdfName.Names);
  44.         PdfDictionary dests = names.getAsDictionary(PdfName.Dests);
  45.         PdfArray name = dests.getAsArray(PdfName.Names);
  46.         for (int i = 0; i < name.size(); i += 2) {
  47.             PdfString original = name.getAsString(i);
  48.             PdfString newName = new PdfString("new" + original.toString());
  49.             name.set(i, newName);
  50.             renamed.put(original.toString(), newName);
  51.         }
  52.         PdfDictionary page = pdfDoc.getPage(1).getPdfObject();
  53.         PdfArray annotations = page.getAsArray(PdfName.Annots);
  54.         PdfDictionary annotation;
  55.         PdfDictionary action;
  56.         PdfString n;
  57.         for (int i = 0; i < annotations.size(); i++) {
  58.             annotation = annotations.getAsDictionary(i);
  59.             action = annotation.getAsDictionary(PdfName.A);
  60.             if (action == null)
  61.                 continue;
  62.             n = action.getAsString(PdfName.D);
  63.             if (n != null && renamed.containsKey(n.toString())) {
  64.                 action.put(PdfName.D, renamed.get(n.toString()));
  65.             }
  66.         }
  67.         pdfDoc.close();
  68.     }
  69. }
AddNamedDestinations.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/29884833/add-named-destinations-to-an-existing-pdf-document-with-itext
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.kernel.pdf.*;
  15. import com.itextpdf.samples.GenericTest;
  16. import com.itextpdf.test.annotations.type.SampleTest;
  17.  
  18. import org.junit.experimental.categories.Category;
  19. import org.w3c.dom.Element;
  20.  
  21. import javax.xml.parsers.DocumentBuilder;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import javax.xml.parsers.ParserConfigurationException;
  24. import javax.xml.transform.Transformer;
  25. import javax.xml.transform.TransformerException;
  26. import javax.xml.transform.TransformerFactory;
  27. import javax.xml.transform.dom.DOMSource;
  28. import javax.xml.transform.stream.StreamResult;
  29. import java.io.File;
  30. import java.io.IOException;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import java.util.Map;
  34.  
  35. @Category(SampleTest.class)
  36. public class AddNamedDestinations extends GenericTest {
  37.     public static final String DEST
  38.             = "./target/test/resources/sandbox/stamper/add_named_destinations.pdf";
  39.     public static final String SRC
  40.             = "./src/test/resources/pdfs/primes.pdf";
  41.     public static final String XML
  42.             = "./target/test/resources/sandbox/stamper/primes_with_destination.xml";
  43.  
  44.     public static void main(String[] args) throws Exception {
  45.         File file = new File(DEST);
  46.         file.getParentFile().mkdirs();
  47.         new AddNamedDestinations().manipulatePdf(DEST);
  48.     }
  49.  
  50.     public static List getFactors(int n) {
  51.         List factors = new ArrayList();
  52.         for (int i = 2; i  1) {
  53.                 continue;
  54.             }
  55.             PdfArray array = new PdfArray();
  56.             array.add(pdfDoc.getPage(i).getPdfObject());
  57.             array.add(PdfName.XYZ);
  58.             array.add(new PdfNumber(pdfDoc.getPage(i).getPageSize().getLeft()));
  59.             array.add(new PdfNumber(pdfDoc.getPage(i).getPageSize().getTop()));
  60.             array.add(new PdfNumber(1));
  61.             // Notice that the document has already destinations like "Prime+i"
  62.             pdfDoc.addNamedDestination("prime" + i, array);
  63.         }
  64.         pdfDoc.close();
  65.  
  66.         createXml(DEST, XML);
  67.     }
  68. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now