Creating and adding annotations
These examples were written in the context of questions such as:
addlinkimages
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
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.properties.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/main/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://kb.itextpdf.com/home/it7kb/ebooks/best-itext-7-questions-on-stackoverflow"));
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;
}
}
C#
C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Kernel.Pdf.Canvas.Wmf;
using iText.Kernel.Pdf.Xobject;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
namespace iText.Samples.Sandbox.Annotations
{
public class AddLinkImages
{
public static readonly String DEST = "results/sandbox/annotations/add_link_images.pdf";
public static readonly String sourceFolder = "../../../resources/img/";
public static readonly String BUTTERFLY = sourceFolder + "butterfly.wmf";
public static readonly String DOG = sourceFolder + "dog.bmp";
public static readonly String FOX = sourceFolder + "fox.bmp";
public static readonly String INFO = sourceFolder + "info.png";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new AddLinkImages().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
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://kb.itextpdf.com/home/it7kb/ebooks/best-itext-7-questions-on-stackoverflow"));
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)
{
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
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
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/main/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();
}
}
C#
C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Annot;
using iText.Layout;
using iText.Layout.Element;
namespace iText.Samples.Sandbox.Annotations
{
public class AddPointerAnnotation
{
public static readonly String DEST = "results/sandbox/annotations/add_pointer_annotation.pdf";
public static readonly String IMG = "../../../resources/img/map_cic.png";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new AddPointerAnnotation().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
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
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
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.PdfName;
import com.itextpdf.kernel.pdf.PdfNumber;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
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.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/main/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://kb.itextpdf.com/home/it7kb/ebooks/best-itext-7-questions-on-stackoverflow");
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();
}
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Kernel.Pdf.Annot;
namespace iText.Samples.Sandbox.Annotations
{
public class AddRotatedAnnotation
{
public static readonly String DEST = "results/sandbox/annotations/add_rotated_annotation.pdf";
public static readonly String SRC = "../../../resources/pdfs/hello.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new AddRotatedAnnotation().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
PdfPage firstPage = pdfDoc.GetFirstPage();
PdfAction linkAction = PdfAction.CreateURI("https://kb.itextpdf.com/home/it7kb/ebooks/best-itext-7-questions-on-stackoverflow");
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
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
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/main/resources/img/itext.png";
public static final String SRC = "./src/main/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.addImageAt(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();
}
}
C#
C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Annot;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;
namespace iText.Samples.Sandbox.Annotations
{
public class AddStamp
{
public static readonly String DEST = "results/sandbox/annotations/add_stamp.pdf";
public static readonly String IMG = "../../../resources/img/itext.png";
public static readonly String SRC = "../../../resources/pdfs/hello.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new AddStamp().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
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.AddImageAt(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
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
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/main/resources/img/info.png";
public static final String PATH = "./src/main/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.addImageFittedIntoRectangle(imageData, rect, true);
attachment.setNormalAppearance(xObject.getPdfObject());
pdfDoc.addNewPage().addAnnotation(attachment);
pdfDoc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Annot;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Filespec;
using iText.Kernel.Pdf.Xobject;
namespace iText.Samples.Sandbox.Annotations
{
public class FileAttachmentAnnot
{
public static readonly String DEST = "results/sandbox/annotations/file_attachment_annot.pdf";
public static readonly String IMG = "../../../resources/img/info.png";
public static readonly String PATH = "../../../resources/text/test.docx";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new FileAttachmentAnnot().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
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.AddImageFittedIntoRectangle(imageData, rect, true);
attachment.SetNormalAppearance(xObject.GetPdfObject());
pdfDoc.AddNewPage().AddAnnotation(attachment);
pdfDoc.Close();
}
}
}
imageslinkstable
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
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.properties.Property;
import com.itextpdf.layout.properties.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/main/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);
}
}
}
C#
C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Kernel.Pdf.Annot;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Layout.Renderer;
namespace iText.Samples.Sandbox.Annotations
{
public class ImagesLinksTable
{
public static readonly String DEST = "results/sandbox/annotations/images_links_table.pdf";
public static readonly String IMG = "../../../resources/img/info.png";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new ImagesLinksTable().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
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 : ImageRenderer
{
public LinkImageRenderer(Image image)
: base(image)
{
}
public override IRenderer GetNextRenderer()
{
return new LinkImageRenderer((Image) modelElement);
}
public override void Draw(DrawContext drawContext)
{
base.Draw(drawContext);
PdfAnnotation annotation = new PdfLinkAnnotation(GetOccupiedAreaBBox())
.SetAction(PdfAction.CreateURI("https://lowagie.com/bio"));
drawContext.GetDocument().GetLastPage().AddAnnotation(annotation);
}
}
}
}
relativelink
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
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/main/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();
}
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Layout;
using iText.Layout.Element;
namespace iText.Samples.Sandbox.Annotations
{
public class RelativeLink
{
public static readonly String DEST = "results/sandbox/annotations/relative_link.pdf";
public static readonly String XML = "../../../../../../resources/xml/data.xml";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new RelativeLink().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
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
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
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();
}
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Layout;
using iText.Layout.Element;
namespace iText.Samples.Sandbox.Annotations
{
public class RemoteGoToPage
{
public static readonly String DEST = "results/sandbox/annotations/";
public static readonly String[] DEST_NAMES =
{
"remote_go_to_page.pdf",
"subdir/xyz2.pdf"
};
public static void Main(String[] args)
{
DirectoryInfo directory = new DirectoryInfo(DEST + "subdir/");
directory.Create();
new RemoteGoToPage().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
CreateLinkPdf(dest + DEST_NAMES[0]);
CreateDestinationPdf(dest + DEST_NAMES[1]);
}
// This method creates a link destination pdf file.
private void CreateDestinationPdf(String src)
{
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)
{
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
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
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.properties.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();
}
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
namespace iText.Samples.Sandbox.Annotations
{
public class RemoteGoto
{
public static readonly String DEST = "results/sandbox/annotations/";
public static readonly String[] DEST_NAMES =
{
"remote_goto.pdf",
"subdir/xyz.pdf"
};
public static void Main(String[] args)
{
DirectoryInfo directory = new DirectoryInfo(DEST + "subdir/");
directory.Create();
new RemoteGoto().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
CreateLinkPdf(dest + DEST_NAMES[0]);
CreateDestinationPdf(dest + DEST_NAMES[1]);
}
// This method creates a link destination pdf file.
private void CreateDestinationPdf(String dest)
{
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)
{
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();
}
}
}