HTML
JPG
PDF
XML
DOCX
PDF
如何通过 Node.js SDK 向 PDF 添加表格
要向 PDF 添加表格,我们将使用 Aspose.PDF Cloud Node.js SDK。这个云 SDK 协助 Node.js 程序员使用 Node.js 编程语言通过 Aspose.PDF REST API 开发基于云的 PDF 创建、注释、编辑和转换应用程序。只需在 Aspose for Cloud 创建一个账户并获取您的应用信息。一旦您拥有了应用 SID 和密钥,您就可以使用 Aspose.PDF Cloud Node.js SDK。
包管理器控制台命令
npm install asposepdfcloud --save
通过云端 Node.js 向 PDF 添加表格的步骤
Aspose.PDF Cloud 开发人员可以轻松地加载和添加表格到 PDF,只需几行代码。
- 读取本地 PDF 文件。
- 将 PDF 文件上传到 Aspose PDF Cloud 存储。
- 创建所需布局的表格。
- 从 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_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);
}
}
在 PDF 中处理表格
表格为系统地呈现数据提供了结构化的格式,使读者更容易理解和分析信息。它们还提高了文档的视觉吸引力,增加了专业性和组织性。当处理数值或比较数据时,表格通过将相关信息分组为易于阅读的格式来提高清晰度。此外,表格可以结合实时或动态生成的内容,例如来自数据库或分析仪表板的数据。 使用 Aspose.PDF Cloud Node.js SDK 将表格添加到 PDF 文档中。
使用我们的 Node.js 库,您可以:
- 以文本或图像格式添加 PDF 文档的页眉和页脚。
- 向 PDF 文档添加表格和印章(文本或图像)。
- 将多个 PDF 文档附加到现有文件。
- 处理 PDF 附件、注释和表单字段。
- 对 PDF 文档应用加密或解密并设置密码。
- 删除页面或整个 PDF 文档中的所有印章和表格。
- 通过其 ID 从 PDF 文档中删除特定印章或表格。
- 替换 PDF 页面或整个文档中的单个或多个文本实例。
- 广泛支持将 PDF 文档转换为多种其他文件格式。
- 提取 PDF 文件的各种元素并优化 PDF 文档。
- 您可以试用我们的免费应用,在线提取 PDF 文件中的表格并测试功能。