Skip to content

Commit f70472b

Browse files
authored
Merge pull request #113 from aspose-pdf-cloud/pdfapps-6682-added-uses-cases-for-tables
PDFAPPS-6682: added use cases for Tables
2 parents 3659487 + d06f60d commit f70472b

File tree

4 files changed

+450
-0
lines changed

4 files changed

+450
-0
lines changed

UsesCases/Tables/add/appendTable.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import credentials from "../../../../Credentials/credentials.json" with { type: "json" };
2+
import fs from 'node:fs/promises';
3+
import path from 'node:path';
4+
import { PdfApi } from "../../../src/api/api.js";
5+
import { Table } from "../../../src/models/table.js";
6+
import { Cell } from "../../../src/models/cell.js";
7+
import { FontStyles } from "../../../src/models/fontStyles.js";
8+
import { GraphInfo } from "../../../src/models/graphInfo.js";
9+
import { Row } from "../../../src/models/row.js";
10+
import { TextRect } from "../../../src/models/textRect.js";
11+
12+
const configParams = {
13+
LOCAL_FOLDER: "C:\\Samples\\",
14+
PDF_DOCUMENT_NAME: "sample.pdf",
15+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
16+
PAGE_NUMBER: 2, // Your document page number...
17+
TABLE_ROWS: 5,
18+
TABLE_COLUMNS: 5,
19+
};
20+
21+
const pdfApi = new PdfApi(credentials.id, credentials.key);
22+
23+
const pdfTables = {
24+
async uploadDocument () {
25+
const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
26+
const pdfFileData = await fs.readFile(fileNamePath);
27+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
28+
console.log("File '" + configParams.PDF_DOCUMENT_NAME + "' successfully uploaded!");
29+
},
30+
31+
async downloadResult () {
32+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
33+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
34+
await fs.writeFile(filePath, changedPdfData.body);
35+
console.log("Downloaded: " + filePath);
36+
},
37+
38+
initTable () {
39+
const numOfCols = configParams.TABLE_COLUMNS;
40+
const numOfRows = configParams.TABLE_ROWS;
41+
42+
const headerTextState = {
43+
font: "Arial Bold",
44+
fontSize: 11,
45+
foregroundColor: { a: 0xFF, r: 0xFF, g: 0xFF, b: 0xFF },
46+
fontStyle: FontStyles.Bold,
47+
};
48+
49+
const commonTextState = {
50+
font: "Arial Bold",
51+
fontSize: 11,
52+
foregroundColor: { a: 0xFF, r: 0x70, g: 0x70, b: 0x70 },
53+
};
54+
55+
const table = new Table();
56+
table.rows = [];
57+
58+
let colWidths = "";
59+
for (let colIndex = 0; colIndex < numOfCols; colIndex++)
60+
{
61+
colWidths += " 70";
62+
}
63+
64+
table.columnWidths = colWidths;
65+
66+
const borderTableBorder = new GraphInfo();
67+
borderTableBorder.color = { a: 0xFF, r: 0x00, g: 0xFF, b: 0x00 };
68+
borderTableBorder.lineWidth = 0.5;
69+
70+
table.defaultCellBorder = {
71+
top: borderTableBorder,
72+
right: borderTableBorder,
73+
bottom: borderTableBorder,
74+
left: borderTableBorder,
75+
roundedBorderRadius: 0
76+
};
77+
table.left = 150;
78+
table.top = 250;
79+
80+
for (let rowIndex = 0; rowIndex < numOfRows; rowIndex++)
81+
{
82+
const row = new Row();
83+
84+
row.cells = [];
85+
86+
for (let colIndex = 0; colIndex < numOfCols; colIndex++)
87+
{
88+
const cell = new Cell();
89+
90+
cell.defaultCellTextState = commonTextState;
91+
92+
if (rowIndex == 0) // header cells
93+
{
94+
cell.backgroundColor = { a: 0xFF, r: 0x80, g: 0x80, b: 0x80 };
95+
cell.defaultCellTextState = headerTextState;
96+
}
97+
else {
98+
cell.backgroundColor = { a: 0xFF, r: 0xFF, g: 0xFF, b: 0xFF };
99+
};
100+
101+
const textRect = new TextRect();
102+
if (rowIndex == 0)
103+
textRect.text = "header #" + colIndex;
104+
else
105+
textRect.text = "value #'(" + rowIndex + "," + colIndex + "')";
106+
cell.paragraphs = [textRect];
107+
108+
row.cells.push(cell);
109+
}
110+
table.rows.push(row);
111+
}
112+
return table;
113+
},
114+
115+
async addTableOnPage () {
116+
const jsonTable = this.initTable();
117+
118+
const resultTabs = await pdfApi.postPageTables(configParams.PDF_DOCUMENT_NAME, configParams.PAGE_NUMBER, [ jsonTable ]);
119+
120+
if (resultTabs.body.code == 200) {
121+
console.log("Table successfully added!");
122+
return resultTabs.body.table;
123+
}
124+
else
125+
comsole.error("Unexpected error : can't get links!!!");
126+
},
127+
}
128+
129+
async function main() {
130+
try {
131+
await pdfTables.uploadDocument();
132+
await pdfTables.addTableOnPage();
133+
await pdfTables.downloadResult();
134+
} catch (error) {
135+
console.error("Error:", error.message);
136+
}
137+
}
138+
139+
main();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import credentials from "../../../../Credentials/credentials.json" with { type: "json" };
2+
import fs from 'node:fs/promises';
3+
import path from 'node:path';
4+
import { PdfApi } from "../../../src/api/api.js";
5+
6+
const configParams = {
7+
LOCAL_FOLDER: "C:\\Samples\\",
8+
PDF_DOCUMENT_NAME: "sample.pdf",
9+
PAGE_NUMBER: 2, // Your document page number...
10+
TABLE_ID: "GE5TCOZSGAYCYNRQGUWDINZVFQ3DGMA", // Your table id...
11+
};
12+
13+
const pdfApi = new PdfApi(credentials.id, credentials.key);
14+
15+
const pdfTables = {
16+
async uploadDocument () {
17+
const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
18+
const pdfFileData = await fs.readFile(fileNamePath);
19+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
20+
},
21+
22+
async getAllTables () {
23+
const resultTabs = await pdfApi.getDocumentTables(configParams.PDF_DOCUMENT_NAME);
24+
25+
if (resultTabs.body.code == 200 && resultTabs.body.tables) {
26+
if (!Array.isArray(resultTabs.body.tables.list) || resultTabs.body.tables.list.length === 0) {
27+
throw new Error("Unexpected error : tables is null or empty!!!");
28+
}
29+
this.showTablesInfo(resultTabs.body.tables.list, "All tables");
30+
return resultTabs.body.tables.list;
31+
}
32+
else
33+
console.error("Unexpected error : can't get links!!!");
34+
},
35+
36+
async getTableById () {
37+
const resultTabs = await pdfApi.getTable(configParams.PDF_DOCUMENT_NAME, configParams.TABLE_ID);
38+
39+
if (resultTabs.body.code == 200 && resultTabs.body.table) {
40+
this.showTablesInfo( [ resultTabs.body.table ], "Table by Id");
41+
return resultTabs.body.table;
42+
}
43+
else
44+
console.error("Unexpected error : can't get links!!!");
45+
},
46+
47+
showTablesInfo(tables, prefix) {
48+
if (Array.isArray(tables) && tables.length > 0)
49+
{
50+
tables.forEach(function(table) {
51+
console.log(prefix +" => id: '" + table.id + "', page: '" + table.pageNum + "', rows: '" + table.rowList.length + "', columns: '" + table.rowList[0].cellList.length + "'");
52+
});
53+
}
54+
else
55+
console.error("showBoormarks() error: array of tables is empty!")
56+
},
57+
}
58+
59+
async function main() {
60+
try {
61+
await pdfTables.uploadDocument();
62+
await pdfTables.getAllTables();
63+
await pdfTables.getTableById();
64+
} catch (error) {
65+
console.error("Error:", error.message);
66+
}
67+
}
68+
69+
main();
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import credentials from "../../../../Credentials/credentials.json" with { type: "json" };
2+
import fs from 'node:fs/promises';
3+
import path from 'node:path';
4+
import { PdfApi } from "../../../src/api/api.js"
5+
6+
const configParams = {
7+
LOCAL_FOLDER: "C:\\Samples\\",
8+
PDF_DOCUMENT_NAME: "sample.pdf",
9+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
10+
PAGE_NUMBER: 1, // Your document page number...
11+
TABLE_ID: "GE5TCOZSGAYCYNRQGUWDINZVFQ3DGMA", // Your table id...
12+
};
13+
14+
const pdfApi = new PdfApi(credentials.id, credentials.key);
15+
16+
const pdfTables = {
17+
async uploadDocument () {
18+
const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
19+
const pdfFileData = await fs.readFile(fileNamePath);
20+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
21+
console.log("File '" + configParams.PDF_DOCUMENT_NAME + "' successfully uploaded!");
22+
},
23+
24+
async downloadResult () {
25+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
26+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
27+
await fs.writeFile(filePath, changedPdfData.body);
28+
console.log("Downloaded: " + filePath);
29+
},
30+
31+
async getAllTables (prefix) {
32+
const resultTabs = await pdfApi.getDocumentTables(configParams.PDF_DOCUMENT_NAME);
33+
34+
if (resultTabs.body.code == 200 && resultTabs.body.tables) {
35+
if (!Array.isArray(resultTabs.body.tables.list) || resultTabs.body.tables.list.length === 0)
36+
console.log(prefix + " => Unexpected error : tables is null or empty!!!");
37+
else
38+
this.showTablesInfo(resultTabs.body.tables.list, prefix);
39+
}
40+
else
41+
console.error(prefix + " => Unexpected error : can't get tables!!!");
42+
},
43+
44+
async deleteTable () {
45+
const resultTabs = await pdfApi.deleteTable(configParams.PDF_DOCUMENT_NAME, configParams.TABLE_ID);
46+
if (resultTabs.body.code == 200) {
47+
console.log("Table #" + configParams.TABLE_ID + " deleted!");
48+
return true;
49+
}
50+
else
51+
console.error("Unexpected error : can't delete table!");
52+
},
53+
54+
async deleteTables () {
55+
const resultTabs = await pdfApi.deletePageTables(configParams.PDF_DOCUMENT_NAME, configParams.PAGE_NUMBER);
56+
57+
if (resultTabs.body.code == 200) {
58+
console.log("Tables on page #" + configParams.PAGE_NUMBER + " deleted!");
59+
return true;
60+
}
61+
else
62+
throw new Error("Unexpected error : can't get tables!!!");
63+
},
64+
65+
showTablesInfo (tables, prefix) {
66+
if (Array.isArray(tables) && tables.length > 0)
67+
{
68+
tables.forEach(function(table) {
69+
console.log(prefix +" => id: '" + table.id + "', page: '" + table.pageNum + "', rows: '" + table.rowList.length + "', columns: '" + table.rowList[0].cellList.length + "'");
70+
});
71+
}
72+
else
73+
console.error("showTablesInfo() error: array of tables is empty!")
74+
},
75+
}
76+
77+
async function main() {
78+
try {
79+
await pdfTables.uploadDocument();
80+
81+
await pdfTables.getAllTables("All tables");
82+
await pdfTables.deleteTable();
83+
await pdfTables.getAllTables("Tables after drop one");
84+
85+
await pdfTables.deleteTables(configParams.PAGE_NUMBER);
86+
await pdfTables.getAllTables("Tables after drop all");
87+
88+
await pdfTables.downloadResult();
89+
} catch (error) {
90+
console.error("Error:", error.message);
91+
}
92+
}
93+
94+
main();

0 commit comments

Comments
 (0)