|
| 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(); |
0 commit comments