Adding links to existing documents

Tags: clickable imageimageslink annotationannotationspolygonquadpoints
Files: 
/**
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/26983703/itext-how-to-stamp-image-on-existing-pdf-and-create-an-anchor
 */
package sandbox.annotations;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfBorderArray;
import com.itextpdf.text.pdf.PdfDestination;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class AddImageLink {
 
    public static final String SRC = "resources/pdfs/primes.pdf";
    public static final String IMG = "resources/images/info.png";
    public static final String DEST = "results/annotations/link_image.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new AddImageLink().manipulatePdf(SRC, DEST);
    }
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        Image img = Image.getInstance(IMG);
        float x = 10;
        float y = 650;
        float w = img.getScaledWidth();
        float h = img.getScaledHeight();
        img.setAbsolutePosition(x, y);
        stamper.getOverContent(1).addImage(img);
        Rectangle linkLocation = new Rectangle(x, y, x + w, y + h);
        PdfDestination destination = new PdfDestination(PdfDestination.FIT);
        PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
                linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,
                reader.getNumberOfPages(), destination);
        link.setBorder(new PdfBorderArray(0, 0, 0));
        stamper.addAnnotation(link, 1);
        stamper.close();
    }
}
/*
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/27083206/itextshape-clickable-polygon-or-path
 */
package sandbox.annotations;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfDestination;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class AddPolygonLink {
 
    public static final String SRC = "resources/pdfs/hello.pdf";
    public static final String DEST = "results/annotations/link_polygon.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new AddPolygonLink().manipulatePdf(SRC, DEST);;
    }
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        PdfContentByte canvas = stamper.getOverContent(1);
        canvas.moveTo(36, 700);
        canvas.lineTo(72, 760);
        canvas.lineTo(144, 720);
        canvas.lineTo(72, 730);
        canvas.closePathStroke();
        Rectangle linkLocation = new Rectangle(36, 700, 144, 760);
        PdfArray array = new PdfArray(new int[]{72, 730, 144, 720, 72, 760, 36, 700});
        PdfDestination destination = new PdfDestination(PdfDestination.FIT);
        PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
                linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,
                1, destination);
        link.put(PdfName.QUADPOINTS, array);
        stamper.addAnnotation(link, 1);
        stamper.close();
    }
}
package sandbox.annotations;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfDestination;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class AddLinkAnnotation {
 
    public static final String SRC = "resources/pdfs/primes.pdf";
    public static final String DEST = "results/annotations/link_annotation.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new AddLinkAnnotation().manipulatePdf(SRC, DEST);
    }
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        Rectangle linkLocation = new Rectangle(523, 770, 559, 806);
        PdfDestination destination = new PdfDestination(PdfDestination.FIT);
        PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
                linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,
                3, destination);
        stamper.addAnnotation(link, 1);
        stamper.close();
        reader.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to
 * http://stackoverflow.com/questions/28554413/how-to-add-overlay-text-with-link-annotations-to-existing-pdf
 */
package sandbox.annotations;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class AddLinkAnnotation2 {
 
    public static final String SRC = "resources/pdfs/hello.pdf";
    public static final String DEST = "results/annotations/link_annotation2.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new AddLinkAnnotation2().manipulatePdf(SRC, DEST);
    }
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        PdfContentByte canvas = stamper.getOverContent(1);
        Font bold = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
        Chunk chunk = new Chunk("The Best iText Questions on StackOverflow", bold);
        chunk.setAnchor("http://pages.itextpdf.com/ebook-stackoverflow-questions.html");
        Phrase p = new Phrase("Download ");
        p.add(chunk);
        p.add(" and discover more than 200 questions and answers.");
        ColumnText ct = new ColumnText(canvas);
        ct.setSimpleColumn(36, 700, 559, 750);
        ct.addText(p);
        ct.go();
        stamper.close();
        reader.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to
 * http://stackoverflow.com/questions/28554413/how-to-add-overlay-text-with-link-annotations-to-existing-pdf
 */
package sandbox.annotations;
 
import com.itextpdf.awt.geom.AffineTransform;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class AddLinkAnnotation3 {
 
    public class AddAnnotation extends PdfPageEventHelper {
        protected PdfStamper stamper;
        protected AffineTransform transform;
 
        public AddAnnotation(PdfStamper stamper, AffineTransform transform) {
            this.stamper = stamper;
            this.transform = transform;
        }
 
        @Override
        public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
            float[] pts = {rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()};
            transform.transform(pts, 0, pts, 0, 2);
            float[] dstPts = {pts[0], pts[1], pts[2], pts[3]};
            rect = new Rectangle(dstPts[0], dstPts[1], dstPts[2], dstPts[3]);
            PdfAnnotation annot = PdfAnnotation.createLink(writer, rect, PdfAnnotation.HIGHLIGHT_INVERT, new PdfAction(text));
            stamper.addAnnotation(annot, 1);
        }
 
    }
 
    public static final String SRC = "resources/pdfs/hello.pdf";
    public static final String DEST = "results/annotations/link_annotation3.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new AddLinkAnnotation3().manipulatePdf(SRC, DEST);
    }
 
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        AffineTransform transform = AffineTransform.getRotateInstance(Math.PI / 6);
        stamper.getWriter().setPageEvent(new AddAnnotation(stamper, transform));
        PdfContentByte canvas = stamper.getOverContent(1);
        Font bold = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
        Chunk chunk = new Chunk("The Best iText Questions on StackOverflow", bold);
        chunk.setGenericTag("http://pages.itextpdf.com/ebook-stackoverflow-questions.html");
        Phrase p = new Phrase("Download ");
        p.add(chunk);
        p.add(" and discover more than 200 questions and answers.");
        canvas.saveState();
        canvas.concatCTM(transform);
        ColumnText ct = new ColumnText(canvas);
        ct.setSimpleColumn(300, 0, 800, 400);
        ct.addText(p);
        ct.go();
        canvas.restoreState();
        stamper.close();
        reader.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to
 * http://stackoverflow.com/questions/28554413/how-to-add-overlay-text-with-link-annotations-to-existing-pdf
 */
