Skip to content

Commit 525f208

Browse files
authored
Merge pull request #107 from aspose-pdf-cloud/PDFAPPS-6674-added-use-cases-for-Bookmarks
PDFAPPS-6674: added use cases for Bookmarks
2 parents e28af96 + c92b99c commit 525f208

File tree

4 files changed

+242
-0
lines changed

4 files changed

+242
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 Bookmarks with the required properties
5+
// 5. Append new Bookmarks to the document using postBookmark() 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 { Bookmark } from "asposepdfcloud/src/models/bookmark.js";
16+
import { Bookmarks } from "asposepdfcloud/src/models/bookmarks.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+
NEW_BOOKMARK_TITLE: "• Productivity improvement",
23+
PARENT_BOOKMARK_FOR_APPEND: "", //The parent bookmark path. Specify an empty string when adding a bookmark to the root.
24+
NEW_BOOKMARK_PAGE_NUMBER: 2,
25+
};
26+
27+
const pdfApi = new PdfApi(credentials.id, credentials.key);
28+
29+
const pdfBookmarks = {
30+
async uploadDocument() {
31+
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
32+
const pdfFileData = await fs.readFile(pdfFilePath);
33+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
34+
},
35+
36+
async downloadResult() {
37+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
38+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
39+
await fs.writeFile(filePath, changedPdfData.body);
40+
console.log("Downloaded: " + filePath);
41+
},
42+
43+
async appendBookmarkLink() {
44+
const bookmarkLink = new Link({ rel: "self" });
45+
const bookmarkColor = new Color({ a: 255, r: 0, g: 255, b: 0 });
46+
47+
const newBookmark = new Bookmark();
48+
newBookmark.title = configParams.NEW_BOOKMARK_TITLE;
49+
newBookmark.italic = true;
50+
newBookmark.bold = false;
51+
newBookmark.links = [bookmarkLink];
52+
newBookmark.color = bookmarkColor;
53+
newBookmark.action = "GoTo";
54+
newBookmark.level = 1;
55+
newBookmark.pageDisplayLeft = 83;
56+
newBookmark.pageDisplayTop = 751;
57+
newBookmark.pageDisplayZoom = 2;
58+
newBookmark.pageNumber = configParams.NEW_BOOKMARK_PAGE_NUMBER;
59+
60+
const response = await pdfApi.postBookmark(configParams.PDF_DOCUMENT_NAME, configParams.PARENT_BOOKMARK_FOR_APPEND, [newBookmark]);
61+
const { code, bookmarks } = response.body;
62+
63+
if (code === 200 && bookmarks) {
64+
const addedBookmark = bookmarks.list[bookmarks.list.length - 1];
65+
console.log("Appended bookmark: " + addedBookmark.links[0].href + " => " + addedBookmark.title);
66+
return addedBookmark;
67+
}
68+
},
69+
};
70+
71+
async function main() {
72+
try {
73+
await pdfBookmarks.uploadDocument();
74+
await pdfBookmarks.appendBookmarkLink();
75+
await pdfBookmarks.downloadResult();
76+
} catch (error) {
77+
console.error("Error:", error.message);
78+
}
79+
}
80+
81+
main();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 Bookmark from the document using getBookmark() function
5+
// 6. Perform some action after successful retrieving the Bookmark 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+
BOOKMARK_PATH: "/5",
17+
};
18+
19+
const pdfApi = new PdfApi(credentials.id, credentials.key);
20+
21+
const pdfBookmarks = {
22+
async uploadDocument() {
23+
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
24+
const pdfFileData = await fs.readFile(pdfFilePath);
25+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
26+
},
27+
28+
async getBookmarkByPath() {
29+
const resultBookmark = await pdfApi.getBookmark(configParams.PDF_DOCUMENT_NAME, configParams.BOOKMARK_PATH);
30+
const { code, bookmark } = resultBookmark.body;
31+
32+
console.log(`Found bookmark title: ${bookmark.title}`);
33+
return bookmark;
34+
},
35+
36+
};
37+
38+
async function main() {
39+
try {
40+
await pdfBookmarks.uploadDocument();
41+
await pdfBookmarks.getBookmarkByPath();
42+
} catch (error) {
43+
console.error("Error:", error.message);
44+
}
45+
}
46+
47+
main();
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 Bookmarks from the document using getBookmark() function
5+
// 6. Perform some action after successful retrieving the Bookmark 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+
BOOKMARK_PATH: "/5",
17+
};
18+
19+
const pdfApi = new PdfApi(credentials.id, credentials.key);
20+
21+
const pdfBookmarks = {
22+
async uploadDocument() {
23+
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
24+
const pdfFileData = await fs.readFile(pdfFilePath);
25+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
26+
},
27+
28+
async getAllBookmarks() {
29+
const resultBookmarks = await pdfApi.getDocumentBookmarks(configParams.PDF_DOCUMENT_NAME);
30+
const { code, bookmarks } = resultBookmarks.body;
31+
32+
this.showBookmarks(bookmarks, "all");
33+
return bookmarks;
34+
},
35+
36+
async showBookmarks(bookmarks, prefix) {
37+
if (Array.isArray(bookmarks.list) && bookmarks.list.length > 0) {
38+
for (const bookmark of bookmarks.list) {
39+
console.log(`${prefix} => level: '${bookmark.level}', title: '${bookmark.title}'`);
40+
}
41+
}
42+
},
43+
};
44+
45+
async function main() {
46+
try {
47+
await pdfBookmarks.uploadDocument();
48+
await pdfBookmarks.getAllBookmarks();
49+
} catch (error) {
50+
console.error("Error:", error.message);
51+
}
52+
}
53+
54+
main();
Lines changed: 60 additions & 0 deletions
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 Bookmarks from the document using deleteBookmark() function
5+
// 6. Perform some action after successful removing the Bookmark 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+
DROP_BOOKMARK_PATH: "/1"
18+
};
19+
20+
const pdfApi = new PdfApi(credentials.id, credentials.key);
21+
22+
const pdfBookmarks = {
23+
async uploadDocument() {
24+
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
25+
const pdfFileData = await fs.readFile(pdfFilePath);
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 deleteBookmark(){
37+
const dropResult = await pdfApi.deleteBookmark(configParams.PDF_DOCUMENT_NAME, configParams.DROP_BOOKMARK_PATH);
38+
39+
if (dropResult.body.code == 200) {
40+
console.log("Bookmark '" + configParams.DROP_BOOKMARK_PATH + "' successfully deleted!");
41+
return true;
42+
}
43+
else
44+
throw new Error("Unexpected error : can't get bookmarks list!!!");
45+
},
46+
};
47+
48+
async function main() {
49+
try
50+
{
51+
await pdfBookmarks.uploadDocument();
52+
await pdfBookmarks.deleteBookmark();
53+
await pdfBookmarks.downloadResult();
54+
}
55+
catch(error) {
56+
console.log(error.message);
57+
}
58+
}
59+
60+
main();

0 commit comments

Comments
 (0)