HTML JPG PDF XML DOCX
  Product Family
PDF

Add Text to PDF in Node.js SDK

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

Get Started

How to add Text to PDF via Node.js SDK

The following code snippet, by the Aspose.PDF, loads a PDF and custom font file, defines the item with the custom style (text color, background color, font, rotation, alignment, and spacing), and specifies the rectangle in which the paragraph should be placed on the page. This is useful for adding stylized text to a certain area of the PDF page.

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

Aspose.PDF Cloud developers can easily load & add Text to PDF in just a few lines of code.

  1. Read the local PDF file.
  2. Upload the PDF file to the Aspose.PDF Cloud storage.
  3. Create a TextState object with formatting options (font, size, color, etc.).
  4. Create a Segment object with the text content and the TextState.
  5. Create a TextLine object with the Segment.
  6. Create a Paragraph object with the TextLine, formatting options, and positioning.
  7. Adds the Paragraph to the PDF file using the PdfApi.putAddText() method.
  8. Logs the status of the operation.
  9. Download the result if needed it
 

Add Text to PDF using Node.js


    import credentials from "./credentials.json"  with { type: "json" };
    import fs from 'node:fs/promises';
    import { PdfApi } from "asposepdfcloud";
    import { Color } from "asposepdfcloud/src/models/color.js";
    import { FontStyles } from "asposepdfcloud/src/models/fontStyles.js";
    import { LineSpacing } from "asposepdfcloud/src/models/lineSpacing.js";
    import { Paragraph } from "asposepdfcloud/src/models/paragraph.js";
    import { TextHorizontalAlignment } from "asposepdfcloud/src/models/textHorizontalAlignment.js";
    import { VerticalAlignment } from "asposepdfcloud/src/models/verticalAlignment.js";
    import { WrapMode } from "asposepdfcloud/src/models/wrapMode.js";
    import { TextLine } from "asposepdfcloud/src/models/textLine.js";
    import { Segment } from "asposepdfcloud/src/models/segment.js";
    import { Rectangle } from "asposepdfcloud/src/models/rectangle.js";
    import { TextState } from "asposepdfcloud/src/models/textState.js";

    const LOCAL_FILE_NAME = "c:\\Samples\\sample.pdf";
    const STORAGE_FILENAME = "sample.pdf";
    const PAGE_NUMBER = 1;
    const TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

    async function add() {
        const pdfApi = new PdfApi(credentials.id, credentials.key);
        try {

            const fileBuffer = await fs.readFile(LOCAL_FILE_NAME);
            await pdfApi.uploadFile(STORAGE_FILENAME, fileBuffer);

            const textState = Object.assign(new TextState(), {
                fontSize: 20,
                font: "Arial",
                foregroundColor: Object.assign(new Color(), { a: 255, r: 0, g: 0, b: 255 }),
                backgroundColor: Object.assign(new Color(), { a: 255, r: 255, g: 255, b: 0 }),
                fontStyle: FontStyles.Regular,
                underline: true
            });

            const segment = Object.assign(new Segment(),
                {
                    value: TEXT_CONTENT,
                    textState: textState
                });

            const textLine = Object.assign(new TextLine(), { segments: [segment] });

            const paragraph = Object.assign(new Paragraph(), {
                lineSpacing: LineSpacing.FullSize,
                wrapMode: WrapMode.ByWords,
                rectangle: Object.assign(new Rectangle(), { lLX: 10, lLY: 10, uRX: 300, uRY: 500 }),
                horizontalAlignment: TextHorizontalAlignment.FullJustify,
                verticalAlignment: VerticalAlignment.Center,
                lines: [textLine]
            });

            const result = await pdfApi.putAddText(
                STORAGE_FILENAME,
                PAGE_NUMBER,
                paragraph
            );

            console.log(result.body.status);
            const buffer = await pdfApi.downloadFile(STORAGE_FILENAME);
        } catch (error) {
            console.error("Error adding text to PDF:", error.message);
        }
    }
 

Work with Text in PDF

Text can be dynamically added to customize a PDF for a specific user, such as adding their name or personalized message. This is often used in marketing materials or for personalized reports. Adding text in specific fonts, colors, and alignments can make certain sections more readable and visually appealing, creating a better user experience. Text additions can help maintain consistent information across different PDF versions, avoiding manual edits and ensuring that key information remains in place. Add the Text 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 add the text into PDF files online and test the functionality.