iText pdf library
Website search

Creating and adding annotations

AddLinkImages.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to
 * http://stackoverflow.com/questions/29388313/itext-how-to-associate-actions-with-graphical-object
 */
package com.itextpdf.samples.sandbox.annotations;
 
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.kernel.pdf.canvas.wmf.WmfImageData;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.Property;
 
import java.io.File;
import java.net.MalformedURLException;
 
public class AddLinkImages {
    public static final String DEST = "./target/sandbox/annotations/add_link_images.pdf";
 
    public static final String sourceFolder = "./src/test/resources/img/";
    public static final String BUTTERFLY = sourceFolder + "butterfly.wmf";
    public static final String DOG = sourceFolder + "dog.bmp";
    public static final String FOX = sourceFolder + "fox.bmp";
    public static final String INFO = sourceFolder + "info.png";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new AddLinkImages().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        Paragraph p = new Paragraph("Objects with links");
        p.add(createImage(INFO, "https://itextpdf.com/"));
        p.add(createImage(DOG, "https://pages.itextpdf.com/ebook-stackoverflow-questions.html"));
        p.add(createImage(FOX, "https://stackoverflow.com/q/29388313/1622493"));
 
        // Create PdfFormXObject object to add .wmf format image to the document,
        // because the creation of an ImageData instance from .wmf format image isn't supported.
        PdfFormXObject wmfImage = new PdfFormXObject(new WmfImageData(BUTTERFLY), pdfDoc);
        p.add(new Image(wmfImage)
                .setAction(PdfAction.createURI("https://stackoverflow.com/questions/tagged/itext*")));
        doc.add(p);
 
        doc.close();
    }
 
    public Image createImage(String src, String url) throws MalformedURLException {
        Image img = new Image(ImageDataFactory.create(src));
 
        // Create the url in the image by setting action property directly
        img.setProperty(Property.ACTION, PdfAction.createURI(url));
        return img;
    }
}
AddPointerAnnotation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/26752663/adding-maps-at-itext-java
 */
package com.itextpdf.samples.sandbox.annotations;
 
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfDictionary;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfNumber;
import com.itextpdf.kernel.pdf.PdfArray;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfLineAnnotation;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
 
import java.io.File;
 
public class AddPointerAnnotation {
    public static final String DEST = "./target/sandbox/annotations/add_pointer_annotation.pdf";
 
    public static final String IMG = "./src/test/resources/img/map_cic.png";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new AddPointerAnnotation().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Image img = new Image(ImageDataFactory.create(IMG));
        Document doc = new Document(pdfDoc, new PageSize(img.getImageWidth(), img.getImageHeight()));
        img.setFixedPosition(0, 0);
        doc.add(img);
 
        Rectangle rect = new Rectangle(220, 350, 255, 245);
        PdfLineAnnotation lineAnnotation = new PdfLineAnnotation(rect,
                new float[] {220 + 5, 350 + 5, 220 + 255 - 5, 350 + 245 - 5});
        lineAnnotation.setTitle(new PdfString("You are here:"));
 
        // This method sets the text that will be displayed for the annotation or the alternate description,
        // if this type of annotation does not display text.
        lineAnnotation.setContents("Cambridge Innovation Center");
        lineAnnotation.setColor(ColorConstants.RED);
 
        // Set to print the annotation when the page is printed
        lineAnnotation.setFlag(PdfAnnotation.PRINT);
 
        // Set arrow's border style
        PdfDictionary borderStyle = new PdfDictionary();
        borderStyle.put(PdfName.S, PdfName.S);
        borderStyle.put(PdfName.W, new PdfNumber(5));
        lineAnnotation.setBorderStyle(borderStyle);
 
        PdfArray le = new PdfArray();
        le.add(PdfName.OpenArrow);
        le.add(PdfName.None);
        lineAnnotation.put(PdfName.LE, le);
        lineAnnotation.put(PdfName.IT, PdfName.LineArrow);
 
        pdfDoc.getFirstPage().addAnnotation(lineAnnotation);
 
        doc.close();
    }
}
AddRotatedAnnotation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/27083206/itextshape-clickable-polygon-or-path
 */
package com.itextpdf.samples.sandbox.annotations;
 
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfNumber;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfStampAnnotation;
 
import java.io.File;
 
public class AddRotatedAnnotation {
    public static final String DEST = "./target/sandbox/annotations/add_rotated_annotation.pdf";
 
    public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new AddRotatedAnnotation().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
        PdfPage firstPage = pdfDoc.getFirstPage();
        PdfAction linkAction = PdfAction.createURI("https://pages.itextpdf.com/ebook-stackoverflow-questions.html");
 
        Rectangle annotLocation = new Rectangle(30, 770, 90, 30);
        PdfAnnotation link = new PdfLinkAnnotation(annotLocation)
 
