|
| 1 | +// Import necessary classes from the Aspose PDF Cloud library |
| 2 | +const fs = require("fs"); |
| 3 | +const { PdfApi } = require("../src/api/api"); |
| 4 | +const { Color } = require("../src/models/color"); |
| 5 | +const { FontStyles } = require("../src/models/fontStyles"); |
| 6 | +const { LineSpacing } = require("../src/models/lineSpacing"); |
| 7 | +const { Paragraph } = require("../src/models/paragraph"); |
| 8 | +const { TextHorizontalAlignment } = require("../src/models/textHorizontalAlignment"); |
| 9 | +const { VerticalAlignment } = require("../src/models/verticalAlignment"); |
| 10 | +const { WrapMode } = require("../src/models/wrapMode"); |
| 11 | +const { TextLine } = require("asposepdfcloud/src/models/textLine"); |
| 12 | +const { Segment } = require("asposepdfcloud/src/models/segment"); |
| 13 | +const { Rectangle } = require("asposepdfcloud/src/models/rectangle"); |
| 14 | +const { TextState } = require("asposepdfcloud/src/models/textState"); |
| 15 | + |
| 16 | +// The initialization assumes that the necessary credentials (Application ID and Application Key) from https://dashboard.aspose.cloud/ |
| 17 | +const api = new PdfApi("YOUR_API_SID", "YOUR_API_KEY"); |
| 18 | + |
| 19 | +// Set the document name |
| 20 | +const fileName = "4pages.pdf"; |
| 21 | +// Set the page number where the text will be added |
| 22 | +const pageNumber = 3; |
| 23 | +// Use default storage (null indicates default storage) |
| 24 | +const storage = null; |
| 25 | +// Set the folder where the document is stored |
| 26 | +const folder = "Documents"; |
| 27 | + |
| 28 | +/** |
| 29 | + * Add text to a PDF document page using the Aspose.PDF Cloud API. |
| 30 | + * |
| 31 | + * This function performs the following steps: |
| 32 | + * 1. Reads a PDF file from the local file system. |
| 33 | + * 2. Uploads the PDF file to the Aspose.PDF Cloud storage. |
| 34 | + * 3. Creates an `Paragraph` object with the inserted text. |
| 35 | + * 4. Put text on the third page of PDF document using the Aspose.PDF Cloud API. |
| 36 | + * 5. Logs the result to the console. |
| 37 | + * |
| 38 | + * @returns {Promise<void>} A Promise that resolves when the adding text is complete. |
| 39 | + */ |
| 40 | +async function putAddText() |
| 41 | +{ |
| 42 | + // Read file from file system. |
| 43 | + const buffer = fs.readFileSync("testData/" + fileName); |
| 44 | + // Upload file to cloud storage. |
| 45 | + const uploadResponse = await api.uploadFile(folder + "/" +fileName, buffer, storage) |
| 46 | + |
| 47 | + // Represents color DTO. |
| 48 | + const foregroundColor = new Color(); |
| 49 | + // Set an alpha component. |
| 50 | + foregroundColor.a = 255; |
| 51 | + // Set the red component. |
| 52 | + foregroundColor.r = 0; |
| 53 | + // Set the green component. |
| 54 | + foregroundColor.g = 0; |
| 55 | + // Set the blue component. |
| 56 | + foregroundColor.b = 255; |
| 57 | + |
| 58 | + // Represents color DTO. |
| 59 | + const backgroundColor = new Color(); |
| 60 | + // Set an alpha component. |
| 61 | + backgroundColor.a = 100; |
| 62 | + // Set the red component. |
| 63 | + backgroundColor.r = 255; |
| 64 | + // Set the green component. |
| 65 | + backgroundColor.g = 255; |
| 66 | + // Set the blue component. |
| 67 | + backgroundColor.b = 0; |
| 68 | + |
| 69 | + // Create the new paragraph object to represent text paragraphs |
| 70 | + const rectangle = new Rectangle(); |
| 71 | + // Lower left x coordinate |
| 72 | + rectangle.lLX = 10; |
| 73 | + // Lower left y coordinate |
| 74 | + rectangle.lLY = 10; |
| 75 | + // Upper right x coordinate |
| 76 | + rectangle.uRX = 300; |
| 77 | + // Upper right y coordinate |
| 78 | + rectangle.uRY = 500; |
| 79 | + |
| 80 | + // Create the new text state. |
| 81 | + const textState = new TextState(); |
| 82 | + // Set font size of the text |
| 83 | + textState.fontSize = 20; |
| 84 | + // Set font name of the text |
| 85 | + textState.font = "Arial"; |
| 86 | + // Set foreground color of the text |
| 87 | + textState.foregroundColor = foregroundColor; |
| 88 | + // Set background color of the text |
| 89 | + textState.backgroundColor = backgroundColor; |
| 90 | + // Set font style of the text |
| 91 | + textState.fontStyle = FontStyles.Regular; |
| 92 | + // Font file path in storage (null means default font) |
| 93 | + textState.fontFile = null; |
| 94 | + // Set underline for the text |
| 95 | + textState.underline = true; |
| 96 | + // Set strikeout for the text |
| 97 | + textState.strikeOut = false; |
| 98 | + // Set superscript mode for the text |
| 99 | + textState.superscript = false; |
| 100 | + // Set subscript mode for the text |
| 101 | + textState.subscript = false; |
| 102 | + |
| 103 | + // Create the new text segment. |
| 104 | + const segment = new Segment(); |
| 105 | + // Text value for the segment |
| 106 | + segment.value = "segment text 1 with text state"; |
| 107 | + // Define the text state for formatting |
| 108 | + segment.textState = textState; |
| 109 | + |
| 110 | + // Create the new text line. |
| 111 | + const textLine = new TextLine(); |
| 112 | + // Set the line's horizontal alignment to Right |
| 113 | + textLine.horizontalAlignment = TextHorizontalAlignment.Right; |
| 114 | + // Define the segments that form the line |
| 115 | + textLine.segments = [segment]; |
| 116 | + |
| 117 | + // Create new text paragraph. |
| 118 | + const paragraph = new Paragraph(); |
| 119 | + // Set the line spacing mode to FullSize |
| 120 | + paragraph.lineSpacing = LineSpacing.FullSize; |
| 121 | + // Set the word wrap mode to wrap by words |
| 122 | + paragraph.wrapMode = WrapMode.ByWords; |
| 123 | + // Set the horizontal alignment for the text inside the paragraph |
| 124 | + paragraph.horizontalAlignment = TextHorizontalAlignment.FullJustify; |
| 125 | + // Set the left margin for the paragraph |
| 126 | + paragraph.leftMargin = 100; |
| 127 | + // Set the right margin for the paragraph |
| 128 | + paragraph.rightMargin = 100; |
| 129 | + // Set the top margin for the paragraph |
| 130 | + paragraph.topMargin = 200; |
| 131 | + // Set the bottom margin for the paragraph |
| 132 | + paragraph.bottomMargin = 200; |
| 133 | + // Define the rectangle area for the paragraph |
| 134 | + paragraph.rectangle = rectangle; |
| 135 | + // Set the rotation angle of the text in degrees |
| 136 | + paragraph.rotation = 10, |
| 137 | + // Set the indent for subsequent lines |
| 138 | + paragraph.subsequentLinesIndent = 30, |
| 139 | + // Set the vertical alignment for the text inside the paragraph |
| 140 | + paragraph.vercolorticalAlignment = VerticalAlignment.Center, |
| 141 | + // Define an array of text lines for the paragraph |
| 142 | + paragraph.lines = [textLine]; |
| 143 | + |
| 144 | + // Swagger method for adding text: https://reference.aspose.cloud/pdf/#/Text/PutAddText |
| 145 | + // Call the API to add text to the specified PDF document page |
| 146 | + const result = await api.putAddText( |
| 147 | + uploadResponse.body.uploaded[0], |
| 148 | + pageNumber, |
| 149 | + paragraph, |
| 150 | + folder, |
| 151 | + storage); |
| 152 | + |
| 153 | + // Log the response to console. |
| 154 | + console.log(result.body.status); // Log the status of the operation |
| 155 | +} |
| 156 | + |
| 157 | +// Execute the putAddText function |
| 158 | +putAddText(); |
0 commit comments