Skip to content

Commit b88be6d

Browse files
authored
Merge pull request #110 from aspose-pdf-cloud/pdfapps-6679-added-use-cases-for-headers-footers
PDFAPPS-6679: added use cases for Headers/Footers
2 parents d70cbe2 + 508befb commit b88be6d

File tree

4 files changed

+275
-0
lines changed

4 files changed

+275
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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. Perform the retreiving link annotattions from Pdf document using getPageLinkAnnotations() function
5+
// 5. Check result and perform some actions with result.body object
6+
// 6. Create a new Link Annotation with the required properties
7+
// 7. Append new Link Annotation to the document using postPageLinkAnnotations() function
8+
// 8. Perform some action after successful addition
9+
// All values of variables starting with "YOUR_****" should be replaced by real user values
10+
11+
import credentials from "./credentials.json" with { type: "json" };
12+
import fs from 'node:fs/promises';
13+
import path from 'node:path';
14+
import { PdfApi } from "asposepdfcloud";
15+
import { ImageFooter } from "asposepdfcloud/src/models/imageFooter.js";
16+
import { HorizontalAlignment } from "asposepdfcloud/src/models/horizontalAlignment.js";
17+
18+
const configParams = {
19+
LOCAL_FOLDER: "C:\\Samples\\",
20+
PDF_DOCUMENT_NAME: "sample.pdf",
21+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
22+
IMAGE_FOOTER_FILE: "sample.png",
23+
PAGE_NUMBER: 2, // Your document page number...
24+
};
25+
26+
const pdfApi = new PdfApi(credentials.id, credentials.key);
27+
28+
const pdfHederFooter = {
29+
async uploadFile (fileName) {
30+
const pdfFileData = await fs.readFile(configParams.LOCAL_FOLDER + fileName);
31+
await pdfApi.uploadFile(fileName, pdfFileData);
32+
},
33+
34+
async uploadDocument () {
35+
await pdfHederFooter.uploadFile(configParams.PDF_DOCUMENT_NAME);
36+
},
37+
38+
async downloadResult() {
39+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
40+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
41+
await fs.writeFile(filePath, changedPdfData.body);
42+
console.log("Downloaded: " + filePath);
43+
},
44+
45+
async addImageFooter () {
46+
const imageFooter = new ImageFooter();
47+
imageFooter.background = true;
48+
imageFooter.value = "TEST FOOTER VALUE";
49+
imageFooter.horizontalAlignment = HorizontalAlignment.Center;
50+
imageFooter.fileName = configParams.IMAGE_FOOTER_FILE;
51+
imageFooter.width = 24;
52+
imageFooter.height = 24;
53+
54+
const resultLinks = await pdfApi.postDocumentImageFooter(configParams.PDF_DOCUMENT_NAME, imageFooter);
55+
56+
if (resultLinks.body.code == 200) {
57+
return resultLinks.body;
58+
}
59+
else
60+
throw new Error("Unexpected error : can't append Text Header!");
61+
},
62+
}
63+
64+
async function main() {
65+
try {
66+
await pdfHederFooter.uploadDocument();
67+
await pdfHederFooter.uploadFile(configParams.IMAGE_FOOTER_FILE);
68+
await pdfHederFooter.addImageFooter();
69+
await pdfHederFooter.downloadResult();
70+
} catch (error) {
71+
console.error("Error:", error.message);
72+
}
73+
}
74+
75+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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. Perform the retreiving link annotattions from Pdf document using getPageLinkAnnotations() function
5+
// 5. Check result and perform some actions with result.body object
6+
// 6. Create a new Link Annotation with the required properties
7+
// 7. Append new Link Annotation to the document using postPageLinkAnnotations() function
8+
// 8. Perform some action after successful addition
9+
// All values of variables starting with "YOUR_****" should be replaced by real user values
10+
11+
import credentials from "./credentials.json" with { type: "json" }; // json-file in this format: { "id": "*****", "key": "*******" }
12+
import fs from 'node:fs/promises';
13+
import path from 'node:path';
14+
import { PdfApi } from "asposepdfcloud";
15+
import { ImageHeader } from "asposepdfcloud/src/models/imageHeader.js";
16+
import { HorizontalAlignment } from "asposepdfcloud/src/models/horizontalAlignment.js";
17+
18+
const configParams = {
19+
LOCAL_FOLDER: "C:\\Samples\\",
20+
PDF_DOCUMENT_NAME: "sample.pdf",
21+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
22+
IMAGE_HEADER_FILE: "sample.png",
23+
PAGE_NUMBER: 2, // Your document page number...
24+
};
25+
26+
const pdfApi = new PdfApi(credentials.id, credentials.key);
27+
28+
const pdfHederFooter = {
29+
async uploadFile (fileName) {
30+
const pdfFileData = await fs.readFile(configParams.LOCAL_FOLDER + fileName);
31+
await pdfApi.uploadFile(fileName, pdfFileData);
32+
},
33+
34+
async uploadDocument () {
35+
await pdfHederFooter.uploadFile(configParams.PDF_DOCUMENT_NAME);
36+
},
37+
38+
async downloadResult() {
39+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
40+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
41+
await fs.writeFile(filePath, changedPdfData.body);
42+
console.log("Downloaded: " + filePath);
43+
},
44+
45+
async adddImageHeader () {
46+
const imageHeader = new ImageHeader();
47+
imageHeader.background = true;
48+
imageHeader.value = "TEST HEADER VALUE";
49+
imageHeader.horizontalAlignment = HorizontalAlignment.Center;
50+
imageHeader.fileName = configParams.IMAGE_HEADER_FILE;
51+
imageHeader.width = 24;
52+
imageHeader.height = 24;
53+
54+
const resultLinks = await pdfApi.postDocumentImageHeader(configParams.PDF_DOCUMENT_NAME, imageHeader);
55+
56+
if (resultLinks.body.code == 200) {
57+
return resultLinks.body;
58+
}
59+
else
60+
throw new Error("Unexpected error : can't append Text Header!");
61+
},
62+
}
63+
64+
async function main() {
65+
try {
66+
await pdfHederFooter.uploadDocument();
67+
await pdfHederFooter.uploadFile(configParams.IMAGE_HEADER_FILE);
68+
await pdfHederFooter.adddImageHeader();
69+
await pdfHederFooter.downloadResult();
70+
} catch (error) {
71+
console.error("Error:", error.message);
72+
}
73+
}
74+
75+
main();
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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. Perform the retreiving link annotattions from Pdf document using getPageLinkAnnotations() function
5+
// 5. Check result and perform some actions with result.body object
6+
// 6. Create a new Link Annotation with the required properties
7+
// 7. Append new Link Annotation to the document using postPageLinkAnnotations() function
8+
// 8. Perform some action after successful addition
9+
// All values of variables starting with "YOUR_****" should be replaced by real user values
10+
11+
import credentials from "./credentials.json" with { type: "json" };
12+
import fs from 'node:fs/promises';
13+
import path from 'node:path';
14+
import { PdfApi } from "asposepdfcloud";
15+
import { TextFooter } from "asposepdfcloud/src/models/textFooter.js";
16+
import { HorizontalAlignment } from "asposepdfcloud/src/models/horizontalAlignment.js";
17+
18+
const configParams = {
19+
LOCAL_FOLDER: "C:\\Samples\\",
20+
PDF_DOCUMENT_NAME: "sample.pdf",
21+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
22+
FOOTER_VALUE: "New Footer Value",
23+
};
24+
25+
const pdfApi = new PdfApi(credentials.id, credentials.key);
26+
27+
const pdfHederFooter = {
28+
async uploadDocument() {
29+
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
30+
const pdfFileData = await fs.readFile(pdfFilePath);
31+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
32+
},
33+
34+
async downloadResult() {
35+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
36+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
37+
await fs.writeFile(filePath, changedPdfData.body);
38+
console.log("Downloaded: " + filePath);
39+
},
40+
41+
async adddTextFooter () {
42+
const textFooter = new TextFooter();
43+
textFooter.background = true;
44+
textFooter.value = configParams.FOOTER_VALUE;
45+
textFooter.horizontalAlignment = HorizontalAlignment.Center;
46+
47+
await pdfApi.postDocumentTextFooter(configParams.PDF_DOCUMENT_NAME, textFooter);
48+
},
49+
}
50+
51+
async function main() {
52+
try {
53+
await pdfHederFooter.uploadDocument();
54+
await pdfHederFooter.adddTextFooter();
55+
await pdfHederFooter.downloadResult();
56+
} catch (error) {
57+
console.error("Error:", error.message);
58+
}
59+
}
60+
61+
main();
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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. Perform the retreiving link annotattions from Pdf document using getPageLinkAnnotations() function
5+
// 5. Check result and perform some actions with result.body object
6+
// 6. Create a new Link Annotation with the required properties
7+
// 7. Append new Link Annotation to the document using postPageLinkAnnotations() function
8+
// 8. Perform some action after successful addition
9+
// All values of variables starting with "YOUR_****" should be replaced by real user values
10+
11+
import credentials from "./credentials.json" with { type: "json" };
12+
import fs from 'node:fs/promises';
13+
import path from 'node:path';
14+
import { PdfApi } from "asposepdfcloud";
15+
import { TextHeader } from "asposepdfcloud/src/models/textHeader.js";
16+
import { HorizontalAlignment } from "asposepdfcloud/src/models/horizontalAlignment.js";
17+
18+
const configParams = {
19+
LOCAL_FOLDER: "C:\\Samples\\",
20+
PDF_DOCUMENT_NAME: "sample.pdf",
21+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
22+
HEADER_VALUE: "New Heder Value",
23+
};
24+
25+
const pdfApi = new PdfApi(credentials.id, credentials.key);
26+
27+
const pdfHederFooter = {
28+
async uploadFile (fileName) {
29+
const pdfFileData = await fs.readFile(configParams.LOCAL_FOLDER + fileName);
30+
await pdfApi.uploadFile(fileName, pdfFileData);
31+
},
32+
33+
async downloadResult() {
34+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
35+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
36+
await fs.writeFile(filePath, changedPdfData.body);
37+
console.log("Downloaded: " + filePath);
38+
},
39+
40+
async uploadDocument () {
41+
await pdfHederFooter.uploadFile(configParams.PDF_DOCUMENT_NAME);
42+
},
43+
44+
async adddTextHeader () {
45+
const textHeader = new TextHeader();
46+
textHeader.background = true;
47+
textHeader.value = configParams.HEADER_VALUE;
48+
textHeader.horizontalAlignment = HorizontalAlignment.Center;
49+
50+
await pdfApi.postDocumentTextHeader(configParams.PDF_DOCUMENT_NAME, textHeader);
51+
},
52+
}
53+
54+
async function main() {
55+
try {
56+
await pdfHederFooter.uploadDocument();
57+
await pdfHederFooter.adddTextHeader();
58+
await pdfHederFooter.downloadResult();
59+
} catch (error) {
60+
console.error("Error:", error.message);
61+
}
62+
}
63+
64+
main();

0 commit comments

Comments
 (0)