Skip to content
Guides

Create Annotations

ComPDFKit supports a wide range of annotation types, including notes, links, shapes, highlights, stamps, freehand drawings, and audio annotations, catering to diverse annotation requirements.

Create Note Annotations

Note annotations appear as small icons or labels. When clicked by the user, they can expand to display relevant annotation content. This annotation type is used for adding personal notes, reminders, or comments, allowing users to add personalized additional information to the document without affecting the readability of the original text.

The steps to create a note are as follows:

  1. Obtain the page object where the annotation needs to be created.
  2. Create a note annotation object on that page.
  3. Set the annotation properties.
  4. Update the annotation appearance to display it on the document.

This example shows how to create note annotations:

Java
// Insert page numbers.
int pageNumber = 0;
// Retrieve an instance of the PDF page.
CPDFPage page = document.pageAtIndex(pageNumber);
CPDFTextAnnotation textAnnotation = (CPDFTextAnnotation) page.addAnnot(CPDFAnnotation.Type.TEXT);
// Retrieve the actual size of the page to be inserted.
RectF pageSize = page.getSize();
RectF insertRect = new RectF(0, 0, 50, 50);
textAnnotation.setColor(Color.RED);
textAnnotation.setAlpha(255);
textAnnotation.setRect(insertRect);
textAnnotation.setContent("ComPDFKit");
textAnnotation.updateAp();

Create Link Annotations

Link annotations allow users to navigate directly to other locations within the document or external resources, enhancing the navigation experience.

The steps to create link annotations are as follows:

  1. Obtain the page object where the annotation needs to be created.
  2. Create a link annotation object on that page.
  3. Use CPDFDestination to specify the destination page, for example, jumping to page 2.
  4. Set the annotation properties and attach the CPDFDestination object to the annotation.

This example shows how to create link annotations:

Java
// Insert page numbers.
int pageNumber = 1;
// Retrieve an instance of the PDF page.
CPDFPage page = document.pageAtIndex(pageNumber);
CPDFLinkAnnotation linkAnnotation = (CPDFLinkAnnotation) page.addAnnot(CPDFAnnotation.Type.LINK);
RectF insertRect = new RectF(0,0,100,100);
linkAnnotation.setRect(insertRect);

// Navigate to a specified page number.
CPDFGoToAction goToAction = new CPDFGoToAction();
CPDFDestination destination = new CPDFDestination(pageNumber,0,pageSize.height(),1f);
goToAction.setDestination(document, destination);
linkAnnotation.setLinkAction(goToAction);

// Open a specific webpage.
CPDFUriAction uriAction = new CPDFUriAction();
uriAction.setUri("website");
linkAnnotation.setLinkAction(uriAction);
// Update the annotations onto the document.
linkAnnotation.updateAp();

Create Free Text Annotations

Free text annotations enable users to insert free-form text into PDF documents, serving the purpose of adding annotations, comments, or explanations to document content.

The steps to create a free text annotation are as follows:

  1. Obtain the page object where the annotation needs to be created.
  2. Create a free text annotation object on that page.
  3. Set the annotation properties.
  4. Update the annotation appearance to display it on the document.

This example shows how to create free text annotations:

Java
// Insert page numbers.
int pageNumber = 0;
// Retrieve an instance of the PDF page.
CPDFPage page = document.pageAtIndex(pageNumber);
CPDFFreetextAnnotation freetextAnnotation = (CPDFFreetextAnnotation) page.addAnnot(CPDFAnnotation.Type.FREETEXT, document);
RectF insertRect = new RectF(0,0,100,100);
freetextAnnotation.setRect(insertRect);
// Set annotation properties
freetextAnnotation.setContent("Hello World!");
freetextAnnotation.setFreetextAlignment(CPDFFreetextAnnotation.Alignment.ALIGNMENT_LEFT);
CPDFTextAttribute textAttribute = freetextAnnotation.getFreetextDa();
textAttribute.setColor(Color.YELLOW.getRGB());
textAttribute.setFontSize(14);
textAttribute.setFontName("fontName");
freetextAnnotation.setFreetextDa(textAttribute);
freetextAnnotation.updateAp();
// Update the annotations onto the document.
freetextAnnotation.updateAp();

Create Shape Annotations

