Skip to content

Annotate PDFs on Windows

Youna edited this page Mar 1, 2023 · 1 revision

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

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

Note: CPDFLineAnnotation 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, strikeouts, 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<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)); }

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

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<CPoint>> pointList = new List<List<CPoint>>(); 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();