Skip to content

Commit 2658c67

Browse files
authored
Merge pull request #111 from aspose-pdf-cloud/pdfapps-6680-added-use-cases-for-pages
PDFAPPS-6680: added use cases for Pages
2 parents b88be6d + 5c9e95f commit 2658c67

File tree

7 files changed

+480
-0
lines changed

7 files changed

+480
-0
lines changed

UsesCases/Pages/add/addPage.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// 1. Load your Application Secret and Key from the JSON file or set credentials in another way
2+
// 2. Create an object to connect to the Pdf.Cloud API
3+
// 3. Upload your document file
4+
// 4. Create a new Link Annotation with the required properties
5+
// 5. Append new Link Annotation to the document using postPageLinkAnnotations() function
6+
// 6. Perform some action after successful addition
7+
// All values of variables starting with "YOUR_****" should be replaced by real user values
8+
9+
import credentials from "./credentials.json" with { type: "json" };
10+
import fs from 'node:fs/promises';
11+
import path from 'node:path';
12+
import { PdfApi } from "asposepdfcloud";
13+
14+
const configParams = {
15+
LOCAL_FOLDER: "C:\\Samples\\",
16+
PDF_DOCUMENT_NAME: "sample.pdf",
17+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf"
18+
};
19+
20+
const pdfApi = new PdfApi(credentials.id, credentials.key);
21+
22+
const pdfPages = {
23+
async uploadDocument () {
24+
const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
25+
const pdfFileData = await fs.readFile(fileNamePath);
26+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
27+
},
28+
29+
async downloadResult () {
30+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
31+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
32+
await fs.writeFile(filePath, changedPdfData.body);
33+
console.log("Downloaded: " + filePath);
34+
},
35+
36+
async addPage () {
37+
const resultPages = await pdfApi.putAddNewPage(configParams.PDF_DOCUMENT_NAME);
38+
39+
if (resultPages.body.code == 200 && resultPages.body.pages) {
40+
this.showPages( [ resultPages.body.pages.list[resultPages.body.pages.list.length - 1] ], "new page");
41+
return resultPages.body.pages.list[resultPages.body.pages.list.length - 1];
42+
}
43+
else
44+
console.error("Unexpected error : can't get pages!!!");
45+
},
46+
47+
showPages (pages, prefix) {
48+
if (Array.isArray(pages) && pages.length > 0)
49+
{
50+
pages.forEach(function(page) {
51+
console.log(prefix +" => id: '" + page.id + "', lLx: '" + page.rectangle.lLX + "', lLY: '" + page.rectangle.lLY + "', uRX: '" + page.rectangle.uRX + "', uRY: '" + page.rectangle.uRY + "'");
52+
});
53+
}
54+
else
55+
console.error("showPages() error: array of pages is empty!")
56+
},
57+
}
58+
59+
async function main() {
60+
try {
61+
await pdfPages.uploadDocument();
62+
await pdfPages.addPage();
63+
await pdfPages.downloadResult();
64+
} catch (error) {
65+
console.error("Error:", error.message);
66+
}
67+
}
68+
69+
main();
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// 1. Load your Application Secret and Key from the JSON file or set credentials in another way
2+
// 2. Create an object to connect to the Pdf.Cloud API
3+
// 3. Upload your document file
4+
// 4. Retrieve required Link Annotation from the document using getPageLinkAnnotation() function
5+
// 6. Perform some action after successful retrieving the Link Annotation from document
6+
// All values of variables starting with "YOUR_****" should be replaced by real user values
7+
8+
import credentials from "./credentials.json" with { type: "json" };
9+
import fs from 'node:fs/promises';
10+
import path from 'node:path';
11+
import { PdfApi } from "asposepdfcloud";
12+
13+
const configParams = {
14+
LOCAL_FOLDER: "C:\\Samples\\",
15+
PDF_DOCUMENT_NAME: "sample.pdf",
16+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
17+
PAGE_NUMBER: 2, // Your document page number...
18+
};
19+
20+
const pdfApi = new PdfApi(credentials.id, credentials.key);
21+
22+
const pdfPages = {
23+
async uploadDocument () {
24+
const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
25+
const pdfFileData = await fs.readFile(fileNamePath);
26+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
27+
},
28+
29+
async downloadResult () {
30+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
31+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
32+
await fs.writeFile(filePath, changedPdfData.body);
33+
console.log("Downloaded: " + filePath);
34+
},
35+
36+
async getPagesInfo () {
37+
const resultPages = await pdfApi.getPages(configParams.PDF_DOCUMENT_NAME);
38+
39+
if (resultPages.body.code == 200 && resultPages.body.pages.list) {
40+
this.showPages(resultPages.body.pages.list, "pages");
41+
return resultPages.body.pages.list;
42+
}
43+
else
44+
console.error("Unexpected error : can't get pages!!!");
45+
},
46+
47+
async getPageInfo (pageNumber) {
48+
const resultPages = await pdfApi.getPage(configParams.PDF_DOCUMENT_NAME, pageNumber);
49+
50+
if (resultPages.body.code == 200 && resultPages.body.page) {
51+
this.showPages( [ resultPages.body.page ], "page");
52+
return resultPages.body.page;
53+
}
54+
else
55+
console.error("Unexpected error : can't get pages!!!");
56+
},
57+
58+
showPages (pages, prefix) {
59+
if (Array.isArray(pages) && pages.length > 0)
60+
{
61+
pages.forEach(function(page) {
62+
console.log(prefix +" => id: '" + page.id + "', lLx: '" + page.rectangle.lLX + "', lLY: '" + page.rectangle.lLY + "', uRX: '" + page.rectangle.uRX + "', uRY: '" + page.rectangle.uRY + "'");
63+
});
64+
}
65+
else
66+
console.error("showPages() error: array of pages is empty!")
67+
},
68+
}
69+
70+
async function main() {
71+
try {
72+
await pdfPages.uploadDocument();
73+
await pdfPages.getPagesInfo();
74+
await pdfPages.getPageInfo(configParams.PAGE_NUMBER);
75+
} catch (error) {
76+
console.error("Error:", error.message);
77+
}
78+
}
79+
80+
main();

