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:

swift
// Retrieve the page object for creating a note.
if let page = document?.page(at: 0) {
    // Configure the properties of the note annotation.
    let text = CPDFTextAnnotation(document: document)
    
    // Configure the properties of the note annotation.
    text?.contents = "test"
    text?.bounds = CGRect(x: 50, y: 200, width: 50, height: 50)
    text?.color = UIColor.yellow
    
    // Update the annotation appearance to make it visible on the document.
    page.addAnnotation(text!)
}
objective-c
// Retrieve the page object for creating a note.
CPDFPage *page = [document pageAtIndex:0];
// Configure the properties of the note annotation.
CPDFTextAnnotation *text = [[CPDFTextAnnotation alloc] initWithDocument:document];
// Configure the properties of the note annotation.
text.contents = @"test";
text.bounds = CGRectMake(50, 200, 50, 50);
text.color = [UIColor yellowColor];

// Update the annotation appearance to make it visible on the document.
[page addAnnotation:text];

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:

swift
// Retrieve the page object for creating a note.
if let page = document?.page(at: 0) {
    // Create a link annotation on the page.
    // Use `CPDFDestination` to set the link to navigate to the second page.
    if let dest = CPDFDestination(document: document, pageIndex: 1),
       let link = CPDFLinkAnnotation(document: document) {
        
        // Configure annotation properties and attach the `CPDFDestination` object to the annotation.
        link?.bounds = CGRect(x: 50, y: 100, width: 50, height: 50)
        link?.destination = dest
        // link.url = "https://www."
        
        page.addAnnotation(link!)
    }
}
objective-c
// Retrieve the page object for creating a note.
CPDFPage *page = [document pageAtIndex:0];
// Create a link annotation on the page.
// Use `CPDFDestination` to set the link to navigate to the second page.
 CPDFDestination *dest = [[CPDFDestination alloc] initWithDocument:document pageIndex:1];
 CPDFLinkAnnotation *link = [[CPDFLinkAnnotation alloc] initWithDocument:document];

// Configure annotation properties and attach the `CPDFDestination` object to the annotation.
link.bounds = CGRectMake(50, 100, 50, 50);
link.destination = dest; //    link.URL = @"https://www.";
[page addAnnotation:link];

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 text annotation are as follows:

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

  2. Create a free 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:

swift
// Retrieve the page object for creating the annotation.
if let page = document?.page(at: 0) {
    // Create a text annotation on that page.
    let freeText = CPDFFreeTextAnnotation(document: document)
    
    // Configure the properties of the annotation.
    freeText?.contents = "\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare."
    freeText?.bounds = CGRect(x: 10, y: 200, width: 160, height: 570)
    freeText?.font = UIFont(name: "Helvetica", size: 12)
    freeText?.fontColor = UIColor.red
    freeText?.alignment = .left
    
    // Add a free text annotation on the page.
    page.addAnnotation(freeText!)
}
objective-c
// Retrieve the page object for creating the annotation.
CPDFPage *page = [document pageAtIndex:0];
// Create a free text annotation on that page.
CPDFFreeTextAnnotation *freeText = [[CPDFFreeTextAnnotation alloc] initWithDocument:document];
// Configure the properties of the annotation.
freeText.contents = @"\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare.";
freeText.bounds = CGRectMake(10, 200, 160, 570);
freeText.font = [UIFont fontWithName:@"Helvetica" size:12];
freeText.fontColor = [UIColor redColor];
freeText.alignment = NSTextAlignmentLeft;
//  add a free text annotation for page
[page1 addAnnotation:freeText1];

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:

swift
// Retrieve the page object for creating the annotation.
let border1 = CPDFBorder(style: .dashed, lineWidth: 1, dashPattern: [2, 1])
let border2 = CPDFBorder(style: .dashed, lineWidth: 1, dashPattern: [2, 0])

