Skip to content

Commit e2e61b3

Browse files
authored
Merge pull request #117 from aspose-pdf-cloud/PPDFCLOUD-4755-added-use-cases-for-Spliter
PDFCLOUD-4755: added use cases for Spliter
2 parents a6980b7 + 4a9bd26 commit e2e61b3

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

UsesCases/Split/splitPages.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
};
10+
11+
const pdfApi = new PdfApi(credentials.id, credentials.key);
12+
13+
const pdfSplitter = {
14+
async uploadDocument () {
15+
const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
16+
const pdfFileData = await fs.readFile(fileNamePath);
17+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
18+
console.log("Source document uploaded!");
19+
},
20+
21+
async downloadPages (pageHref, index) {
22+
const changedPdfData = await pdfApi.downloadFile(pageHref);
23+
const filePath = path.join(configParams.LOCAL_FOLDER, 'Page_' + index +'_' + pageHref);
24+
await fs.writeFile(filePath, changedPdfData.body);
25+
console.log("Downloaded: " + filePath);
26+
},
27+
28+
async splitPages () {
29+
const resultPages = await pdfApi.postSplitDocument(configParams.PDF_DOCUMENT_NAME, "pdf");
30+
31+
if (resultPages.body.code == 200 && resultPages.body.result.documents) {
32+
resultPages.body.result.documents.forEach(async (docPage, index) => {
33+
await this.downloadPages(docPage.href, index);
34+
})
35+
}
36+
else
37+
console.error("Unexpected error : can't get splitted documents!!!");
38+
},
39+
40+
41+
}
42+
43+
async function main() {
44+
try {
45+
await pdfSplitter.uploadDocument();
46+
await pdfSplitter.splitPages();
47+
} catch (error) {
48+
console.error("Error:", error.message);
49+
}
50+
}
51+
52+
main();

UsesCases/Split/splitRange.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 { SplitRangePdfOptions } from "../../src/models/splitRangePdfOptions.js";
6+
import { PageRange } from "../../src/models/pageRange.js";
7+
8+
const configParams = {
9+
LOCAL_FOLDER: "C:\\Samples\\",
10+
PDF_DOCUMENT_NAME: "sample.pdf",
11+
};
12+
13+
const range_1 = new PageRange();
14+
range_1.from = 1;
15+
range_1.to = 3;
16+
17+
const range_2 = new PageRange();
18+
range_2.from = 4;
19+
range_2.to = 7;
20+
21+
const splitRangesArray = new SplitRangePdfOptions();
22+
splitRangesArray.pageRanges = [ range_1, range_2 ];
23+
24+
const pdfApi = new PdfApi(credentials.id, credentials.key);
25+
26+
const pdfSplitter = {
27+
async uploadDocument () {
28+
const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
29+
const pdfFileData = await fs.readFile(fileNamePath);
30+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
31+
console.log("Source document uploaded!");
32+
},
33+
34+
async downloadPages (pageHref, index) {
35+
const changedPdfData = await pdfApi.downloadFile(pageHref);
36+
const filePath = path.join(configParams.LOCAL_FOLDER, 'Page_' + index +'_' + pageHref);
37+
await fs.writeFile(filePath, changedPdfData.body);
38+
console.log("Downloaded: " + filePath);
39+
},
40+
41+
async splitRanges () {
42+
const resultPages = await pdfApi.postSplitRangePdfDocument(configParams.PDF_DOCUMENT_NAME, splitRangesArray);
43+
44+
if (resultPages.body.code == 200 && resultPages.body.result.documents) {
45+
resultPages.body.result.documents.forEach(async (docPage, index) => {
46+
await this.downloadPages(docPage.href, index);
47+
})
48+
}
49+
else
50+
console.error("Unexpected error : can't get splitted documents!!!");
51+
},
52+
53+
54+
}
55+
56+
async function main() {
57+
try {
58+
await pdfSplitter.uploadDocument();
59+
await pdfSplitter.splitRanges();
60+
} catch (error) {
61+
console.error("Error:", error.message);
62+
}
63+
}
64+
65+
main();

0 commit comments

Comments
 (0)