HTML JPG PDF XML DOCX
  Product Family
PDF

Add Table to PDF in Node.js SDK

API for working with Table in PDF documents using Cloud Node.js SDK

Get Started

How to add Table to PDF via Node.js SDK

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

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

  1. Read the local PDF file.
  2. Uploads the PDF file to the Aspose PDF Cloud storage.
  3. Create the table of the desired layout.
  4. Downloads the updated PDF file from the Aspose PDF Cloud storage.
 

Add Table to PDF using Node.js


    import credentials from "../../../../Credentials/credentials.json"  with { type: "json" };
    import fs from 'node:fs/promises';
    import path from 'node:path';
    import { PdfApi } from "../../../src/api/api.js";
    import { Table } from "../../../src/models/table.js";
    import { Cell } from "../../../src/models/cell.js";
    import { FontStyles } from "../../../src/models/fontStyles.js";
    import { GraphInfo } from "../../../src/models/graphInfo.js";
    import { Row } from "../../../src/models/row.js";
    import { TextRect } from "../../../src/models/textRect.js";

    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...
        TABLE_ROWS: 5,
        TABLE_COLUMNS: 5,
    };

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

    const pdfTables = {
    async uploadDocument () {
            const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
            const pdfFileData = await fs.readFile(fileNamePath);
            await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
            console.log("File '" + configParams.PDF_DOCUMENT_NAME + "' successfully uploaded!");
        },
                        
        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);
        },

        initTable () {
            const numOfCols = configParams.TABLE_COLUMNS;
            const numOfRows = configParams.TABLE_ROWS;

            const headerTextState = {
                font: "Arial Bold",
                fontSize: 11,
                foregroundColor: { a: 0xFF, r: 0xFF, g: 0xFF, b: 0xFF },
                fontStyle: FontStyles.Bold,
            };

            const commonTextState = {
                font: "Arial Bold",
                fontSize: 11,
                foregroundColor: { a: 0xFF, r: 0x70, g: 0x70, b: 0x70 },
            };
        
            const table = new Table();
            table.rows = [];
        
            let colWidths = "";
            for (let colIndex = 0; colIndex < numOfCols; colIndex++)
            {
                colWidths += " 70";
            }
        
            table.columnWidths = colWidths;
        
            const borderTableBorder = new GraphInfo();
            borderTableBorder.color = { a: 0xFF, r: 0x00, g: 0xFF, b: 0x00 };
            borderTableBorder.lineWidth = 0.5;
        
            table.defaultCellBorder = {
                top: borderTableBorder,
                right: borderTableBorder,
                bottom: borderTableBorder,
                left: borderTableBorder,
                roundedBorderRadius: 0
            };
            table.left = 150;
            table.top = 250;
        
            for (let rowIndex = 0; rowIndex < numOfRows; rowIndex++)
            {
                const row = new Row();

                row.cells = [];
        
                for (let colIndex = 0; colIndex < numOfCols; colIndex++)
                {
                    const cell = new Cell();
                    
                    cell.defaultCellTextState = commonTextState;

                    if (rowIndex == 0)  // header cells
                    {
                        cell.backgroundColor = { a: 0xFF, r: 0x80, g: 0x80, b: 0x80 };
                        cell.defaultCellTextState = headerTextState;
                    }
                    else {
                        cell.backgroundColor = { a: 0xFF, r: 0xFF, g: 0xFF, b: 0xFF };
                    };
                
                    const textRect = new TextRect();
                    if (rowIndex == 0)
                        textRect.text = "header #" + colIndex;
                    else
                        textRect.text = "value #'(" + rowIndex + "," + colIndex + "')";
                    cell.paragraphs = [textRect];

                    row.cells.push(cell);
                }
                table.rows.push(row);
            }
            return table;
        },

        async addTableOnPage () {
            const jsonTable = this.initTable();

            const resultTabs = await pdfApi.postPageTables(configParams.PDF_DOCUMENT_NAME, configParams.PAGE_NUMBER, [ jsonTable ]);

            if (resultTabs.body.code == 200) {
                console.log("Table successfully added!");
                return resultTabs.body.table;
            }
            else
                comsole.error("Unexpected error : can't get links!!!");
        },
    }

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

Work with Table in PDF

Tables provide a structured format for presenting data systematically, making it easier for readers to understand and analyze information. They also enhance the visual appeal of a document, adding professionalism and organization. When dealing with numerical or comparative data, tables improve clarity by grouping related information in an easy-to-read format. Additionally, tables can incorporate real-time or dynamically generated content, such as data from databases or analytics dashboards. Add the Table 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 extract Table into PDF files online and test the functionality.