iOS
ComPDFKit PDF SDK
Guides

Create & Edit Annotations

 

ComPDFKit PDF SDK includes a wide variety of standard annotations, and each of them is added into the project in similar way.

 

Note

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

 

NSURL *url = [NSURL fileURLWithPath:pdfPath];
CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];
CPDFPage *page = [document pageAtIndex:0];
​
CPDFTextAnnotation *text = [[[CPDFTextAnnotation alloc] initWithDocument:document] autorelease];
text.contents = @"test";
text.bounds = CGRectMake(0, 0, 50, 50);
text.color = [UIColor yellowColor];
[page addAnnotation:text];

 


Link

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

 

NSURL *url = [NSURL fileURLWithPath:pdfPath];
CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];
CPDFPage *page = [document pageAtIndex:0];
​
CPDFDestination *dest = [[[CPDFDestination alloc] initWithDocument:document pageIndex:1] autorelease];
CPDFLinkAnnotation *link = [[[CPDFLinkAnnotation alloc] initWithDocument:document] autorelease];
link.bounds = CGRectMake(0, 0, 50, 50);
link.destination = dest;
//link.URL = @"https://www.";
[page addAnnotation:link];

 

 

Free Text

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

 

NSURL *url = [NSURL fileURLWithPath:pdfPath];
CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];
CPDFPage *page = [document pageAtIndex:0];
​
CPDFFreeTextAnnotation *freeText = [[[CPDFFreeTextAnnotation alloc] initWithDocument:document] autorelease];
freeText.contents = @"test";
freeText.bounds = CGRectMake(0, 0, 50, 50);
freeText.font = [UIFont systemFontOfSize:12];
freeText.fontColor = [UIColor redColor];
freeText.alignment = NSTextAlignmentLeft;
[page addAnnotation:freeText];

 


Shapes

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

 

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

NSURL *url = [NSURL fileURLWithPath:pdfPath];
CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];
CPDFPage *page = [document pageAtIndex:0];
​
CPDFBorder *border = [[[CPDFBorder alloc] initWithStyle:CPDFBorderStyleDashed
                                                lineWidth:1
                                              dashPattern:@[@(2), @(1)]] autorelease];
​
// Square
CPDFSquareAnnotation *square = [[[CPDFSquareAnnotation alloc] initWithDocument:document] autorelease];
square.bounds = CGRectMake(0, 0, 50, 50);
square.color = [UIColor redColor];
square.interiorColor = [UIColor yellowColor];
square.opacity = 0.5;
square.interiorOpacity = 0.5;
square.border = border;
[page addAnnotation:square];
​
// Circle
CPDFCircleAnnotation *circle = [[[CPDFCircleAnnotation alloc] initWithDocument:document] autorelease];
circle.bounds = CGRectMake(0, 0, 50, 50);
circle.color = [UIColor redColor];
circle.interiorColor = [UIColor yellowColor];
circle.opacity = 0.5;
circle.interiorOpacity = 0.5;
circle.border = border;
[page addAnnotation:circle];
​
// Line
CPDFLineAnnotation *line = [[[CPDFLineAnnotation alloc] initWithDocument:document] autorelease];
line.startPoint = CGPointMake(0, 0);
line.endPoint = CGPointMake(50, 50);
line.startLineStyle = CPDFLineStyleNone;
line.endLineStyle = CPDFLineStyleClosedArrow;
line.color = [UIColor redColor];
line.interiorColor = [UIColor yellowColor];
line.opacity = 0.5;
line.interiorOpacity = 0.5;
line.border = border;
[page addAnnotation:line];

 

NoteCPDFLineAnnotation properties (startPoint, endPoint) 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

To add a highlight annotation to a PDF Document page by using the following method, and add other markup annotations in similar way.

 

NSURL *url = [NSURL fileURLWithPath:pdfPath];
CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];
CPDFPage *page = [document pageAtIndex:0];
​
CPDFSelection *selection = ...;
NSMutableArray *quadrilateralPoints = [NSMutableArray array];
for (CPDFSelection *selection in selection.selectionsByLine) {
    CGRect bounds = selection.bounds;
    [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(bounds), CGRectGetMaxY(bounds))]];
    [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))]];
    [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(bounds), CGRectGetMinY(bounds))]];
    [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMinY(bounds))]];
}
​
CPDFMarkupAnnotation *highlight = [[[CPDFMarkupAnnotation alloc] initWithDocument:document markupType:CPDFMarkupTypeHighlight] autorelease];
highlight.color = [UIColor yellowColor];
highlight.quadrilateralPoints = quadrilateralPoints;
[page addAnnotation:highlight];

 


Stamp

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

 

NSURL *url = [NSURL fileURLWithPath:pdfPath];
CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];
CPDFPage *page = [document pageAtIndex:0];

// Standard
CPDFStampAnnotation *standard = [[[CPDFStampAnnotation alloc] initWithDocument:document type:0] autorelease];
[page addAnnotation:standard];

// Text
CPDFStampAnnotation *text = [[[CPDFStampAnnotation alloc] initWithDocument:document text:@"test" detailText:@"detail text" style:CPDFStampStyleRed shape:CPDFStampShapeArrowLeft] autorelease];
[page addAnnotation:text];

// Image
CPDFStampAnnotation *image = [[[CPDFStampAnnotation alloc] initWithDocument:document image:[UIImage imageNamed:@""]] autorelease];
[page addAnnotation:image];