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