|
| 1 | +/** |
| 2 | + * Splitting the PDF document to pages using the Aspose.PDF Cloud API. |
| 3 | + * |
| 4 | + * This use case performs the following steps: |
| 5 | + * 1. Import the necessary classes. |
| 6 | + * 2. Reads a PDF file from the local file system. |
| 7 | + * 3. Uploads the PDF file to the Aspose.PDF Cloud storage. |
| 8 | + * 4. Splitting document to pages using the Aspose.PDF Cloud API. |
| 9 | + * 5. Logs the result to the console. |
| 10 | + * |
| 11 | + */ |
| 12 | + |
| 13 | +// Import necessary classes. |
| 14 | +const fs = require("fs"); |
| 15 | +const { PdfApi } = require("asposepdfcloud"); |
| 16 | + |
| 17 | +/** |
| 18 | + * Splitting the PDF document per pages using the Aspose.PDF Cloud API. |
| 19 | + * |
| 20 | + * @returns {Promise<void>} A Promise that resolves when the splitting document is complete. |
| 21 | + */ |
| 22 | +async function splitDocument() |
| 23 | +{ |
| 24 | + // The initialization assumes that the necessary credentials (Application ID and Application Key) from https://dashboard.aspose.cloud/ |
| 25 | + const api = new PdfApi("YOUR_API_SID", "YOUR_API_KEY"); |
| 26 | + |
| 27 | + // Set the document name. |
| 28 | + const fileName = "4pages.pdf"; |
| 29 | + // Set the start page number. |
| 30 | + const pageFrom = 2; |
| 31 | + // Set the end page number same as pageFrom. |
| 32 | + const pageTo = 2; |
| 33 | + // Set the resulting documents format. |
| 34 | + // Available formats are: PDF, PDFA1A, PDFA1B, PDFA3A, PDFA3B, DOC, DOCX, XLS, XPS, TIFF, SVG, JPEG, JPG, PNG, EMF, BMP, GIF, PPTX, EPUB. |
| 35 | + const format = "PDF"; |
| 36 | + // Use default storage (null indicates default storage). |
| 37 | + const storage = null; |
| 38 | + // Set the folder where the document is stored. |
| 39 | + const folder = "Documents"; |
| 40 | + |
| 41 | + // Read the file from file system. |
| 42 | + const buffer = fs.readFileSync("testData/" + fileName); |
| 43 | + // Upload the file to cloud storage. |
| 44 | + const uploadResponse = await api.uploadFile(folder + "/" + fileName, buffer, storage) |
| 45 | + |
| 46 | + // Swagger documentation available at: |
| 47 | + // https://reference.aspose.cloud/pdf/#/Document/PostSplitDocument |
| 48 | + // Split the PDF document to single page. |
| 49 | + const result = await api.postSplitDocument( |
| 50 | + uploadResponse.body.uploaded[0], |
| 51 | + format, |
| 52 | + pageFrom, |
| 53 | + pageTo, |
| 54 | + storage, |
| 55 | + folder); |
| 56 | + |
| 57 | + // Log the response to console. |
| 58 | + console.log(result.body.status); |
| 59 | + // Log split result. |
| 60 | + result.body.result.documents.forEach((document, index) => |
| 61 | + { |
| 62 | + console.log(index + 1 + ") " + document.href); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | +// Execute the splitDocument function. |
| 67 | +splitDocument(); |
0 commit comments