if let page = document?.page(at: 0) {
    // Create a rectangle annotation object on that page.
    let square = CPDFSquareAnnotation(document: document)
    // Configure the properties of the annotation.
    square?.bounds = CGRect(x: 400, y: 200, width: 80, height: 300)
    square?.color = UIColor.green
    square?.interiorColor = UIColor.purple
    square?.opacity = 1.0
    square?.interiorOpacity = 1.0
    square?.border = border2
    page.addAnnotation(square!)

    // Create a circular annotation object on that page.
    let circle = CPDFCircleAnnotation(document: document)
    // Configure the properties of the annotation.
    circle?.bounds = CGRect(x: 300, y: 300, width: 100, height: 100)
    circle?.color = UIColor.red
    circle?.interiorColor = UIColor.yellow
    circle?.opacity = 0.5
    circle?.interiorOpacity = 0.5
    circle?.border = border1
    page.addAnnotation(circle!)

    // Create a line segment annotation object on that page.
    let line = CPDFLineAnnotation(document: document)
    // Configure the properties of the annotation.
    line?.startPoint = CGPoint(x: 350, y: 270)
    line?.endPoint = CGPoint(x: 260, y: 370)
    line?.startLineStyle = .square
    line?.endLineStyle = .circle
    line?.color = UIColor.red
    line?.interiorColor = UIColor.yellow
    line?.opacity = 0.5
    line?.interiorOpacity = 0.5
    line?.border = border1
    line?.contents = "Dashed Captioned"
    page.addAnnotation(line!)
}
objective-c
// Retrieve the page object for creating the annotation.
CPDFBorder *border1 = [[CPDFBorder alloc] initWithStyle:CPDFBorderStyleDashed
                                              lineWidth:1
                                            dashPattern:@[@(2), @(1)]];
CPDFBorder *border2 = [[CPDFBorder alloc] initWithStyle:CPDFBorderStyleDashed
                                              lineWidth:1
                                            dashPattern:@[@(2), @(0)]];
CPDFPage *page = [document pageAtIndex:0];

// Create a rectangle annotation object on that page.
CPDFSquareAnnotation *square = [[CPDFSquareAnnotation alloc] initWithDocument:document];
// Configure the properties of the annotation.
square.bounds = CGRectMake(400, 200, 80, 300);
square.color = [UIColor greenColor];
square.interiorColor = [UIColor purpleColor];
square.opacity = 1.0;
square.interiorOpacity = 1.0;
square.border = border2;
[page addAnnotation:square];

// Create a circular annotation object on that page.
CPDFCircleAnnotation *circle = [[CPDFCircleAnnotation alloc] initWithDocument:document];
// Configure the properties of the annotation.
circle.bounds = CGRectMake(300, 300, 100, 100);
circle.color = [UIColor redColor];
circle.interiorColor = [UIColor yellowColor];
circle.opacity = 0.5;
circle.interiorOpacity = 0.5;
circle.border = border1;
[page addAnnotation:circle];

// Create a line segment annotation object on that page.
CPDFLineAnnotation *line = [[CPDFLineAnnotation alloc] initWithDocument:document];
// Configure the properties of the annotation.
line.startPoint = CGPointMake(350, 270);
line.endPoint = CGPointMake(260, 370);
line.startLineStyle = CPDFLineStyleSquare;
line.endLineStyle = CPDFLineStyleCircle;
line.color = [UIColor redColor];
line.interiorColor = [UIColor yellowColor];
line.opacity = 0.5;
line.interiorOpacity = 0.5;
line1.border = border1;
[line setContents:@"Dashed Captioned"];
[page addAnnotation:line1];

Explanation of Line Types

NameDescription
CPDFLineStyleNoneNo special line ending.
CPDFLineStyleOpenArrowTwo short lines meeting in an acute angle to form an open arrowhead.
CPDFLineStyleClosedArrowTwo short lines meeting in an acute angle as in the LINETYPE_ARROW style and connected by a third line to form a triangular closed arrowhead filled with the annotation's interior color.
CPDFLineStyleSquareA square filled with the annotation's interior color.
CPDFLineStyleCircleA circle filled with the annotation's interior color.
CPDFLineStyleDiamondA diamond shape filled with the annotation's interior color.
                               |

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:

