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:
Obtain the page object where the annotation needs to be created.
Create a note annotation object on that page.
Set the annotation properties.
Update the annotation appearance to display it on the document.
This example shows how to create note annotations:
// 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, 650, 350, 600));
// Update the annotation appearance to display it on the document
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:
- Obtain the page object where the annotation needs to be created.
- Create a link annotation object on that page.
- Use
CPDFDestination
to specify the destination page, for example, jumping to page 2. - Set the annotation properties and attach the
CPDFDestination
object to the annotation.
This example shows how to create link annotations:
// 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:
Obtain the page object where the annotation needs to be created.
Create a text annotation object on that page.
Set the annotation properties.
Update the annotation appearance to display it on the document.
This example shows how to create free text annotations:
// 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:
Obtain the page object where the annotations need to be created.
Sequentially create rectangle, circle, and line annotations on that page.
Set the annotation properties.
Sequentially update the annotation appearance to display them on the document.
This example shows how to create shape annotations:
// 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, 410, 110, 300));
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
Name | Description |
---|---|
LINETYPE_UNKNOWN | Non-standard or invalid line segment endpoint. |
LINETYPE_NONE | No line segment endpoint. |
LINETYPE_ARROW | Two short lines intersect at a sharp angle, forming an open arrow. |
LINETYPE_CLOSEDARROW | Two 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_SQUARE | A square filled with the interior color of the annotation. |
LINETYPE_CIRCLE | A circle filled with the interior color of the annotation. |
LINETYPE_DIAMOND | A diamond shape filled with the interior color of the annotation. |
LINETYPE_BUTT | A short line perpendicular to the line itself, located at the endpoint. |
LINETYPE_ROPENARROW | Two short lines in opposite directions. |
LINETYPE_RCLOSEDARROW | A triangular closed arrow in opposite directions. |
LINETYPE_SLASH | A 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:
Obtain the page object where the annotation needs to be created.
Create a text object through the page object.
Use the text object to identify the location of the text to be marked.
Create the corresponding markup object on that page.
Set the properties of the markup object.
Update the annotation appearance to display it on the document.
This example shows how to create markup annotations:
// 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<CRect> cRectList = textPage.GetCharsRectAtPos(new CPoint(0,0),new CPoint(500,500),new CPoint(10,10));
// 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:
Obtain the page object where the annotation needs to be created.
Create the corresponding stamp on that page.
Set the properties of the stamp.
Update the annotation appearance to display it on the document.
This example shows how to create stamp annotations:
// 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:
Obtain the page object where the annotation needs to be created.
Create a ink annotation on that page.
Set the path taken by the freehand annotation.
Set other properties of the annotation.
Update the annotation appearance to display it on the document.
This example shows how to create freehand annotations:
// 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);
// Update the ink annotation in the document.
ink.UpdateAp();
Create Audio Annotations
The steps to create an audio annotation are as follows:
Obtain the page object where the annotation needs to be created.
Create an audio annotation on that page.
Set the audio file.
Set other properties.
Update the annotation appearance to display it on the document.
This example shows how to create audio annotations:
// 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(null, "soundFilePath");
// Set other properties.
sound.SetRect(new CRect(0, 50, 50, 0));
// Update the annotation appearance to display it in the document.
sound.UpdateAp();