HTML JPG PDF XML DOCX
  Product Family
PDF

Replace Tables in PDF in Node.js SDK

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

Get Started

How to replace Tables in PDF via Node.js SDK

To replace Tables in 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 replace Tables in PDF via Cloud Node.js

Aspose.PDF Cloud developers can easily load & replace Tables in PDF in just a few lines of code.

  1. Read the local PDF file.
  2. Upload a local PDF to Aspose Cloud.
  3. Retrieve and display all detected tables.
  4. Retrieve and display one table by its ID.
  5. Downloads the updated PDF file from the Aspose PDF Cloud storage.
 

Replace Tables in 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_ID: "GE5TCOZSGAYCYNRQGUWDINZVFQ3DGMA",    // Your table id...
        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 replaceTable () {
            const jsonTable = this.initTable();

            const resultTabs = await pdfApi.putTable(configParams.PDF_DOCUMENT_NAME, configParams.TABLE_ID, jsonTable);

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

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

Replace Tables in PDF

Replacing tables in a PDF document is often necessary to ensure the content remains accurate, updated, and structured according to specific needs. Common reasons include correcting outdated or incorrect data, updating formatting for better readability or consistency, converting scanned or unstructured tables into editable and searchable formats, and aligning table structures with new templates or standards. This process is especially useful in automated document processing systems, where maintaining high data quality and clarity is essential for reporting, analysis, and compliance. Replace Tables in 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.