swift
// Retrieve the page object for creating the annotation.
if let page = document?.page(at: 0) {
    // Get the array of search results.
    if let resultArray = document?.findString("Page", withOptions: .caseInsensitive) as? [[CPDFSelection]] {
        
        // Get the first page of search results. 
        if let selections = resultArray[safe: 3] {
            
            // Get the first search result on the first page.
            if let selection = selections.first {
                
                var quadrilateralPoints: [CGPoint] = []
                
                let bounds = selection. bounds
                quadrilateralPoints?.append(CGPoint(x: bounds.minX, y: bounds.maxY))
                quadrilateralPoints?.append(CGPoint(x: bounds.maxX, y: bounds.maxY))
                quadrilateralPoints?.append(CGPoint(x: bounds.minX, y: bounds.minY))
                quadrilateralPoints?.append(CGPoint(x: bounds.maxX, y: bounds.minY))
                
                // Create a highlight annotations.
                if let highlight = CPDFMarkupAnnotation(document: document, markupType: .highlight) {
                    highlight.color = UIColor.yellow
                    highlight.quadrilateralPoints = quadrilateralPoints!
                    page.addAnnotation(highlight)
                }
            }
        }
    }
}
objective-c
// Retrieve the page object for creating the annotation.
CPDFPage *page = [document pageAtIndex:0];
// Get the array of search results.
NSArray *resultArray = [document findString:@"Page" withOptions:CPDFSearchCaseInsensitive];

// Get the first page of search results.
NSArray *selections = [resultArray objectAtIndex:3];

// Get the first search result on the first page.
CPDFSelection *selection = [selections objectAtIndex:0];

NSMutableArray *quadrilateralPoints = [NSMutableArray array];

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


// Create a highlight annotations.
CPDFMarkupAnnotation *highlight = [[CPDFMarkupAnnotation alloc] initWithDocument:document markupType:CPDFMarkupTypeHighlight];
highlight.color = [UIColor yellowColor];
highlight.quadrilateralPoints = quadrilateralPoints;
[page4 addAnnotation: highlight];

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:

swift
// Retrieve the page object for creating the annotation.
if let page = document?.page(at: 0) {
    // Create a standard stamp.
    let standard = CPDFStampAnnotation(document: document, type: i)
    standard?.bounds = CGRect(x: 50, y:30, width: 50, height: 30)
    page.addAnnotation(standard!)

    // Create a text stamp.
    let outputFormatter = DateFormatter()
    outputFormatter?.timeZone = TimeZone.current

    // Get date.
    let tDate: String
    outputFormatter.dateFormat = "yyyy-MM-dd HH:mm:SS"
    tDate = outputFormatter.string(from: Date())
    
    let text = CPDFStampAnnotation(document: document, text: "ComPDFKit", detailText: tDate, style: .red, 		shape: .arrowLeft)
    text?.bounds = CGRect(x: 150, y: 50, width: 80, height: 50)
    page.addAnnotation(text!)

    // Create an image stamp.
    if let logoImage = UIImage(named: "Logo") {
        let image = CPDFStampAnnotation(document: document, image: logoImage)
        image?.bounds = CGRect(x: 150, y: 120, width: 50, height: 50)
        page.addAnnotation(image!)
    }
}
objective-c
// Retrieve the page object for creating the annotation.
CPDFPage *page = [document pageAtIndex:0];

// Create a standard stamp.
 CPDFStampAnnotation *standard = [[CPDFStampAnnotation alloc] initWithDocument:document type:i];
 standard.bounds = CGRectMake(50, height5 - i*30, 50, 30);
 [page addAnnotation:standard];

// Create a text stamp.
NSTimeZone* timename = [NSTimeZone systemTimeZone];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init ];
[outputFormatter setTimeZone:timename ];