                // Set highlighting type which is enabled after a click on the annotation
                .setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT)
                .setAction(linkAction)
                .setColor(ColorConstants.RED.getColorValue());
        firstPage.addAnnotation(link);
 
        annotLocation = new Rectangle(30, 670, 30, 90);
        link = new PdfLinkAnnotation(annotLocation)
                .setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT)
                .setAction(linkAction)
                .setColor(ColorConstants.GREEN.getColorValue());
        firstPage.addAnnotation(link);
 
        annotLocation = new Rectangle(150, 770, 90, 30);
        PdfAnnotation stamp = new PdfStampAnnotation(annotLocation)
                .setStampName(new PdfName("Confidential"))
 
                // This method sets the text that will be displayed for the annotation or the alternate description,
                // if this type of annotation does not display text.
                .setContents("Landscape");
        firstPage.addAnnotation(stamp);
 
        annotLocation = new Rectangle(150, 670, 90, 90);
        stamp = new PdfStampAnnotation(annotLocation)
                .setStampName(new PdfName("Confidential"))
                .setContents("Portrait")
                .put(PdfName.Rotate, new PdfNumber(90));
        firstPage.addAnnotation(stamp);
 
        annotLocation = new Rectangle(250, 670, 90, 90);
        stamp = new PdfStampAnnotation(annotLocation)
                .setStampName(new PdfName("Confidential"))
                .setContents("Portrait")
                .put(PdfName.Rotate, new PdfNumber(45));
        firstPage.addAnnotation(stamp);
 
        pdfDoc.close();
    }
}
AddStamp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to
 * http://stackoverflow.com/questions/29229629/how-to-add-a-printable-or-non-printable-bitmap-stamp-to-a-pdf
 */
package com.itextpdf.samples.sandbox.annotations;
 
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfStampAnnotation;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
 
import java.io.File;
 
public class AddStamp {
    public static final String DEST = "./target/sandbox/annotations/add_stamp.pdf";
 
    public static final String IMG = "./src/test/resources/img/itext.png";
    public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new AddStamp().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
        ImageData img = ImageDataFactory.create(IMG);
        float width = img.getWidth();
        float height = img.getHeight();
        PdfFormXObject xObj = new PdfFormXObject(new Rectangle(width, height));
 
        PdfCanvas canvas = new PdfCanvas(xObj, pdfDoc);
        canvas.addImage(img, 0, 0, false);
 
        Rectangle location = new Rectangle(36, 770 - height, width, height);
        PdfStampAnnotation stamp = new PdfStampAnnotation(location);
        stamp.setStampName(new PdfName("ITEXT"));
        stamp.setNormalAppearance(xObj.getPdfObject());
 
        // Set to print the annotation when the page is printed
        stamp.setFlags(PdfAnnotation.PRINT);
        pdfDoc.getFirstPage().addAnnotation(stamp);
 
        pdfDoc.close();
    }
}
FileAttachmentAnnot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/31006683/itext-clickable-image-should-open-ms-word-attachment
 */
package com.itextpdf.samples.sandbox.annotations;
 
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfFileAttachmentAnnotation;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.filespec.PdfFileSpec;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
 
import java.io.File;
 
public class FileAttachmentAnnot {
    public static final String DEST = "./target/sandbox/annotations/file_attachment_annot.pdf";
 
    public static final String IMG = "./src/test/resources/img/info.png";
    public static final String PATH = "./src/test/resources/txt/test.docx";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new FileAttachmentAnnot().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
 
        Rectangle rect = new Rectangle(36, 700, 100, 100);
        String embeddedFileName = "test.docx";
 
        // the 3rd argument is the file description.
        // the 5th argument is the mime-type of the embedded file;
        // the 6th argument is the AFRelationship key value.
        PdfFileSpec fileSpec = PdfFileSpec.createEmbeddedFileSpec(pdfDoc, PATH, null, embeddedFileName, null, null);
        PdfAnnotation attachment = new PdfFileAttachmentAnnotation(rect, fileSpec);
 
        // This method sets the text that will be displayed for the annotation or the alternate description,
        // if this type of annotation does not display text.
        attachment.setContents("Click me");
 
        // Create XObject and draw it with the imported image on the canvas
        // to add XObject as normal appearance.
        PdfFormXObject xObject = new PdfFormXObject(rect);
        ImageData imageData = ImageDataFactory.create(IMG);
        PdfCanvas canvas = new PdfCanvas(xObject, pdfDoc);
        canvas.addImage(imageData, rect, true);
        attachment.setNormalAppearance(xObject.getPdfObject());
 
        pdfDoc.addNewPage().addAnnotation(attachment);
 
        pdfDoc.close();
    }
}
ImagesLinksTable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/32839816/export-hyperlink-to-pdf-file-from-itextsharp-library-in-c-sharp
 */
package com.itextpdf.samples.sandbox.annotations;
 
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.Property;
import com.itextpdf.layout.property.UnitValue;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;
import com.itextpdf.layout.renderer.ImageRenderer;
 
import java.io.File;
 
public class ImagesLinksTable {
    public static final String DEST = "./target/sandbox/annotations/images_links_table.pdf";
 
    public static final String IMG = "./src/test/resources/img/info.png";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new ImagesLinksTable().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        Image img = new Image(ImageDataFactory.create(IMG));
        Paragraph anchor = new Paragraph().add(img);
        anchor.setProperty(Property.ACTION, PdfAction.createURI("https://lowagie.com/"));
 
