Named destinations

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

6th November 2015
admin-marketing

Switch code for this example

RenameDestinations.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/20131610/renaming-named-destinations-in-pdf-files
  4.  *
  5.  * Searching for all the named destinations, with the purpose to rename them.
  6.  * Change the destination for all GoTo actions from Link annotations on the first page.
  7.  */
  8. package sandbox.annotations;
  9.  
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.pdf.PdfArray;
  12. import com.itextpdf.text.pdf.PdfDictionary;
  13. import com.itextpdf.text.pdf.PdfName;
  14. import com.itextpdf.text.pdf.PdfReader;
  15. import com.itextpdf.text.pdf.PdfStamper;
  16. import com.itextpdf.text.pdf.PdfString;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23.  
  24. import sandbox.WrapToTest;
  25.  
  26. @WrapToTest
  27. public class RenameDestinations {
  28.  
  29.     public static final String SRC = "resources/pdfs/nameddestinations.pdf";
  30.     public static final String DEST = "results/annotations/nameddests.pdf";
  31.    
  32.     public static void main(String[] args) throws IOException, DocumentException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         RenameDestinations app = new RenameDestinations();
  36.         app.manipulatePdf(SRC, DEST);
  37.     }
  38.    
  39.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  40.         PdfReader reader = new PdfReader(src);
  41.         Map renamed = new HashMap();
  42.         PdfDictionary catalog = reader.getCatalog();
  43.         PdfDictionary names = catalog.getAsDict(PdfName.NAMES);
  44.         PdfDictionary dests = names.getAsDict(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 = reader.getPageN(1);
  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.getAsDict(i);
  59.             action = annotation.getAsDict(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.         try {
  68.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  69.         stamper.close();
  70.         }
  71.         catch(Exception e) { e.printStackTrace(); }
  72.     }
  73. }
AddNamedDestinations.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/29884833/add-named-destinations-to-an-existing-pdf-document-with-itext
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfDestination;
  9. import com.itextpdf.text.pdf.PdfReader;
  10. import com.itextpdf.text.pdf.PdfStamper;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12. import com.itextpdf.text.pdf.SimpleNamedDestination;
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.List;
  19.  
  20. public class AddNamedDestinations {
  21.  
  22.     public static final String SRC = "resources/pdfs/primes.pdf";
  23.     public static final String DEST = "results/stamper/primes_with_destination.pdf";
  24.     public static final String XML = "results/stamper/primes_with_destination.xml";
  25.    
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         AddNamedDestinations app = new AddNamedDestinations();
  30.         app.manipulatePdf(SRC, DEST);
  31.         app.createXml(DEST, XML);
  32.     }
  33.  
  34.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  35.         PdfReader reader = new PdfReader(src);
  36.         int n = reader.getNumberOfPages();
  37.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  38.         PdfDestination destination = new PdfDestination(PdfDestination.FIT);
  39.         PdfWriter writer = stamper.getWriter();
  40.         for (int i = 1; i < n; ) {
  41.             if (getFactors(++i).size() > 1) continue;
  42.                 writer.addNamedDestination("prime" + i, i - 1, destination);
  43.         }
  44.         stamper.close();
  45.         reader.close();
  46.     }
  47.    
  48.     public static List getFactors(int n) {
  49.         List factors = new ArrayList();
  50.         for (int i = 2; i
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