// Get date.
NSString *tDate = nil;
[outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:SS"];
tDate = [outputFormatter stringFromDate:[NSDate date]];
CPDFStampAnnotation *text = [[CPDFStampAnnotation alloc] initWithDocument:document text:@"ComPDFKit" detailText:tDate style:CPDFStampStyleRed shape:CPDFStampShapeArrowLeft];
text.bounds = CGRectMake(150, height5-50, 80, 50);
[page addAnnotation:text];

// Create an image stamp.
CPDFStampAnnotation *image = [[CPDFStampAnnotation alloc] initWithDocument:document image:[UIImage imageNamed:@"Logo"]];
image.bounds = CGRectMake(150, height5-120, 50, 50);
[page addAnnotation:image];

Create Ink Annotations

Ink annotations provide a direct and convenient method for drawing annotations.

The steps to create a freehand 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 ink annotations:

swift
// Retrieve the page object for creating the annotation.
if let page = document?.page(at: 0) {
    // Create a ink drawing annotation on that page.
    let ink = CPDFInkAnnotation(document: document)
    // Set the path traced by the freehand drawing annotation.
    let startPoint = CGPoint(x: 220, y: 505)
    let point1 = CGPoint(x: 100, y: 490)
    let point2 = CGPoint(x: 120, y: 410)
    let point3 = CGPoint(x: 100, y: 400)
    let point4 = CGPoint(x: 180, y: 490)
    let endPoint = CGPoint(x: 140, y: 440)
    
    ink?.color = UIColor.red
    ink?.opacity = 0.5
    ink?.borderWidth = 2.0
    ink?.paths = [
        [
            NSValue(cgPoint: startPoint),
            NSValue(cgPoint: point1),
            NSValue(cgPoint: point2),
            NSValue(cgPoint: point3),
            NSValue(cgPoint: point4),
            NSValue(cgPoint: endPoint)
        ]
    ]
    page.addAnnotation(ink!)
}
objective-c
// Retrieve the page object for creating the annotation.
CPDFPage *page = [document pageAtIndex:0];
// Create a ink drawing annotation on that page.
CPDFInkAnnotation *ink = [[CPDFInkAnnotation alloc] initWithDocument:document];
// Set the path traced by the freehand drawing annotation.
CGPoint startPoint = CGPointMake(220, 505);
CGPoint point1 = CGPointMake(100, 490);
CGPoint point2 = CGPointMake(120, 410);
CGPoint point3 = CGPointMake(100, 400);
CGPoint point4 = CGPointMake(180, 490);
CGPoint endPoint = CGPointMake(140, 440);
ink.color = [UIColor redColor];
ink.opacity = 0.5;
ink.borderWidth = 2.0;
ink.paths = @[@[[NSValue valueWithCGPoint:startPoint],[NSValue valueWithCGPoint:point1],[NSValue valueWithCGPoint:point2],[NSValue valueWithCGPoint:point3],[NSValue valueWithCGPoint:point4],[NSValue valueWithCGPoint:endPoint]]];
[page addAnnotation:ink];

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:

swift
// Retrieve the page object for creating the annotation.
if let page = document.page(at: 0) {
    // Create an audio annotation on that page.
    if let soundAnnotation = CPDFSoundAnnotation(document: document) {
        // Set the audio file for the annotation.
        if let filePath = Bundle.main.path(forResource: "Bird", ofType: "wav") {
            soundAnnotation.setMediaPath(filePath)
            // Configure additional properties.
            soundAnnotation?.bounds = CGRect(x: 100, y: 200, width: 50, height: 50)
            page.addAnnotation(soundAnnotation!)
        }
    }
}
objective-c
// Retrieve the page object for creating the annotation.
CPDFPage page = document.PageAtIndex(0);
// Create an audio annotation on that page.
CPDFSoundAnnotation *soundAnnotation = [[CPDFSoundAnnotation alloc] initWithDocument:document];
// Set the audio file for the annotation.
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Bird" ofType:@"wav"];
// Configure additional properties.
if ([soundAnnotation setMediaPath:filePath]) {
   soundAnnotation.bounds = CGRectMake(100, 200, 50, 50);
   [page4 addAnnotation:soundAnnotation];
}