Skip to content

Commit e0ffba7

Browse files
authored
Merge pull request #35 from aspose-pdf-cloud/holub
Refactor PDF form field creation in Node.js SDK
2 parents 9297666 + 3e65636 commit e0ffba7

File tree

1 file changed

+32
-53
lines changed

1 file changed

+32
-53
lines changed

pdf/nodejs/acroforms/create/_index.md

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -70,68 +70,47 @@ It is easy to get started with Aspose.PDF Cloud Node.js SDK and there is nothing
7070

7171
```js
7272

73-
import credentials from "./credentials.json" with { type: "json" };
73+
import credentials from "./credentials.json" with { type: "json" };
7474
import fs from 'node:fs/promises';
7575
import { PdfApi } from "asposepdfcloud";
76-
import { Color } from "asposepdfcloud/src/models/color.js";
77-
import { FontStyles } from "asposepdfcloud/src/models/fontStyles.js";
78-
import { LineSpacing } from "asposepdfcloud/src/models/lineSpacing.js";
79-
import { Paragraph } from "asposepdfcloud/src/models/paragraph.js";
80-
import { TextHorizontalAlignment } from "asposepdfcloud/src/models/textHorizontalAlignment.js";
81-
import { VerticalAlignment } from "asposepdfcloud/src/models/verticalAlignment.js";
82-
import { WrapMode } from "asposepdfcloud/src/models/wrapMode.js";
83-
import { TextLine } from "asposepdfcloud/src/models/textLine.js";
84-
import { Segment } from "asposepdfcloud/src/models/segment.js";
76+
import { Border } from "asposepdfcloud/src/models/border.js"
77+
import { TextBoxField } from "asposepdfcloud/src/models/textBoxField.js";
78+
import { Dash } from "asposepdfcloud/src/models/dash.js";
8579
import { Rectangle } from "asposepdfcloud/src/models/rectangle.js";
86-
import { TextState } from "asposepdfcloud/src/models/textState.js";
80+
import { Field } from "asposepdfcloud/src/models/field.js";
81+
import { FieldType } from "asposepdfcloud/src/models/fieldType.js";
82+
import { Fields } from "asposepdfcloud/src/models/fields.js";
8783

88-
const LOCAL_FILE_NAME = "c:\\Samples\\sample.pdf";
89-
const STORAGE_FILENAME = "sample.pdf";
90-
const PAGE_NUMBER = 1;
91-
const TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
84+
async function addFormField () {
85+
const LOCAL_FILE_NAME = "C:\\Samples\\StudentInfoFormElectronic.pdf";
86+
const STORAGE_FILE_NAME = "StudentInfoFormElectronic.pdf";
9287

93-
async function add() {
9488
const pdfApi = new PdfApi(credentials.id, credentials.key);
9589
try {
90+
let fileData = await fs.readFile(LOCAL_FILE_NAME);
91+
let uploadResult = await pdfApi.uploadFile(STORAGE_FILE_NAME, fileData);
92+
console.log(uploadResult.response.text);
93+
}
94+
catch (error) {
95+
console.error(error.message);
96+
}
9697

97-
const fileBuffer = await fs.readFile(LOCAL_FILE_NAME);
98-
await pdfApi.uploadFile(STORAGE_FILENAME, fileBuffer);
99-
100-
const textState = Object.assign(new TextState(), {
101-
fontSize: 20,
102-
font: "Arial",
103-
foregroundColor: Object.assign(new Color(), { a: 255, r: 0, g: 0, b: 255 }),
104-
backgroundColor: Object.assign(new Color(), { a: 255, r: 255, g: 255, b: 0 }),
105-
fontStyle: FontStyles.Regular,
106-
underline: true
107-
});
108-
109-
const segment = Object.assign(new Segment(),
110-
{
111-
value: TEXT_CONTENT,
112-
textState: textState
113-
});
114-
115-
const textLine = Object.assign(new TextLine(), { segments: [segment] });
116-
117-
const paragraph = Object.assign(new Paragraph(), {
118-
lineSpacing: LineSpacing.FullSize,
119-
wrapMode: WrapMode.ByWords,
120-
rectangle: Object.assign(new Rectangle(), { lLX: 10, lLY: 10, uRX: 300, uRY: 500 }),
121-
horizontalAlignment: TextHorizontalAlignment.FullJustify,
122-
verticalAlignment: VerticalAlignment.Center,
123-
lines: [textLine]
124-
});
125-
126-
const result = await pdfApi.putAddText(
127-
STORAGE_FILENAME,
128-
PAGE_NUMBER,
129-
paragraph
130-
);
131-
132-
console.log(result.body.status);
98+
let textBoxField = new TextBoxField();
99+
textBoxField.pageIndex = 1;
100+
textBoxField.partialName = "Email";
101+
textBoxField.rect = new Rectangle(100, 100, 180, 120);
102+
textBoxField.value = "aspose-pdf-cloud@example.com";
103+
let border = new Border();
104+
border.width = 5;
105+
border.dash = new Dash(1, 1);
106+
textBoxField.Border = border;
107+
try {
108+
let response = await pdfApi.putTextBoxField(STORAGE_FILE_NAME, "Email", textBoxField);
109+
console.log(response.body.status);
110+
const downloadRes = await pdfApi.downloadFile(STORAGE_FILE_NAME)
111+
await fs.writeFile(RESULT_FILE_NAME, downloadRes.body);
133112
} catch (error) {
134-
console.error("Error adding text to PDF:", error.message);
113+
console.log(error.message);
135114
}
136115
}
137116
```

0 commit comments

Comments
 (0)