        Table table = new Table(UnitValue.createPercentArray(3)).useAllAvailableWidth();
        table.addCell(anchor);
        table.addCell("A");
        table.addCell("B");
        table.addCell("C");
 
        img = new Image(ImageDataFactory.create(IMG));
        img.setNextRenderer(new LinkImageRenderer(img));
        table.addCell(img);
 
        doc.add(table);
 
        doc.close();
    }
 
    protected class LinkImageRenderer extends ImageRenderer {
        public LinkImageRenderer(Image image) {
            super(image);
        }
 
        @Override
        public IRenderer getNextRenderer() {
            return new LinkImageRenderer((Image) modelElement);
        }
 
        @Override
        public void draw(DrawContext drawContext) {
            super.draw(drawContext);
            PdfAnnotation annotation = new PdfLinkAnnotation(getOccupiedAreaBBox())
                    .setAction(PdfAction.createURI("https://lowagie.com/bio"));
            drawContext.getDocument().getLastPage().addAnnotation(annotation);
        }
    }
}
RelativeLink.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/27063677/use-of-relative-path-for-anchor-method-using-itext-for-pdf-generation
 */
package com.itextpdf.samples.sandbox.annotations;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Link;
import com.itextpdf.layout.element.Paragraph;
 
import java.io.File;
 
public class RelativeLink {
    public static final String DEST = "./target/sandbox/annotations/relative_link.pdf";
 
    public static final String XML = "../../../src/test/resources/xml/data.xml";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new RelativeLink().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        Paragraph chunk = new Paragraph(new Link("Click me", PdfAction.createURI(XML)));
        doc.add(chunk);
 
        doc.close();
    }
}
RemoteGoToPage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/19999048/how-to-create-hyperlink-from-a-pdf-to-another-pdf-to-a-specified-page-using-itex
 * <p>
 * Creating a link from one PDF to another
 */
package com.itextpdf.samples.sandbox.annotations;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Link;
import com.itextpdf.layout.element.Paragraph;
 
import java.io.File;
 
public class RemoteGoToPage {
    public static final String DEST = "./target/sandbox/annotations/";
 
    public static final String[] DEST_NAMES = {
            "remote_go_to_page.pdf",
            "subdir/xyz2.pdf",
    };
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST + "subdir/");
        file.mkdirs();
 
        new RemoteGoToPage().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        createLinkPdf(dest + DEST_NAMES[0]);
        createDestinationPdf(dest + DEST_NAMES[1]);
    }
 
    // This method creates a link destination pdf file.
    private void createDestinationPdf(String src) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(src));
        Document doc = new Document(pdfDoc);
 
        doc.add(new Paragraph("page 1"));
        for (int i = 2; i < 8; i++) {
            doc.add(new AreaBreak());
            doc.add(new Paragraph("page " + i));
        }
 
        doc.close();
    }
 
    // This method creates a pdf file, which will contain a link
    // to the sixth page of another pdf file.
    private static void createLinkPdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        // Create a link action, which leads to the another pdf file's page.
        // The 1st argument is the relative destination pdf file's path;
        // the 2nd argument is the number of the page (in the destination pdf file),
        // to which the link will lead after a click on it.
        PdfAction action = PdfAction.createGoToR(DEST_NAMES[1], 6);
        Paragraph chunk = new Paragraph(new Link("Link", action));
        doc.add(chunk);
 
        doc.close();
    }
}
RemoteGoto.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/19999048/how-to-create-hyperlink-from-a-pdf-to-another-pdf-to-a-specified-page-using-itex
 * <p>
 * Creating a link from one PDF to another
 */
package com.itextpdf.samples.sandbox.annotations;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Link;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.Property;
 
import java.io.File;
 
public class RemoteGoto {
    public static final String DEST = "./target/sandbox/annotations/";
 
    public static final String[] DEST_NAMES = {
            "remote_goto.pdf",
            "subdir/xyz.pdf"
    };
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST + "subdir/");
        file.mkdirs();
 
        new RemoteGoto().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        createLinkPdf(dest + DEST_NAMES[0]);
        createDestinationPdf(dest + DEST_NAMES[1]);
    }
 
    // This method creates a link destination pdf file.
    private void createDestinationPdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        Paragraph anchor = new Paragraph("This is a destination");
 
        // Set string destination, to which the created in the another pdf file link will lead.
        anchor.setProperty(Property.DESTINATION, "dest");
        doc.add(anchor);
 
        doc.close();
    }
 
    // This method creates a pdf file, which will contain a link
    // to the page with set string destination of another pdf file.
    private static void createLinkPdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        // Create a link action, which leads to the another pdf file's page.
        // The 1st argument is the relative destination pdf file's path;
        // the 2nd argument is the string destination in the destination pdf file,
        // to which the link will lead after a click on it.
        PdfAction action = PdfAction.createGoToR(DEST_NAMES[1], "dest");
        Paragraph chunk = new Paragraph(new Link("Link", action));
        doc.add(chunk);
 
        doc.close();
    }
}

Click the following link to see the legacy example for iText 5. Except for security fixes, iText 5 is no longer being developed.



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