HTML
JPG
PDF
XML
DOCX
PDF
如何通过 Node.js SDK 替换 PDF 中的表格
为了替换 PDF 中的表格,我们将使用 Aspose.PDF Cloud Node.js SDK。这个云端 SDK 帮助 Node.js 程序员使用 Aspose.PDF REST API 开发基于云的 PDF 创建、注释、编辑和转换应用程序。只需在 Aspose for Cloud 创建一个账户并获取您的应用信息。一旦您拥有 App SID 和密钥,您就可以使用 Aspose.PDF Cloud Node.js SDK。
软件包管理器控制台命令
npm install asposepdfcloud --save
通过云端 Node.js 替换 PDF 中表格的步骤
Aspose.PDF Cloud 开发者可以轻松地在几行代码中加载和替换 PDF 中的表格。
- 读取本地 PDF 文件。
- 将本地 PDF 上传到 Aspose Cloud。
- 检索并显示所有检测到的表格。
- 通过其 ID 检索并显示一个表格。
- 从 Aspose PDF Cloud 存储下载更新后的 PDF 文件。
使用 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 文件中的表格并测试功能。