Tutorials

How to Annotate PDFs on Windows

By ComPDFKit | Thu. 14 Nov. 2024
WindowsC#Annotations

Have you ever thought about providing your users with an efficient annotation tool so that they can add anything they want?  It's a common need to annotate PDFs when we are studying or working. Annotations are everywhere, reviewing, note-taking, proofreading, and even picture processing. Let's take a deep look at annotations.

 

We are going to show you the annotation types, how to annotate PDF materials on Windows programmatically, integration steps, and related useful functions.

 

 

Annotation Types

 

The ComPDFKit PDF SDK supports all common and standard annotations (as defined in the PDF Reference) that can be read and written by many popular apps.

- Note

- Link

- Free Text

- Shapes: Square, Circle, and Line

- Markup: Highlight, Underline, Strikeout, and Squiggly

- Stamp

- Ink

- Sound

 

You must want to avoid a time-consuming endeavor and bring document annotation to your app. That's why ComPDFKit PDF SDK can be useful. 

 

 

Create & Edit Annotations

 

ComPDFKit PDF SDK includes a wide variety of standard annotations, and each of them is added to the project similarly and simply. 

 

Note

 

To add a sticky note (text annotation) to a PDF document page by using the following method.

 

  CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
  CPDFPage page = document.PageAtIndex(0);
  CPDFTextAnnotation text = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_TEXT) as CPDFTextAnnotation;
  text.SetContent("test");
  text.SetRect(new CRect(0,50,50,0));
  byte[] color = {255,0,0};
  text.SetColor(color);
  text.UpdateAp();

  

Link

 

To add a hyperlink or intra-document link annotation to a PDF document page by using the following method.

 

  CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
  CPDFPage page = document.PageAtIndex(0);

  CPDFDestination dest = new CPDFDestination();
  CPDFLinkAnnotation link = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINK) as CPDFLinkAnnotation;
  link.SetRect(new CRect(0,50,50,0));
  link.SetDestination(document,dest);

  

Free Text

 

To add a free text annotation to a PDF document page by using the following method.

 

CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
CPDFPage page = document.PageAtIndex(0);

CPDFFreeTextAnnotation freeText = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT) as CPDFFreeTextAnnotation;
freeText.SetContent("test");
freeText.SetRect(new CRect(0, 50, 50, 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_LEFT);
freeText.UpdateAp();

 

Shapes

 

To add a shape annotation to a PDF document page by using the following method.

 

CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
CPDFPage page = document.PageAtIndex(0);
float[] dashArray = {2,1};
byte[] lineColor = {255,0,0};
byte[] bgColor = {0,255,0};

// Square
CPDFSquareAnnotation square = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE)   as CPDFSquareAnnotation;
square.SetRect(new CRect(0,50,50,0));
square.SetLineColor(lineColor);
square.SetBgColor(bgColor);
square.SetTransparency(120);
square.SetLineWidth(1);
square.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED,dashArray);
square.UpdateAp();

// Circle
CPDFCircleAnnotation circle = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE) as CPDFCircleAnnotation;
circle.SetRect(new CRect(0,50,50,0));
circle.SetLineColor(lineColor);
circle.SetBgColor(bgColor);
circle.SetTransparency(120);
circle.SetLineWidth(1);
circle.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED,dashArray);
circle.UpdateAp();

// Line
CPDFLineAnnotation line = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINE) as CPDFLineAnnotation;
line.SetLinePoints(new CPoint(0,0),new CPoint(50,50));
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_DASHDED,dashArray);
line.UpdateAp();

 

NoteCPDFLineAnnotation properties (points) point is specified in page-space coordinates. Page space is a coordinate system with the origin at the lower-left corner of the current page.  

 

Markup

 

It includes highlighters, underlines, strikeout, and squiggly annotations. Add a highlight annotation to a PDF document page by the following method, and similarly add other markup annotations.

 

CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
CPDFPage page = document.PageAtIndex(0);

CPDFTextPage textPage = page.GetTextPage();
List rectList = textPage.GetCharsRectAtPos(new Point(0,0),new Point(500,500),new Point(10,10));
List cRectList = new List();
foreach(var rect in rectList)
{
    cRectList.Add(new CRect((float)rect.Left, (float)rect.Top, (float)rect.Right, (float)rect.Bottom));
}

CPDFHighlightAnnotation highlight = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT) as CPDFHighlightAnnotation;
byte[]color = {0,255,0};
highlight.SetColor(color);
highlight.SetTransparency(120);
highlight.SetQuardRects(cRectList);
highlight.UpdateAp();

 

Stamp

 

To add standard, text, and image stamps to a PDF document page by using the following method.

 

CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
CPDFPage page = document.PageAtIndex(0);

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

// Text
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();

// Image
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();

 

Ink

 

Add annotations with your mouse like a pen to draw anything you like. You will no longer be limited by the shapes of annotations. Here is the sample code.

 

CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
CPDFPage page = document.PageAtIndex(0);

CPDFInkAnnotation ink = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_INK) as CPDFInkAnnotation;
List<List> pointList = new List<List>();
ink.SetInkPath(pointList);
ink.SetInkRect(new CRect(0, 50, 50, 0));
byte[] color = { 0, 255, 0 };
ink.SetInkColor(color);
ink.SetTransparency(120);
ink.SetThickness(4);
ink.UpdateAp();

 

Sound

 

If you have no time to annotate, you can just record your words by saying them out, and there will be a sound annotation. About the method of adding sound annotations, you can take the following code as an example. 

 

CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
CPDFPage page = document.PageAtIndex(0);

CPDFSoundAnnotation sound = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SOUND) as CPDFSoundAnnotation;
sound.SetRect(new CRect(0, 50, 50, 0));
sound.SetSoundPath("soundFilePath");
sound.UpdateAp();

 

 

Integrate Annotations into Your Project

 

With the four steps below, annotating functions can be embedded.  

- Create a new Windows project in C#.

- Integrate ComPDFKit PDF SDK annotations into your project.

- Apply the license key.

- Display a PDF document.


For detailed information about how to integrate the annotations to your app, and the sample code,  you can turn to our documentation page.

 

 

What Else Can You Do with Annotations?

 

We can also provide you with some more about annotations like deleting, flattening, exporting, importing, and the annotation appearances. If you are interested in how to complete these in code, please connect to our developer guides.

 

 

Download!

 

Now you have an overview of the annotations. Hope this post is helpful. Don’t hesitate to request a free trial. Contact our support team if you need any help with it or your ComPDFKit PDF SDK installation for Windows.