Creating and adding annotations
These examples were written in the context of questions such as:
remotegoto
JAVA
JAVA
/**
* 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
*
* Creating a link from one PDF to another
*/
package sandbox.annotations;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfWriter;
public class RemoteGoto {
public static final String DEST = "results/annotations/subdir/abc.pdf";
public static final String SRC = "results/annotations/xyz.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
RemoteGoto app = new RemoteGoto();
app.createPdf(DEST);
app.createPdf2(SRC);
}
public void createPdf(String dest) throws IOException, DocumentException {
// first document
Document document1 = new Document();
PdfWriter.getInstance(document1, new FileOutputStream(dest));
document1.open();
Anchor anchor = new Anchor("This is a destination");
anchor.setName("dest");
document1.add(anchor);
document1.close();
}
public void createPdf2(String src) throws IOException, DocumentException {
// second document (with a link to the first one)
Document document2 = new Document();
PdfWriter.getInstance(document2, new FileOutputStream(src));
document2.open();
Chunk chunk = new Chunk("Link");
chunk.setAction(PdfAction.gotoRemotePage("subdir/abc.pdf", "dest", false, true));
document2.add(chunk);
document2.close();
}
}
remotegotopage
JAVA
JAVA
/**
* 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
*
* Creating a link from one PDF to another
*/
package sandbox.annotations;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfWriter;
public class RemoteGoToPage {
public static final String DEST = "results/annotations/subdir/abc2.pdf";
public static final String SRC = "results/annotations/xyz2.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
RemoteGoToPage app = new RemoteGoToPage();
app.createPdf(DEST);
app.createPdf2(SRC);
}
public void createPdf(String dest) throws IOException, DocumentException {
// first document
Document document1 = new Document();
PdfWriter.getInstance(document1, new FileOutputStream(dest));
document1.open();
document1.add(new Paragraph("page 1"));
document1.newPage();
document1.add(new Paragraph("page 2"));
document1.newPage();
document1.add(new Paragraph("page 3"));
document1.newPage();
document1.add(new Paragraph("page 4"));
document1.newPage();
document1.add(new Paragraph("page 5"));
document1.newPage();
document1.add(new Paragraph("page 6"));
document1.newPage();
document1.add(new Paragraph("page 7"));
document1.close();
}
public void createPdf2(String src) throws IOException, DocumentException {
// second document (with a link to the first one)
Document document2 = new Document();
PdfWriter.getInstance(document2, new FileOutputStream(src));
document2.open();
Chunk chunk = new Chunk("Link");
chunk.setAction(new PdfAction("subdir/abc2.pdf", 6));
document2.add(chunk);
document2.close();
}
}
addlinkimages
JAVA
JAVA
/**
* Example written by Bruno Lowagie in answer to
* http://stackoverflow.com/questions/29388313/itext-how-to-associate-actions-with-graphical-object
*/
package sandbox.annotations;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
/**
*
* @author Bruno Lowagie (iText Software)
*/
@WrapToTest
public class AddLinkImages {
public static final String DEST = "results/annotations/link_images.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new AddLinkImages().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfContentByte cb = writer.getDirectContent();
document.add(new Paragraph("Objects with links"));
Paragraph p = new Paragraph();
p.add(createImage("resources/images/info.png", "http://itextpdf.com/"));
p.add(createImage("resources/images/dog.bmp", "http://pages.itextpdf.com/ebook-stackoverflow-questions.html"));
p.add(createImage("resources/images/fox.bmp", "http://stackoverflow.com/q/29388313/1622493"));
p.add(createImage("resources/images/butterfly.wmf", "http://stackoverflow.com/questions/tagged/itext*"));
document.add(p);
document.close();
}
public Chunk createImage(String src, String url) throws BadElementException, IOException {
Image img = Image.getInstance(src);
Chunk chunk = new Chunk(img, 0, 0, true);
chunk.setAction(new PdfAction(url));
return chunk;
}
}
imageslinkstable
JAVA
JAVA
/*
* 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 sandbox.annotations;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.Annotation;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
/**
* @author Bruno Lowagie (iText Software)
*/
@WrapToTest
public class ImagesLinksTable {
public static final String DEST = "results/annotations/images_links_table.pdf";
public static final String IMG = "resources/images/info.png";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new ImagesLinksTable().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(3);
Image img = Image.getInstance(IMG);
Chunk c = new Chunk(img, 0, 0, true);
Anchor anchor = new Anchor(c);
anchor.setReference("http://lowagie.com/");
table.addCell(anchor);
table.addCell("A");
table.addCell("B");
table.addCell("C");
Chunk c2 = new Chunk(img, 0, 0, true);
c2.setAction(new PdfAction("http://itextpdf.com"));
table.addCell(new Phrase(c2));
table.addCell("D");
table.addCell("E");
table.addCell("F");
Image img2 = Image.getInstance(IMG);
Annotation a = new Annotation(0, 0, 0, 0, "http://lowagie.com/bio");
img2.setAnnotation(a);
table.addCell(img2);
document.add(table);
document.close();
}
}
relativelink
JAVA
JAVA
/**
* 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 sandbox.annotations;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class RelativeLink {
public static final String DEST = "results/annotations/relative_link.pdf";
public static final String XML = "resources/xml/data.xml";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new RelativeLink().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
// first document
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Chunk chunk = new Chunk("Click me");
chunk.setAnchor("./" + new File(XML).getName());
document.add(chunk);
document.close();
}
}
fileattachmentannot
JAVA
JAVA
/*
* Example written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/31006683/itext-clickable-image-should-open-ms-word-attachment
*/
package sandbox.annotations;
import com.itextpdf.text.Document;
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.PdfAppearance;
import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author Bruno Lowagie (iText Software)
*/
public class FileAttachmentAnnot {
public static final String DEST = "results/annotations/file_attachment.pdf";
public static final String PATH = "resources/misc/test.docx";
public static final String IMG = "resources/images/info.png";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new FileAttachmentAnnot().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Rectangle rect = new Rectangle(36, 700, 136, 800);
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
writer, PATH, "test.docx", null);
PdfAnnotation attachment =
PdfAnnotation.createFileAttachment(writer, rect, "Click me" , fs);
PdfAppearance app = writer.getDirectContent().createAppearance(100, 100);
Image img = Image.getInstance(IMG);
img.scaleAbsolute(100, 100);
img.setAbsolutePosition(0, 0);
app.addImage(img);
attachment.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, app);
writer.addAnnotation(attachment);
document.close();
}
}
addpointerannotation
JAVA
JAVA
/*
* This example was written by Bruno Lowagie in answer to the following question:
* http://stackoverflow.com/questions/26752663/adding-maps-at-itext-java
*/
package sandbox.annotations;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
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.PdfArray;
import com.itextpdf.text.pdf.PdfBorderDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class AddPointerAnnotation {
public static final String IMG = "resources/images/map_cic.png";
public static final String DEST = "results/annotations/map_with_pointer_annotation.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new AddPointerAnnotation().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Image img = Image.getInstance(IMG);
Document document = new Document(img);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
img.setAbsolutePosition(0, 0);
document.add(img);
Rectangle rect = new Rectangle(220, 350, 475, 595);
PdfAnnotation annotation = PdfAnnotation.createLine(writer, rect, "Cambridge Innovation Center", 225, 355, 470, 590);
PdfArray le = new PdfArray();
le.add(new PdfName("OpenArrow"));
le.add(new PdfName("None"));
annotation.setTitle("You are here:");
annotation.setColor(BaseColor.RED);
annotation.setFlags(PdfAnnotation.FLAGS_PRINT);
annotation.setBorderStyle(
new PdfBorderDictionary(5, PdfBorderDictionary.STYLE_SOLID));
annotation.put(new PdfName("LE"), le);
annotation.put(new PdfName("IT"), new PdfName("LineArrow"));
writer.addAnnotation(annotation);
document.close();
}
}
addstamp
JAVA
JAVA
/**
* 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 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.PdfAppearance;
import com.itextpdf.text.pdf.PdfContentByte;
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 AddStamp {
public static final String SRC = "resources/pdfs/hello.pdf";
public static final String DEST = "results/annotations/stamp_annotation.pdf";
public static final String IMG = "resources/images/itext.png";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new AddStamp().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 w = img.getScaledWidth();
float h = img.getScaledHeight();
Rectangle location = new Rectangle(36, 770 - h, 36 + w, 770);
PdfAnnotation stamp = PdfAnnotation.createStamp(
stamper.getWriter(), location, null, "ITEXT");
img.setAbsolutePosition(0, 0);
PdfContentByte cb = stamper.getOverContent(1);
PdfAppearance app = cb.createAppearance(w, h);
app.addImage(img);
stamp.setAppearance(PdfName.N, app);
stamp.setFlags(PdfAnnotation.FLAGS_PRINT);
stamper.addAnnotation(stamp, 1);
stamper.close();
reader.close();
}
}
addrotatedannotation
JAVA
JAVA
/**
* This example was written by Bruno Lowagie in answer to the following question:
* http://stackoverflow.com/questions/29303345/java-itext-rotate-a-link-rectangle
*/
package sandbox.annotations;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfAnnotation;
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 AddRotatedAnnotation {
public static final String SRC = "resources/pdfs/hello.pdf";
public static final String DEST = "results/annotations/rotated_annotation.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new AddRotatedAnnotation().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));
PdfAction action = new PdfAction("http://pages.itextpdf.com/ebook-stackoverflow-questions.html");
Rectangle linkLocation1 = new Rectangle(30, 770, 120, 800);
PdfAnnotation link1 = PdfAnnotation.createLink(stamper.getWriter(),
linkLocation1, PdfAnnotation.HIGHLIGHT_INVERT, action);
link1.setColor(BaseColor.RED);
stamper.addAnnotation(link1, 1);
Rectangle linkLocation2 = new Rectangle(30, 670, 60, 760);
PdfAnnotation link2 = PdfAnnotation.createLink(stamper.getWriter(),
linkLocation2, PdfAnnotation.HIGHLIGHT_INVERT, action);
link2.setColor(BaseColor.GREEN);
stamper.addAnnotation(link2, 1);
Rectangle linkLocation3 = new Rectangle(150, 770, 240, 800);
PdfAnnotation stamp1 = PdfAnnotation.createStamp(stamper.getWriter(), linkLocation3, "Landscape", "Confidential");
stamper.addAnnotation(stamp1, 1);
Rectangle linkLocation4 = new Rectangle(150, 670, 240, 760);
PdfAnnotation stamp2 = PdfAnnotation.createStamp(stamper.getWriter(), linkLocation4, "Portrait", "Confidential");
stamp2.setRotate(90);
stamper.addAnnotation(stamp2, 1);
Rectangle linkLocation5 = new Rectangle(250, 670, 340, 760);
PdfAnnotation stamp3 = PdfAnnotation.createStamp(stamper.getWriter(), linkLocation5, "Portrait", "Confidential");
stamp3.setRotate(45);
stamper.addAnnotation(stamp3, 1);
stamper.close();
reader.close();
}
}