Back to Blog

    Developer Blog

    How can I add a link annotation to a PDF file?

    Foxit PDF SDK for iOS

    To add a link annotation to a PDF file, you should first call the FSPDFPage::addAnnot to add an annotation to a specified page, then call FSAction::Create to create an action, and set the action to the added link annotation. Following is the sample code for adding a URI link annotation to the first page of a PDF file:

    #define DOCUMENT_PATH [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
    ...
    // Get the path of a PDF
    NSString* pdfPath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"pdf"];
        
    // Initialize a PDFDoc object with the path to the PDF file.
    FSPDFDoc *document = [[FSPDFDoc alloc] initWithPath: pdfPath];
        
    // load the unencrypted document content.
     [document load:nil];
        
    // Get the first page of the PDF file.
    FSPDFPage *page = [document getPage:0];
        
    // Add a link annotation to the first page.
    FSRectF *rect = [[FSRectF alloc] initWithLeft1:250 bottom1:650 right1:450 top1:750];
    FSLink *linkAnnot = [[FSLink alloc] initWithAnnot:[page addAnnot: FSAnnotLink rect:rect]];
        
    // Create a URI action and set the URI.
    FSURIAction *uriAction = [[FSURIAction alloc] initWithAction:[FSAction create:document action_type:FSActionTypeURI]];
    [uriAction setURI:@"https://www.foxit.com"];
        
    // Set the action to link annotation.
    [linkAnnot setAction:uriAction];
        
    // Reset appearance stream.
     [linkAnnot resetAppearanceStream];
        
    // Save the document that has added the link annotaiton.
    [document saveAs:[DOCUMENT_PATH stringByAppendingString:@"Sample_annot.pdf"] save_flags:FSPDFDocSaveFlagNormal];