Back to Blog

    Developer Blog

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

    Foxit PDF SDK for Android

    To add a link annotation to a PDF file, you should first call the PDFPage.addAnnot to add an annotation to a specified page, then call Action.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:

    private Link linkAnnot = null;
    ...
    String path = "mnt/sdcard/input_files/sample.pdf";
    try {
    // Initialize a PDFDoc object with the path to the PDF file.
    PDFDoc document = new PDFDoc(path);
    
    // Load the unencrypted document content.
    document.load(null);
    
    // Get the first page of the PDF file.
    PDFPage page = document.getPage(0);
    
    // Add a link annotation to the first page.
    linkAnnot = (Link) page.addAnnot(Annot.e_Link, new RectF(250, 750, 500, 650));
    
    // Create a URI action and set the URI.
    URIAction uriAction = (URIAction) Action.create(document, Action.e_TypeURI);
    uriAction.setURI("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 annotation.
    document.saveAs("mnt/sdcard/input_files/sample_annot.pdf", PDFDoc.e_SaveFlagNormal);
    
    } catch (Exception e) {
    e.printStackTrace();
    
    }