HTML JPG PDF XML DOCX
  Product Family
PDF

Node.js SDK で PDF 内のテーブルを置換

クラウド Node.js SDK を使用して PDF ドキュメント内のテーブルを操作するための API

Get Started

Node.js SDK を使用して PDF 内のテーブルを置換する方法

PDF 内のテーブルを置換するには、 Aspose.PDF Cloud Node.js SDK を使用します。このクラウド SDK は、Node.js プログラマーがクラウドベースの PDF 作成、注釈付け、編集、変換アプリを Node.js プログラミング言語を使用して Aspose.PDF REST API 経由で開発するのを支援します。Aspose for Cloud でアカウントを作成し、アプリケーション情報を取得してください。App SID とキーを入手したら、Aspose.PDF Cloud Node.js SDK を利用する準備が整います。

パッケージ マネージャー コンソール コマンド


     
    npm install asposepdfcloud --save
     
     

Cloud Node.js を介して PDF 内のテーブルを置換する手順

Aspose.PDF Cloud 開発者は、数行のコードで簡単に PDF 内のテーブルをロードして置換できます。

  1. ローカルの PDF ファイルを読み取ります。
  2. ローカル PDF を Aspose Cloud にアップロードします。
  3. 検出されたすべてのテーブルを取得して表示します。
  4. ID による 1 つのテーブルを取得して表示します。
  5. 更新された PDF ファイルを Aspose PDF Cloud ストレージからダウンロードします。
 

Node.js を使用して PDF 内のテーブルを置換


    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);
        }
    }
 

PDF 内のテーブルを置換

PDF ドキュメント内のテーブルを置換することは、コンテンツを正確に保ち、更新し、特定のニーズに応じて構造化するためにしばしば必要です。一般的な理由には、古いデータや誤ったデータの修正、読みやすさや一貫性を向上させるためのフォーマットの更新、スキャンされたまたは非構造化のテーブルを編集可能で検索可能な形式に変換すること、新しいテンプレートや標準とテーブル構造を一致させることが含まれます。このプロセスは、自動化されたドキュメント処理システムにおいて、報告、分析、コンプライアンスのために高いデータ品質と明確さを維持するのに特に有用です。 Aspose.PDF Cloud Node.js SDK を使用して PDF ドキュメント内のテーブルを置換します。

Node.js ライブラリを使用して次のことができます:

  • PDF ドキュメントのヘッダーとフッターをテキストまたは画像形式で追加します。
  • PDF ドキュメントにテーブルとスタンプ(テキストまたは画像)を追加します。
  • 複数の PDF ドキュメントを既存のファイルに追加します。
  • PDF の添付ファイル、注釈、およびフォームフィールドを操作します。
  • PDF ドキュメントに暗号化または復号化を適用し、パスワードを設定します。
  • ページまたは PDF ドキュメント全体からすべてのスタンプとテーブルを削除します。
  • ID によって PDF ドキュメントから特定のスタンプまたはテーブルを削除します。
  • PDF ページまたはドキュメント全体からテキストの単一または複数のインスタンスを置換します。
  • PDF ドキュメントをさまざまな他のファイル形式に変換するための広範なサポート。
  • PDF ファイルのさまざまな要素を抽出し、PDF ドキュメントを最適化します。
  • 無料アプリを試して、オンラインで PDF ファイルにテーブルを抽出し、機能をテストできます。