Skip to content

Commit d70cbe2

Browse files
authored
Merge pull request #109 from aspose-pdf-cloud/pdfapps-6675-added-use-cases-for-links
PDFAPPS-6675: added use cases for Links
2 parents 525f208 + a236687 commit d70cbe2

File tree

5 files changed

+356
-0
lines changed

5 files changed

+356
-0
lines changed

UsesCases/Links/add/appendLink.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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" }; // json-file in this format: { "id": "*****", "key": "*******" }
10+
import fs from 'node:fs/promises';
11+
import path from 'node:path';
12+
import { PdfApi } from "asposepdfcloud";
13+
import { Color } from "asposepdfcloud/src/models/color.js";
14+
import { Link } from "asposepdfcloud/src/models/link.js";
15+
import { Rectangle } from "asposepdfcloud/src/models/rectangle.js";
16+
import { LinkAnnotation } from "asposepdfcloud/src/models/linkAnnotation.js";
17+
import { LinkHighlightingMode} from "asposepdfcloud/src/models/linkHighlightingMode.js";
18+
import { LinkActionType } from "asposepdfcloud/src/models/linkActionType.js";
19+
20+
const configParams = {
21+
LOCAL_FOLDER: "C:\\Samples\\",
22+
PDF_DOCUMENT_NAME: "sample.pdf",
23+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
24+
NEW_LINK_ACTION: "https://reference.aspose.cloud/pdf/#/",
25+
PAGE_NUMBER: 2, // Your document page number...
26+
LINK_POS_LLX: 244.914,
27+
LINK_POS_LLY: 488.622,
28+
LINK_POS_URX: 284.776,
29+
LINK_POS_URY: 498.588,
30+
};
31+
32+
const pdfApi = new PdfApi(credentials.id, credentials.key);
33+
34+
const pdfLinks = {
35+
async uploadDocument() {
36+
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
37+
const pdfFileData = await fs.readFile(pdfFilePath);
38+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
39+
},
40+
41+
async downloadResult() {
42+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
43+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
44+
await fs.writeFile(filePath, changedPdfData.body);
45+
console.log("Downloaded: " + filePath);
46+
},
47+
48+
async appendLink () {
49+
const linkColor = new Color({ a: 255, r: 0, g: 255, b: 0 });
50+
51+
const linkRectangle = new Rectangle();
52+
linkRectangle.lLX = configParams.LINK_POS_LLX;
53+
linkRectangle.lLY = configParams.LINK_POS_LLY;
54+
linkRectangle.uRX = configParams.LINK_POS_URX;
55+
linkRectangle.uRY = configParams.LINK_POS_URY;
56+
57+
const linkItem = new Link({ rel: "self" });
58+
59+
const newLink = new LinkAnnotation();
60+
newLink.links = [ linkItem ];
61+
newLink.actionType = LinkActionType.GoToURIAction,
62+
newLink.action = configParams.NEW_LINK_ACTION,
63+
newLink.highlighting = LinkHighlightingMode.Invert,
64+
newLink.color = linkColor;
65+
newLink.rect = linkRectangle;
66+
67+
var addResponse = await pdfApi.postPageLinkAnnotations(configParams.PDF_DOCUMENT_NAME, configParams.PAGE_NUMBER, [ newLink ]);
68+
69+
if (addResponse.body.code == 200) {
70+
console.log("Append link successful!");
71+
return true;
72+
}
73+
},
74+
}
75+
76+
async function main() {
77+
try {
78+
await pdfLinks.uploadDocument();
79+
await pdfLinks.appendLink();
80+
await pdfLinks.downloadResult();
81+
} catch (error) {
82+
console.error("Error:", error.message);
83+
}
84+
}
85+
86+
main();

UsesCases/Links/get/getAllLinks.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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" }; // json-file in this format: { "id": "*****", "key": "*******" }
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+
LINK_FIND_ID: "GI5UO32UN5KVESKBMN2GS33OHMZTEMJMGUYDQLBTGYYCYNJSGE",
19+
};
20+
21+
const pdfApi = new PdfApi(credentials.id, credentials.key);
22+
23+
const pdfLinks = {
24+
async uploadDocument() {
25+
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
26+
const pdfFileData = await fs.readFile(pdfFilePath);
27+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
28+
},
29+
30+
async getAllLinks () {
31+
const resultLinks = await pdfApi.getPageLinkAnnotations(configParams.PDF_DOCUMENT_NAME, configParams.PAGE_NUMBER);
32+
33+
if (resultLinks.body.code == 200 && resultLinks.body.links.list) {
34+
this.showLinks(resultLinks.body.links.list, "all");
35+
return resultLinks.body.links.list;
36+
}
37+
else
38+
console.Error("Unexpected error : can't get links!!!");
39+
},
40+
41+
showLinks (links, prefix) {
42+
if (Array.isArray(links) && links.length > 0)
43+
{
44+
links.forEach(function(link) {
45+
console.log(prefix +" => '" + link.id + "', '" + link.action);
46+
});
47+
}
48+
},
49+
}
50+
51+
async function main() {
52+
try {
53+
await pdfLinks.uploadDocument();
54+
await pdfLinks.getAllLinks();
55+
} catch (error) {
56+
console.error("Error:", error.message);
57+
}
58+
};
59+
60+
main();

