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(); }
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); } }