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:

C#
// Obtain the page object where the note needs to be created
CPDFPage page = document.PageAtIndex(0);

// Create a text annotation for the note on that page
CPDFTextAnnotation textAnnotation = 
    page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_TEXT) as CPDFTextAnnotation;

// Set properties for the text annotation
textAnnotation.SetColor(new byte[] { 255, 0, 0 });
textAnnotation.SetTransparency(255);
textAnnotation.SetContent("ComPDFKit");
textAnnotation.SetRect(new CRect(300, 600, 350, 650));

// Update the annotation appearance to display it on the document
textAnnotation.UpdateAp();

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:

C#
// Get the page object for which the annotation needs to be created.
CPDFPage page = document.PageAtIndex(0); 
// Create a link annotation on the page.
CPDFLinkAnnotation link = 
page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINK) as CPDFLinkAnnotation;
CPDFDestination dest = new CPDFDestination();
// Set the link to navigate to the second page using `CPDFDestination`.
dest.PageIndex = 1;
// Set annotation properties and attach the `CPDFDestination` object to the annotation.
link.SetRect(new CRect(0, 50, 50, 0));
link.SetDestination(document, dest);

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 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:

C#
// Get the page object for which the annotation needs to be created.
CPDFPage page = document.PageAtIndex(0);
// Create a free text annotation on the page.
CPDFFreeTextAnnotation freeText = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT) as CPDFFreeTextAnnotation;
// Set annotation properties.
string str = "ComPDFKit Samples";
freeText.SetContent(str);
freeText.SetRect(new CRect(0, 100, 160, 0));
CTextAttribute textAttribute = new CTextAttribute();
textAttribute.FontName = "Helvetica";
textAttribute.FontSize = 12;
byte[] fontColor = { 255, 0, 0 };
textAttribute.FontColor = fontColor;
freeText.SetFreetextDa(textAttribute);
freeText.SetFreetextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_CENTER);
// Update the annotation in the document.
freeText.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:

C#
// Get the page object for which annotations need to be created.
CPDFPage page = document.PageAtIndex(0);
float[] dashArray = { 2, 1 };
byte[] lineColor = { 255, 0, 0 };
byte[] bgColor = { 0, 255, 0 };

// Create a square annotation on the page.
CPDFSquareAnnotation square = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE) as CPDFSquareAnnotation;
// Set annotation properties.
square.SetRect(new CRect(10, 250, 200, 200));
square.SetLineColor(lineColor);
square.SetBgColor(bgColor);
square.SetTransparency(120);
square.SetLineWidth(1);
square.SetBorderStyle(C_BORDER_STYLE.BS_DASHED, dashArray);
// Update the annotation appearance to display it in the document.
square.UpdateAp();

// Create a circle annotation on the page.
CPDFCircleAnnotation circle = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE) as CPDFCircleAnnotation;
// Set annotation properties.
circle.SetRect(new CRect(10, 300, 110, 410));
circle.SetLineColor(lineColor);
circle.SetBgColor(bgColor);
circle.SetTransparency(120);
circle.SetLineWidth(1);
circle.SetBorderStyle(C_BORDER_STYLE.BS_DASHED, dashArray);
// Update the annotation appearance to display it in the document.
circle.UpdateAp();

// Create a line annotation on the page.
CPDFLineAnnotation line = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINE) as CPDFLineAnnotation;
// Set annotation properties.
line.SetLinePoints(new CPoint(300, 300), new CPoint(350, 350));
line.SetLineType(C_LINE_TYPE.LINETYPE_NONE, C_LINE_TYPE.LINETYPE_CLOSEDARROW);
line.SetLineColor(lineColor);
line.SetTransparency(120);
line.SetLineWidth(1);
line.SetBorderStyle(C_BORDER_STYLE.BS_DASHED, dashArray);
// Update the annotation appearance to display it in the document.
line.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:

C#
// Get the page object for which annotations need to be created.
CPDFPage page = document.PageAtIndex(0);
// Create a text object using the page object.
CPDFTextPage textPage = page.GetTextPage();
// Use this text object to obtain the positions of the text to be marked.
List<Rect> rectList = textPage.GetCharsRectAtPos(new Point(0, 0), new Point(500, 500), new Point(10, 10));
List<CRect> cRectList = new List<CRect>();
foreach (var rect in rectList)
{
    cRectList.Add(new CRect((float)rect.Left, (float)rect.Top, (float)rect.Right, (float)rect.Bottom));
}
// Create corresponding highlight annotation objects on the page.
CPDFHighlightAnnotation highlight = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT) as CPDFHighlightAnnotation;
// Set properties for the highlight annotation object.
byte[] color = { 0, 255, 0 };
highlight.SetColor(color);
highlight.SetTransparency(120);
highlight.SetQuardRects(cRectList);
// Update the annotation appearance to display it in the document.
highlight.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:

C#
// Get the page object for which annotations need to be created.
CPDFPage page = document.PageAtIndex(0);

// Create a standard stamp.
CPDFStampAnnotation standard = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
standard.SetStandardStamp("Approved");
standard.UpdateAp();

// Create a text stamp.
CPDFStampAnnotation text = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
text.SetTextStamp("test", "detail text", C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE, C_TEXTSTAMP_COLOR.TEXTSTAMP_RED);
text.UpdateAp();

// Create an image stamp.
CPDFStampAnnotation image = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
byte[] imageData = new byte[500 * 500];
image.SetImageStamp(imageData, 500, 500);
image.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 freehand 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 freehand annotations:

C#
// Get the page object for which annotations need to be created.
CPDFPage page = document.PageAtIndex(0);
// Create a ink annotation on the page.
CPDFInkAnnotation ink = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_INK) as CPDFInkAnnotation;
// Set the path traversed by the ink annotation.
List<List<CPoint>> points = new List<List<CPoint>>();
points.Clear();
points.Add(new List<CPoint>()
{
    new CPoint(10, 100),
    new CPoint(100, 10),
});
ink.SetInkPath(points);
// Set other properties of the annotation.
ink.SetInkColor(new byte[] { 255, 0, 0 });
ink.SetBorderWidth(2);
ink.SetTransparency(128);
ink.SetInkPath(points);
ink.SetThickness(8);
ink.SetInkRect(new CRect(10, 10, 200, 200));
// Update the ink annotation in the document.
ink.UpdateAp();

Create Audio Annotations

The steps to create an audio annotation are as follows:

  1. Obtain the page object where the annotation needs to be created.

  2. Create an audio annotation on that page.

  3. Set the audio file.

  4. Set other properties.

  5. Update the annotation appearance to display it on the document.

This example shows how to create audio annotations:

C#
// Get the page object for which annotations need to be created.
CPDFPage page = document.PageAtIndex(0);
// Create a sound annotation on the page.
CPDFSoundAnnotation sound = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SOUND) as CPDFSoundAnnotation;
// Set the path to the audio file.
sound.SetSoundPath("soundFilePath");
// Set other properties.
sound.SetRect(new CRect(0, 50, 50, 0));
// Update the annotation appearance to display it in the document.
sound.UpdateAp();