Graphic annotations encompass shapes such as rectangles, circles, lines, and arrows, used to draw attention to or highlight specific areas in a document, conveying information that may be challenging to describe with text alone.

The steps to create graphic annotations are as follows:

  1. Obtain the page object where the annotations need to be created.
  2. Sequentially create rectangle, circle, and line annotations on that page.
  3. Set the annotation properties.
  4. Sequentially update the annotation appearance to display them on the document.

This example shows how to create shape annotations:

Java
// Insert page numbers.
int pageNumber = 0;
// Retrieve an instance of the PDF page.
CPDFPage page = document.pageAtIndex(pageNumber);
// Create a rectangle annotation object on the page.
CPDFSquareAnnotation squareAnnotation = (CPDFSquareAnnotation) page.addAnnot(CPDFAnnotation.Type.SQUARE, document);
RectF insertRect = new RectF(0,0,100,100);
squareAnnotation.setRect(insertRect);
// Set annotation properties.
squareAnnotation.setBorderColor(Color.YELLOW.getRGB());
CPDFBorderStyle borderStyle = new CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid, 10, new float[]{8.0F, 0F});
borderStyle.setBorderWidth(10F);
squareAnnotation.setBorderStyle(borderStyle);
squareAnnotation.setBorderAlpha(255);
squareAnnotation.setFillColor(Color.RED.getRGB());
squareAnnotation.setFillAlpha(255);
// Update the annotations onto the document.
squareAnnotation.updateAp();

// Create a circular annotation object on the page.
CPDFCircleAnnotation circleAnnotation = (CPDFCircleAnnotation) page.addAnnot(CPDFAnnotation.Type.CIRCLE, document);
RectF insertRect = new RectF(0,0,100,100);
circleAnnotation.setRect(insertRect);
// Set annotation properties.
circleAnnotation.setBorderColor(Color.YELLOW.getRGB());
CPDFBorderStyle borderStyle = new CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid, 10, new float[]{8.0F, 0F});
borderStyle.setBorderWidth(10F);
circleAnnotation.setBorderStyle(borderStyle);
circleAnnotation.setBorderAlpha(255);
circleAnnotation.setFillColor(Color.RED.getRGB());
circleAnnotation.setFillAlpha(255);
// Update the annotations onto the document.
circleAnnotation.updateAp();

// Create a circular annotation object on the page.
CPDFLineAnnotation lineAnnotation = (CPDFLineAnnotation) page.addAnnot(CPDFAnnotation.Type.LINE, document);
float lineWidth = 10f;
PointF startPoint = new PointF(0,0);
PointF endPoint = new PointF(200,200);
RectF area = new RectF(0,0,200,200);
lineAnnotation.setRect(area);
lineAnnotation.setLinePoints(startPoint, endPoint);
// Set annotation properties.
lineAnnotation.setLineType(CPDFLineAnnotation.LineType.LINETYPE_NONE, CPDFLineAnnotation.LineType.LINETYPE_ARROW);
CPDFBorderStyle borderStyle = new CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid,lineWidth,null);
lineAnnotation.setBorderStyle(borderStyle);
lineAnnotation.setBorderAlpha(255);
lineAnnotation.setBorderColor(Color.RED.getRGB())
  
// Update the annotations onto the document.
lineAnnptation.updateAp();

Explanation of Line Types

NameDescription
LINETYPE_UNKNOWNNon-standard or invalid line segment endpoint.
LINETYPE_NONENo line segment endpoint.
LINETYPE_ARROWTwo short lines intersect at a sharp angle, forming an open arrow.
LINETYPE_CLOSEDARROWTwo short lines intersect at a sharp angle, similar to style one, and are connected by a third line, forming a triangular closed arrow filled with the interior color of the annotation.
LINETYPE_SQUAREA square filled with the interior color of the annotation.
LINETYPE_CIRCLEA circle filled with the interior color of the annotation.
LINETYPE_DIAMONDA diamond shape filled with the interior color of the annotation.
LINETYPE_BUTTA short line perpendicular to the line itself, located at the endpoint.
LINETYPE_ROPENARROWTwo short lines in opposite directions.
LINETYPE_RCLOSEDARROWA triangular closed arrow in opposite directions.
LINETYPE_SLASHA short line, located at the endpoint, rotated approximately 30 degrees clockwise from the direction perpendicular to the line itself.

Create Markup Annotations