UsesCases/Links/get/getLinkById.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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" }; // json-file in this format: { "id": "*****", "key": "*******" }
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+
LINK_FIND_ID: "GI5UO32UN5KVESKBMN2GS33OHMZTEMJMGUYDQLBTGYYCYNJSGE",
19+
};
20+
21+
const pdfApi = new PdfApi(credentials.id, credentials.key);
22+
23+
const pdfLinks = {
24+
async uploadDocument() {
25+
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
26+
const pdfFileData = await fs.readFile(pdfFilePath);
27+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
28+
},
29+
30+
async getLinkById () {
31+
const resultLinks = await pdfApi.getPageLinkAnnotation(configParams.PDF_DOCUMENT_NAME, configParams.PAGE_NUMBER, configParams.LINK_FIND_ID);
32+
33+
if (resultLinks.body.code == 200 && resultLinks.body.link) {
34+
this.showLinks( [ resultLinks.body.link ], "found");
35+
return resultLinks.body.link;
36+
}
37+
else
38+
throw new Error("Unexpected error : can't get link !!!");
39+
},
40+
41+
showLinks (links, prefix) {
42+
if (Array.isArray(links) && links.length > 0)
43+
{
44+
links.forEach(function(link) {
45+
console.log(prefix +" => '" + link.id + "', '" + link.action);
46+
});
47+
}
48+
else
49+
console.error("showLinks() error: array of links is empty!")
50+
},
51+
}
52+
53+
async function main() {
54+
try {
55+
await pdfLinks.uploadDocument();
56+
await pdfLinks.getLinkById();
57+
} catch (error) {
58+
console.error("Error:", error.message);
59+
}
60+
};
61+
62+
main();

UsesCases/Links/remove/removeLink.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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" }; // json-file in this format: { "id": "*****", "key": "*******" }
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+
LINK_REMOVE_ID: "GI5UO32UN5KVESKBMN2GS33OHMZTEMJMGUYDQLBTGYYCYNJSGE",
18+
PAGE_NUMBER: 2, // Your document page number...
19+
};
20+
21+
const pdfApi = new PdfApi(credentials.id, credentials.key);
22+
23+
const pdfLinks = {
24+
async uploadDocument() {
25+
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
26+
const pdfFileData = await fs.readFile(pdfFilePath);
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+
removeLink: async function () {
38+
const resultDelete = await pdfApi.deleteLinkAnnotation(configParams.PDF_DOCUMENT_NAME, configParams.LINK_REMOVE_ID);
39+
40+
if (resultDelete.body.code == 200) {
41+
console.log("Link '" + configParams.LINK_REMOVE_ID + "' was deleted!");
42+
return ;
43+
}
44+
else
45+
throw new Error("Unexpected error : can't get link !!!");
46+
}
47+
48+
}
49+
50+
async function main() {
51+
try {
52+
await pdfLinks.uploadDocument();
53+
await pdfLinks.removeLink();
54+
await pdfLinks.downloadResult();
55+
} catch (error) {
56+
console.error("Error:", error.message);
57+
}
58+
}
59+
60+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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" }; // json-file in this format: { "id": "*****", "key": "*******" }
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+
LINK_REMOVE_ID: "GI5UO32UN5KVESKBMN2GS33OHMZTEMJMGUYDQLBTGYYCYNJSGE",
19+
NEW_LINK_ACTION: "https://reference.aspose.cloud/pdf/#/",
20+
PAGE_NUMBER: 2, // Your document page number...
21+
};
22+
23+
const pdfApi = new PdfApi(credentials.id, credentials.key);
24+
25+
const pdfLinks = {
26+
async uploadDocument() {
27+
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
28+
const pdfFileData = await fs.readFile(pdfFilePath);
29+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
30+
},
31+
32+
async downloadResult() {
33+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
34+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
35+
await fs.writeFile(filePath, changedPdfData.body);
36+
console.log("Downloaded: " + filePath);
37+
},
38+
39+
async replaceLink () {
40+
const link = await pdfLinks.getLinkById(configParams.LINK_REMOVE_ID);
41+
42+
link.action = configParams.NEW_LINK_ACTION;
43+
44+
var updResponse = await pdfApi.putLinkAnnotation(configParams.PDF_DOCUMENT_NAME, configParams.LINK_REMOVE_ID, link);
45+
46+
if (updResponse.body.code == 200 && updResponse.body.link) {
47+
pdfLinks.showLinks( [ updResponse.body.link ], "drop");
48+
return updResponse.body.link;
49+
}
50+
else
51+
console.Error("Unexpected error : can't append link!!!");
52+
},
53+
54+
async getLinkById () {
55+
const resultLinks = await pdfApi.getPageLinkAnnotation(configParams.PDF_DOCUMENT_NAME, configParams.PAGE_NUMBER, configParams.LINK_REMOVE_ID);
56+
57+
if (resultLinks.body.code == 200 && resultLinks.body.link) {
58+
this.showLinks( [ resultLinks.body.link ], "found");
59+
return resultLinks.body.link;
60+
}
61+
else
62+
console.Error("Unexpected error : can't get link !!!");
63+
},
64+
65+
showLinks (links, prefix) {
66+
if (Array.isArray(links) && links.length > 0)
67+
{
68+
links.forEach(function(link) {
69+
console.log(prefix +" => '" + link.id + "', '" + link.action);
70+
});
71+
}
72+
else
73+
console.error("showLinks() error: array of links is empty!")
74+
},
75+
76+
}
77+
78+
async function main() {
79+
try {
80+
await pdfLinks.uploadDocument();
81+
await pdfLinks.replaceLink();
82+
await pdfLinks.downloadResult();
83+
} catch (error) {
84+
console.error("Error:", error.message);
85+
}
86+
}
87+
88+
main();

0 commit comments

Comments
 (0)