package sandbox.annotations;
 
import com.itextpdf.awt.geom.AffineTransform;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfBorderArray;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class AddLinkAnnotation4 {
 
    public class AddAnnotation extends PdfPageEventHelper {
        protected PdfStamper stamper;
        protected AffineTransform transform;
 
        public AddAnnotation(PdfStamper stamper, AffineTransform transform) {
            this.stamper = stamper;
            this.transform = transform;
        }
 
        @Override
        public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
            float[] pts = {rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()};
            transform.transform(pts, 0, pts, 0, 2);
            float[] dstPts = {pts[0], pts[1], pts[2], pts[3]};
            rect = new Rectangle(dstPts[0], dstPts[1], dstPts[2], dstPts[3]);
            PdfAnnotation annot = PdfAnnotation.createLink(writer, rect, PdfAnnotation.HIGHLIGHT_INVERT, new PdfAction(text));
            annot.setBorder(new PdfBorderArray(0, 0, 0));
            stamper.addAnnotation(annot, 1);
        }
 
    }
 
    public static final String SRC = "resources/pdfs/hello.pdf";
    public static final String DEST = "results/annotations/link_annotation4.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new AddLinkAnnotation4().manipulatePdf(SRC, DEST);
    }
 
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        AffineTransform transform = AffineTransform.getRotateInstance(Math.PI / 2);
        stamper.getWriter().setPageEvent(new AddAnnotation(stamper, transform));
        PdfContentByte canvas = stamper.getOverContent(1);
        Font bold = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
        Chunk chunk = new Chunk("The Best iText Questions on StackOverflow", bold);
        chunk.setGenericTag("http://pages.itextpdf.com/ebook-stackoverflow-questions.html");
        Phrase p = new Phrase("Download ");
        p.add(chunk);
        p.add(" and discover more than 200 questions and answers.");
        canvas.saveState();
        canvas.concatCTM(transform);
        ColumnText ct = new ColumnText(canvas);
        ct.setSimpleColumn(36, -559, 806, -36);
        ct.addText(p);
        ct.go();
        canvas.restoreState();
        stamper.close();
        reader.close();
    }
}
/*
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/31240828/how-navigate-one-page-to-another-page-in-pdf-file-by-using-itextsharp-c
 */
package sandbox.stamper;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfDestination;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 * @author iText
 */
@WrapToTest
public class AddNavigation {
 
    public static final String SRC = "resources/pdfs/primes.pdf";
    public static final String DEST = "results/stamper/primes_links.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new AddNavigation().manipulatePdf(SRC, DEST);
    }
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        PdfDestination d1 = new PdfDestination(PdfDestination.FIT);
        Rectangle rect = new Rectangle(0, 806, 595, 842);
        PdfAnnotation a10 = PdfAnnotation.createLink(stamper.getWriter(), rect, PdfAnnotation.HIGHLIGHT_INVERT, 10, d1);
        stamper.addAnnotation(a10, 1);
        PdfDestination d2 = new PdfDestination(PdfDestination.FIT);
        PdfAnnotation a1 = PdfAnnotation.createLink(stamper.getWriter(), rect, PdfAnnotation.HIGHLIGHT_PUSH, 1, d2);
        stamper.addAnnotation(a1, 10);
        stamper.close();
    }
}
package sandbox.annotations;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfBorderArray;
import com.itextpdf.text.pdf.PdfDestination;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class AddLinkAnnotation5 {
 
    public static final String SRC = "resources/pdfs/primes.pdf";
    public static final String DEST = "results/annotations/link_annotation5.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new AddLinkAnnotation5().manipulatePdf(SRC, DEST);
    }
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        // Here we define the location:
        Rectangle linkLocation = new Rectangle(320, 695, 560, 741);
        // here we add the actual content at this location:
        ColumnText ct = new ColumnText(stamper.getOverContent(1));
        ct.setSimpleColumn(linkLocation);
        ct.addElement(new Paragraph("This is a link. Click it and you'll be forwarded to another page in this document."));
        ct.go();
        // now we create the link that will jump to a specific destination:
        PdfDestination destination = new PdfDestination(PdfDestination.FIT);
        PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
                linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,
                3, destination);
        // If you don't want a border, here's where you remove it:
        link.setBorder(new PdfBorderArray(0, 0, 0));
        // We add the link (that is the clickable area, not the text!)
        stamper.addAnnotation(link, 1);
        stamper.close();
        reader.close();
    }
}
Resources: 
File nameRaw URLUpdated
primes.pdfprimes.pdf2015-10-25 3:10 am
info.pnginfo.png2015-10-25 3:10 am
hello.pdfhello.pdf2015-10-11 3:52 pm