|
| 1 | +/** |
| 2 | + * Add text to a PDF document page 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. Put text on the PDF document page 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 | + * Add text to a PDF document page using the Aspose.PDF Cloud API. |
| 19 | + * |
| 20 | + * @returns {Promise<void>} A Promise that resolves when the adding text is complete. |
| 21 | + */ |
| 22 | +async function addText() |
| 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 page number where the text will be added. |
| 30 | + const pageNumber = 3; |
| 31 | + // Use default storage (null indicates default storage). |
| 32 | + const storage = null; |
| 33 | + // Set the folder where the document is stored. |
| 34 | + const folder = "Documents"; |
| 35 | + |
| 36 | + // Read file from file system. |
| 37 | + const buffer = fs.readFileSync("testData/" + fileName); |
| 38 | + // Upload file to cloud storage. |
| 39 | + await api.uploadFile(folder + "/" +fileName, buffer, storage); |
| 40 | + |
| 41 | + // Create new text paragraph. |
| 42 | + // Documentation available at: https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-node.js/blob/master/docs/Paragraph.md |
| 43 | + const paragraph = |
| 44 | + { |
| 45 | + // Set the left margin for the paragraph. |
| 46 | + leftMargin: 100, |
| 47 | + // Set the bottom margin for the paragraph. |
| 48 | + bottomMargin: 200, |
| 49 | + // Set the rotation angle of the text in degrees. |
| 50 | + rotation: 10, |
| 51 | + // Define an array of text lines for the paragraph. |
| 52 | + lines: [{ |
| 53 | + // Define the segments that form the line. |
| 54 | + segments: [{ |
| 55 | + // Text value for the segment. |
| 56 | + value: "segment text 1 with text state", |
| 57 | + // Define the text state for formatting. |
| 58 | + textState: { |
| 59 | + // Set font size of the text. |
| 60 | + fontSize: 20, |
| 61 | + // Set font name of the text. |
| 62 | + font: "Arial", |
| 63 | + // Set underline for the text. |
| 64 | + underline: true |
| 65 | + } |
| 66 | + }] |
| 67 | + }] |
| 68 | + }; |
| 69 | + |
| 70 | + // Swagger method definition available at: |
| 71 | + // https://reference.aspose.cloud/pdf/#/Text/PutAddText |
| 72 | + // Adding text to the PDF document page. |
| 73 | + const result = await api.putAddText( |
| 74 | + fileName, |
| 75 | + pageNumber, |
| 76 | + paragraph, |
| 77 | + folder, |
| 78 | + storage); |
| 79 | + |
| 80 | + // Log the response to console. |
| 81 | + console.log(result.body.status); |
| 82 | +} |
| 83 | + |
| 84 | +// Execute the addText function. |
| 85 | +addText(); |
0 commit comments