HTML JPG PDF XML DOCX
  Product Family
PDF

Add Links to PDF in Node.js SDK

Add a Links to a PDF Document using Cloud Node.js SDK

Get Started

How to add Links to PDF via Node.js SDK

To add links into 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 add Links into PDF via Cloud Node.js

Aspose.PDF Cloud developers can easily load & add Links to 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. Create a new Link Annotation with the required properties
  4. Append new Link Annotation to the document using postPageLinkAnnotations() function
  5. Perform some action after successful addition
  6. Download the result if needed it
 

Add Links to 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";
    import { Color } from "asposepdfcloud/src/models/color.js";
    import { Link } from "asposepdfcloud/src/models/link.js";
    import { Rectangle } from "asposepdfcloud/src/models/rectangle.js";
    import { LinkAnnotation } from "asposepdfcloud/src/models/linkAnnotation.js";
    import { LinkHighlightingMode} from "asposepdfcloud/src/models/linkHighlightingMode.js";
    import { LinkActionType } from "asposepdfcloud/src/models/linkActionType.js";

    const configParams = {
        LOCAL_FOLDER: "C:\\Samples\\",
        PDF_DOCUMENT_NAME: "sample.pdf",
        LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
        NEW_LINK_ACTION: "https://reference.aspose.cloud/pdf/#/",
        PAGE_NUMBER: 2,     // Your document page number...
        LINK_POS_LLX: 244.914,
        LINK_POS_LLY: 488.622,
        LINK_POS_URX: 284.776,
        LINK_POS_URY: 498.588,
    };

    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 downloadResult() {
            const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
            const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
            await fs.writeFile(filePath, changedPdfData.body);
            console.log("Downloaded: " + filePath);
        },

        async appendLink () {
            const linkColor = new Color({ a: 255, r: 0, g: 255, b: 0 });

            const linkRectangle = new Rectangle();
            linkRectangle.lLX = configParams.LINK_POS_LLX;
            linkRectangle.lLY = configParams.LINK_POS_LLY;
            linkRectangle.uRX = configParams.LINK_POS_URX;
            linkRectangle.uRY = configParams.LINK_POS_URY;

            const linkItem = new Link({ rel: "self" });

            const newLink = new LinkAnnotation();
            newLink.links = [ linkItem ];
            newLink.actionType = LinkActionType.GoToURIAction,
            newLink.action = configParams.NEW_LINK_ACTION,
            newLink.highlighting = LinkHighlightingMode.Invert,
            newLink.color = linkColor;
            newLink.rect = linkRectangle;
            
            var addResponse = await pdfApi.postPageLinkAnnotations(configParams.PDF_DOCUMENT_NAME, configParams.PAGE_NUMBER, [ newLink ]);

            if (addResponse.body.code == 200) {
                console.log("Append link successful!");
                return true;
            }
        },
    }

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

Work with Links in PDF

Adding links to a PDF enhances usability, interactivity, and accessibility. Whether for navigation, marketing, or cross-referencing, hyperlinks improve the document’s effectiveness, making it easier for users to find relevant content and take action. Add the Links into 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.