UsesCases/Pages/move/movePage.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// 1. Load your Application Secret and Key from the JSON file or set credentials in another way
2+
// 2. Create an object to connect to the Pdf.Cloud API
3+
// 3. Upload your document file
4+
// 4. Create a new Link Annotation with the required properties
5+
// 5. Append new Link Annotation to the document using postPageLinkAnnotations() function
6+
// 6. Perform some action after successful addition
7+
// All values of variables starting with "YOUR_****" should be replaced by real user values
8+
9+
import credentials from "./credentials.json" with { type: "json" };
10+
import fs from 'node:fs/promises';
11+
import path from 'node:path';
12+
import { PdfApi } from "asposepdfcloud";
13+
14+
const configParams = {
15+
LOCAL_FOLDER: "C:\\Samples\\",
16+
PDF_DOCUMENT_NAME: "sample.pdf",
17+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
18+
PAGE_NUMBER: 2, // Your document page number...
19+
};
20+
21+
const pdfApi = new PdfApi(credentials.id, credentials.key);
22+
23+
const pdfPages = {
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+
},
29+
30+
async downloadResult () {
31+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
32+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
33+
await fs.writeFile(filePath, changedPdfData.body);
34+
console.log("Downloaded: " + filePath);
35+
},
36+
37+
async movePage (pageNumber, newPageNumber) {
38+
const resultPages = await pdfApi.postMovePage(configParams.PDF_DOCUMENT_NAME, pageNumber, newPageNumber);
39+
40+
if (resultPages.body.code == 200) {
41+
console.log("Page #" + pageNumber + " moved to #" + newPageNumber + " !");
42+
return true;
43+
}
44+
else
45+
console.error("Unexpected error : can't move page!!!");
46+
},
47+
}
48+
49+
async function main() {
50+
try {
51+
await pdfPages.uploadDocument();
52+
await pdfPages.movePage(configParams.PAGE_NUMBER, configParams.PAGE_NUMBER + 1);
53+
await pdfPages.downloadResult();
54+
} catch (error) {
55+
console.error("Error:", error.message);
56+
}
57+
}
58+
59+
main();