Incorporate annotations in a PDF document to highlight, emphasize, or explain specific content, such as important paragraphs, lines, words, keywords, tables, etc. ComPDFKit provides four types of markup annotations: highlight, underline, squiggly line, and strikethrough.

The steps to create a markup annotation are as follows:

  1. Obtain the page object where the annotation needs to be created.
  2. Create a text object through the page object.
  3. Use the text object to identify the location of the text to be marked.
  4. Create the corresponding markup object on that page.
  5. Set the properties of the markup object.
  6. Update the annotation appearance to display it on the document.

This example shows how to create markup annotations:

Java
// First, retrieve a TextSelection array from a specific region on the page to select text.
CPDFPage pdfPage = douc.pageAtIndex(1);
CPDFTextPage pdfTextPage = pdfPage.getTextPage();
RectF selectRect = new RectF(0f, 0f, 500f, 500f);
// Then, add a highlight annotation to the specific region for highlighting.
highlightAnnotation.setQuadRects(selectRect);
highlightAnnotation.setMarkedText("text");
highlightAnnotation.setRect(annotRect);
highlightAnnotation.updateAp();

Create Stamp Annotations

Stamp annotations are used to identify and validate the source and authenticity of a document. ComPDFKit supports standard stamps, text stamps, and image stamps.

The steps to create a stamp annotation are as follows:

  1. Obtain the page object where the annotation needs to be created.
  2. Create the corresponding stamp on that page.
  3. Set the properties of the stamp.
  4. Update the annotation appearance to display it on the document.

This example shows how to create stamp annotations:

java
// Retrieve the page object for creating annotations.
int pageNumber = 0;
CPDFPage pdfPage = document.pageAtIndex(pageNumber);

// Create a standard stamp.
int yOffset = 50;
float lastOffset = 0;
for (int i = 0; i < CPDFStampAnnotation.StandardStamp.values().length; i++) {
    CPDFPage page = document.pageAtIndex(4);
    CPDFStampAnnotation.StandardStamp standardStamp = CPDFStampAnnotation.StandardStamp.values()[i];
    if (standardStamp == null || standardStamp == CPDFStampAnnotation.StandardStamp.UNKNOWN) {
        continue;
    }
    // Add Standard stamp
    CPDFStampAnnotation standard = (CPDFStampAnnotation) page.addAnnot(CPDFAnnotation.Type.STAMP, document);
    if (standard == null) {
        continue;
    }
    standard.setStandardStamp(standardStamp);
    RectF pageSize = page.getSize();
    RectF insertRect = standard.getRect();
    float defaultWidth = 100F;
    int x = 50;
    if (i == 10) {
        lastOffset = 50;
    }
    if (i >= 10) {
        x = 150;
    }
    yOffset = (int) lastOffset + 10;
    PointF vertex = new PointF(x, yOffset);
    insertRect.set(vertex.x, vertex.y, vertex.x + defaultWidth, vertex.y + defaultWidth * Math.abs(insertRect.height() / insertRect.width()));
    lastOffset = insertRect.bottom;
    standard.setRect(insertRect);
    standard.updateAp();
}

// Create a text stamp.
CPDFPage page = document.pageAtIndex(4);
CPDFStampAnnotation stampAnnotation = (CPDFStampAnnotation) page.addAnnot(CPDFAnnotation.Type.STAMP, document);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = df.format(new Date());
stampAnnotation.setTextStamp(new CPDFStampAnnotation.TextStamp(
        "ComPDFKit", date, CPDFStampAnnotation.TextStampShape.TEXTSTAMP_RECT.id,
        CPDFStampAnnotation.TextStampColor.TEXTSTAMP_GREEN.id));
RectF insertRect = stampAnnotation.getRect();
insertRect.set( insertRect);
float defaultWidth = 150f;
PointF vertex = new PointF(300, 50);
insertRect.set(vertex.x, vertex.y, vertex.x + defaultWidth, vertex.y + defaultWidth * Math.abs(insertRect.height() / insertRect.width()));
stampAnnotation.setRect(insertRect);
stampAnnotation.updateAp();

