Skip to main content
Skip table of contents

Named destinations

Examples in answer to questions such as:


renamedestinations

JAVA

JAVA
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/20131610/renaming-named-destinations-in-pdf-files
 * 
 * Searching for all the named destinations, with the purpose to rename them.
 * Change the destination for all GoTo actions from Link annotations on the first page.
 */
package sandbox.annotations;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfString;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import sandbox.WrapToTest;

@WrapToTest
public class RenameDestinations {

    public static final String SRC = "resources/pdfs/nameddestinations.pdf";
    public static final String DEST = "results/annotations/nameddests.pdf";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        RenameDestinations app = new RenameDestinations();
        app.manipulatePdf(SRC, DEST);
    }
    
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        Map<String, PdfString> renamed = new HashMap<String, PdfString>();
        PdfDictionary catalog = reader.getCatalog();
        PdfDictionary names = catalog.getAsDict(PdfName.NAMES);
        PdfDictionary dests = names.getAsDict(PdfName.DESTS);
        PdfArray name = dests.getAsArray(PdfName.NAMES);
        for (int i = 0; i < name.size(); i += 2) {
            PdfString original = name.getAsString(i);
            PdfString newName = new PdfString("new" + original.toString());
            name.set(i, newName);
            renamed.put(original.toString(), newName);
        }
        PdfDictionary page = reader.getPageN(1);
        PdfArray annotations = page.getAsArray(PdfName.ANNOTS);
        PdfDictionary annotation;
        PdfDictionary action;
        PdfString n;
        for (int i = 0; i < annotations.size(); i++) {
            annotation = annotations.getAsDict(i);
            action = annotation.getAsDict(PdfName.A);
            if (action == null)
                continue;
            n = action.getAsString(PdfName.D);
            if (n != null && renamed.containsKey(n.toString())) {
                action.put(PdfName.D, renamed.get(n.toString()));
            }
        }
        try {
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        stamper.close();
        }
        catch(Exception e) { e.printStackTrace(); }
    }
}


addnameddestinations

JAVA

JAVA
/*
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/29884833/add-named-destinations-to-an-existing-pdf-document-with-itext
 */
package sandbox.stamper;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfDestination;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.SimpleNamedDestination;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AddNamedDestinations {

    public static final String SRC = "resources/pdfs/primes.pdf";
    public static final String DEST = "results/stamper/primes_with_destination.pdf";
    public static final String XML = "results/stamper/primes_with_destination.xml";
   
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        AddNamedDestinations app = new AddNamedDestinations();
        app.manipulatePdf(SRC, DEST);
        app.createXml(DEST, XML);
    }

    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        int n = reader.getNumberOfPages();
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        PdfDestination destination = new PdfDestination(PdfDestination.FIT);
        PdfWriter writer = stamper.getWriter();
        for (int i = 1; i < n; ) {
            if (getFactors(++i).size() > 1) continue;
                writer.addNamedDestination("prime" + i, i - 1, destination);
        }
        stamper.close();
        reader.close();
    }
    
    public static List<Integer> getFactors(int n) {
        List<Integer> factors = new ArrayList<Integer>();
        for (int i = 2; i <= n; i++) {
            while (n % i == 0) {
                factors.add(i);
                n /= i;
            }
        }
        return factors;
    }
    
    public void createXml(String src, String dest) throws IOException {
        PdfReader reader = new PdfReader(src);
        HashMap<String,String> map = SimpleNamedDestination.getNamedDestination(reader, false);
        SimpleNamedDestination.exportToXML(map, new FileOutputStream(dest),
                "ISO8859-1", true);
        reader.close();
    }
}
JavaScript errors detected

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

If this problem persists, please contact our support.