UsesCases/Pages/remove/removePage.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// 1. Load your Application Secret and Key from the JSON file or set credentials in another way
2+
// 2. Create an object to connect to the Pdf.Cloud API
3+
// 3. Upload your document file
4+
// 4. Delete required Link Annotation from the document using deleteLinkAnnotation() function
5+
// 6. Perform some action after successful removing the Link Annotation from document
6+
// All values of variables starting with "YOUR_****" should be replaced by real user values
7+
8+
import credentials from "./credentials.json" with { type: "json" };
9+
import fs from 'node:fs/promises';
10+
import path from 'node:path';
11+
import { PdfApi } from "asposepdfcloud";
12+
13+
const configParams = {
14+
LOCAL_FOLDER: "C:\\Samples\\",
15+
PDF_DOCUMENT_NAME: "sample.pdf",
16+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
17+
PAGE_NUMBER: 2, // Your document page number...
18+
};
19+
20+
const pdfApi = new PdfApi(credentials.id, credentials.key);
21+
22+
const pdfPages = {
23+
async uploadDocument () {
24+
const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
25+
const pdfFileData = await fs.readFile(fileNamePath);
26+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
27+
},
28+
29+
async downloadResult () {
30+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
31+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
32+
await fs.writeFile(filePath, changedPdfData.body);
33+
console.log("Downloaded: " + filePath);
34+
},
35+
36+
async deletePage () {
37+
const resultPages = await pdfApi.deletePage(configParams.PDF_DOCUMENT_NAME, configParams.PAGE_NUMBER);
38+
39+
if (resultPages.body.code == 200) {
40+
console.log("Page #" + configParams.PAGE_NUMBER + " - deleted!");
41+
return true;
42+
}
43+
else
44+
console.error("Unexpected error : can't get pages!!!");
45+
},
46+
47+
}
48+
49+
async function main() {
50+
try {
51+
await pdfPages.uploadDocument();
52+
await pdfPages.deletePage();
53+
await pdfPages.downloadResult();
54+
} catch (error) {
55+
console.error("Error:", error.message);
56+
}
57+
}
58+
59+
main();
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// 1. Load your Application Secret and Key from the JSON file or set credentials in another way
2+
// 2. Create an object to connect to the Pdf.Cloud API
3+
// 3. Upload your document file
4+
// 4. Create a new Text stamp for page with the required properties
5+
// 5. Append new Text stamp to the document page using putPageAddStamp() function
6+
// 6. Perform some action after successful addition
7+
// All values of variables starting with "YOUR_****" should be replaced by real user values
8+
9+
import credentials from "./credentials.json" with { type: "json" };
10+
import fs from 'node:fs/promises';
11+
import path from 'node:path';
12+
import { PdfApi } from "asposepdfcloud";
13+
import { Stamp } from "asposepdfcloud/src/models/stamp.js";
14+
15+
const configParams = {
16+
LOCAL_FOLDER: "C:\\Samples\\",
17+
PDF_DOCUMENT_NAME: "sample.pdf",
18+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
19+
PAGE_NUMBER: 2, // Your document page number...
20+
};
21+
22+
const pdfApi = new PdfApi(credentials.id, credentials.key);
23+
24+
const pdfPages = {
25+
async uploadDocument () {
26+
const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
27+
const pdfFileData = await fs.readFile(fileNamePath);
28+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
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+
async addPageTextStamp () {
39+
40+
const pageStamp = new Stamp();
41+
pageStamp.type = "Text";
42+
pageStamp.background = true;
43+
pageStamp.horizontalAlignment = "Center";
44+
pageStamp.textAlignment = "Center";
45+
pageStamp.value = "NEW TEXT STAMP";
46+
pageStamp.pageIndex = configParams.PAGE_NUMBER;
47+
48+
const resultPages = await pdfApi.putPageAddStamp(configParams.PDF_DOCUMENT_NAME, configParams.PAGE_NUMBER, pageStamp);
49+
50+
if (resultPages.body.code == 200) {
51+
console.log("Text Stamp added!");
52+
return true;
53+
}
54+
else
55+
console.error("Unexpected error : can't get pages!!!");
56+
},
57+
}
58+
59+
async function main() {
60+
try {
61+
await pdfPages.uploadDocument();
62+
await pdfPages.addPageTextStamp();
63+
await pdfPages.downloadResult();
64+
} catch (error) {
65+
console.error("Error:", error.message);
66+
}
67+
}
68+
69+
main();

0 commit comments

Comments
 (0)