// Create an text signature stamp.
CPDFStampAnnotation standard = (CPDFStampAnnotation) page.addAnnot(CPDFAnnotation.Type.STAMP, document);
String imagePath = rootDir + "/TestFiles/ComPDFKit.png";
RectF imageInsertRect = new RectF(457, 626.5f, 513, 570.5f);
standard.setRect(imageInsertRect);
if (imagePath.endsWith("png")) {
    File imageFile = new File(imagePath);
    BufferedImage image = null;
    try {
        image = ImageIO.read(imageFile);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    int[] pixel = getPixel(image);
    standard.updateApWithBitmap(pixel, image.getWidth(), image.getHeight());
} else {
    standard.setImageStamp(imagePath);
    standard.updateAp();
}

Create Ink Annotations

Ink annotations provide a direct and convenient method for drawing annotations.

The steps to create a ink annotation are as follows:

  1. Obtain the page object where the annotation needs to be created.
  2. Create a ink annotation on that page.
  3. Set the path taken by the ink annotation.
  4. Set other properties of the annotation.
  5. Update the annotation appearance to display it on the document.

This example shows how to create ink annotations:

Java
// Retrieve the page object for creating annotations.
int pageNumber = 0;
CPDFPage page = document.pageAtIndex(pageNumber);
// Set the path traced by the freehand annotation.
CPDFPage page1 = document.pageAtIndex(0);
ArrayList<ArrayList<PointF>> mDrawing = new ArrayList<>();
ArrayList<PointF> arc1 = new ArrayList<>();
arc1.add(new PointF(100, 100));
arc1.add(new PointF(110, 110));
arc1.add(new PointF(120, 120));
mDrawing.add(arc1);
ArrayList<PointF> arc2 = new ArrayList<>();
arc2.add(new PointF(115, 115));
arc2.add(new PointF(130, 130));
arc2.add(new PointF(160, 160));
mDrawing.add(arc2);
float scaleValue = 1F;
float borderWidth = 5F;
// Create a ink annotation on the page.
CPDFInkAnnotation inkAnnotation = (CPDFInkAnnotation) page1.addAnnot(CPDFInkAnnotation.Type.INK, document);
inkAnnotation.setColor(Color.RED.getRGB());
inkAnnotation.setAlpha(255);
inkAnnotation.setBorderWidth(borderWidth);
RectF rect = null;
RectF size = document.getPageSize(0);
if (size.isEmpty()) {
     return;
   }
int lineCount = mDrawing.size();
PointF[][] path = new PointF[lineCount][];
for (int lineIndex = 0; lineIndex < lineCount; lineIndex++) {
   ArrayList<PointF> line = mDrawing.get(lineIndex);
   int pointCount = line.size();
   PointF[] linePath = new PointF[pointCount];
   for (int pointIndex = 0; pointIndex < pointCount; pointIndex++) {
        PointF point = line.get(pointIndex);
         /****** Calculate the minimum bounding rectangle for the path ******/
        if (rect == null) {
          rect = new RectF(point.x, point.y, point.x, point.y);
           } else {
              rect.union(point.x, point.y);
       }
       /****** Calculate the coordinates transformed to the page and stored in the linePath collection ******/
        linePath[pointIndex] = point;
  }
   path[lineIndex] = linePath;
}
float dx = borderWidth / scaleValue / 2;
rect.inset(-dx, -dx);
rect.set(rect);
inkAnnotation.setInkPath(path);
inkAnnotation.setRect(rect);
inkAnnotation.updateAp();

Creating Reply Annotations

Reply annotations are used to reply to comments on annotations.

The steps to create a reply annotation are as follows:

  1. Get the page object and comment for which you need to create a reply.

  2. Create the reply comment on that page annotation.

  3. set other properties of the annotation.

This example shows how to create reply annotations:

Java
// Add reply annotation
CPDFPage page = document.pageAtIndex(0);
CPDFFreetextAnnotation freetextAnnotation = (CPDFFreetextAnnotation) page.getAnnotations().get(0);      CPDFAnnotation replyAnnotation = freetextAnnotation.createReplyAnnotation();
if (replyAnnotation.isReplyAnnot()) {
    Date date = new Date();
    CPDFDate cpdfDate = new CPDFDate(date.getYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), 480);
    replyAnnotation.setCreationDate(cpdfDate);
    replyAnnotation.setRecentlyModifyDate(cpdfDate);
    replyAnnotation.setTitle("Guest");
    replyAnnotation.setContent("ComPDFKit Reply");
}