|
| 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" }; |
| 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 | +}; |
| 19 | + |
| 20 | +const pdfApi = new PdfApi(credentials.id, credentials.key); |
| 21 | + |
| 22 | +const pdfPages = { |
| 23 | + async uploadDocument () { |
| 24 | + const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME); |
| 25 | + const pdfFileData = await fs.readFile(fileNamePath); |
| 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 getPagesInfo () { |
| 37 | + const resultPages = await pdfApi.getPages(configParams.PDF_DOCUMENT_NAME); |
| 38 | + |
| 39 | + if (resultPages.body.code == 200 && resultPages.body.pages.list) { |
| 40 | + this.showPages(resultPages.body.pages.list, "pages"); |
| 41 | + return resultPages.body.pages.list; |
| 42 | + } |
| 43 | + else |
| 44 | + console.error("Unexpected error : can't get pages!!!"); |
| 45 | + }, |
| 46 | + |
| 47 | + async getPageInfo (pageNumber) { |
| 48 | + const resultPages = await pdfApi.getPage(configParams.PDF_DOCUMENT_NAME, pageNumber); |
| 49 | + |
| 50 | + if (resultPages.body.code == 200 && resultPages.body.page) { |
| 51 | + this.showPages( [ resultPages.body.page ], "page"); |
| 52 | + return resultPages.body.page; |
| 53 | + } |
| 54 | + else |
| 55 | + console.error("Unexpected error : can't get pages!!!"); |
| 56 | + }, |
| 57 | + |
| 58 | + showPages (pages, prefix) { |
| 59 | + if (Array.isArray(pages) && pages.length > 0) |
| 60 | + { |
| 61 | + pages.forEach(function(page) { |
| 62 | + console.log(prefix +" => id: '" + page.id + "', lLx: '" + page.rectangle.lLX + "', lLY: '" + page.rectangle.lLY + "', uRX: '" + page.rectangle.uRX + "', uRY: '" + page.rectangle.uRY + "'"); |
| 63 | + }); |
| 64 | + } |
| 65 | + else |
| 66 | + console.error("showPages() error: array of pages is empty!") |
| 67 | + }, |
| 68 | +} |
| 69 | + |
| 70 | +async function main() { |
| 71 | + try { |
| 72 | + await pdfPages.uploadDocument(); |
| 73 | + await pdfPages.getPagesInfo(); |
| 74 | + await pdfPages.getPageInfo(configParams.PAGE_NUMBER); |
| 75 | + } catch (error) { |
| 76 | + console.error("Error:", error.message); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +main(); |
0 commit comments