HTML JPG PDF XML DOCX
  Product Family
PDF

Extract Links from PDF in Node.js SDK

Extract a Links from a PDF Document using Cloud Node.js SDK

Get Started

How to Extract Links from PDF via Node.js SDK

To extract links from PDF, we’ll use Aspose.PDF Cloud Node.js SDK. This Cloud SDK assists Node.js programmers in developing cloud-based PDF creator, annotator, editor, and converter apps using Node.js programming language via Aspose.PDF REST API. Simply create an account at Aspose for Cloud and get your application information. Once you have the App SID & key, you are ready to give the Aspose.PDF Cloud Node.js SDK.

Package Manager Console Command


     
    npm install asposepdfcloud --save
     
     

Steps to Extract Links using Node.js

Aspose.PDF Cloud developers can easily load & get Links from PDF in just a few lines of code.

  1. Create an object to connect to the Pdf.Cloud API
  2. Upload your document file
  3. Retrieve required Link Annotation from the document using getPageLinkAnnotation() function
  4. Perform some action after successful retrieving the Link Annotation from document
  5. Download the result if needed it
 

Extract Links from PDF using Node.js


 
    import credentials from "./credentials.json"  with { type: "json" };    // json-file in this format: { "id": "*****", "key": "*******" }
    import fs from 'node:fs/promises';
    import path from "node:path";
    import { PdfApi } from "asposepdfcloud";

    const configParams = {
        LOCAL_FOLDER: "C:\\Samples\\",
        PDF_DOCUMENT_NAME: "sample.pdf",
        LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
        PAGE_NUMBER: 2,         // Your document page number...
        LINK_FIND_ID: "GI5UO32UN5KVESKBMN2GS33OHMZTEMJMGUYDQLBTGYYCYNJSGE",
    };

    const pdfApi = new PdfApi(credentials.id, credentials.key);

    const pdfLinks = {
        async uploadDocument() {
            const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
            const pdfFileData = await fs.readFile(pdfFilePath);
            await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
        },
            
        async getAllLinks () {
            const resultLinks = await pdfApi.getPageLinkAnnotations(configParams.PDF_DOCUMENT_NAME, configParams.PAGE_NUMBER);

            if (resultLinks.body.code == 200 && resultLinks.body.links.list) {
                this.showLinks(resultLinks.body.links.list, "all");
                return resultLinks.body.links.list;
            }
            else
                console.Error("Unexpected error : can't get links!!!");
        },
        
        showLinks (links, prefix) {
            if (Array.isArray(links) && links.length > 0)
            {
                links.forEach(function(link) {
                    console.log(prefix +" => '" + link.id + "', '" + link.action);
                });
            }
        },
    }

    async function main() {
        try {
            await pdfLinks.uploadDocument();
            await pdfLinks.getAllLinks();
        } catch (error) {
            console.error("Error:", error.message);
        }
    };
 

Work with Links in PDF

By extracting links, one can systematically verify the validity and relevance of each URL, ensuring that all references are current and functional.​ For tasks such as downloading linked documents or conducting batch analyses, extracting URLs enables automation, saving time and reducing manual effort. Extract the Links from PDF documents with Aspose.PDF Cloud Node.js SDK.

With our Node.js library you can:

  • Add PDF document’s header & footer in text or image format.
  • Add tables & stamps (text or image) to PDF documents.
  • Append multiple PDF documents to an existing file.
  • Work with PDF attachments, annotations, & form fields.
  • Apply encryption or decryption to PDF documents & set a password.
  • Delete all stamps & tables from a page or entire PDF document.
  • Delete a specific stamp or table from the PDF document by its ID.
  • Replace single or multiple instances of text on a PDF page or from the entire document.
  • Extensive support for converting PDF documents to various other file formats.
  • Extract various elements of PDF files & make PDF documents optimized.
  • You can try out our free App to test the functionality online.