PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Creating form fields works the same as adding any other annotation, as can be seen in the guides for programmatically creating annotations. But you need to create a field with Annotations.Forms.Field, use the corresponding widget annotation class, such as TextWidgetAnnotation or CheckButtonWidgetAnnotation, and add the field with the field manager of annotationManager.
Text fields could be created, customized, named, filled, downloaded, hidden, and deleted. Except for the field, ComPDF for Web provides options to change the text color, background color, font, single/multiple lines, and alignment of the text in the text field. Here is the sample code below to set edit a text field.
const fieldManager = annotationManager.getFieldManager();
const textField = new Annotations.Forms.Field('Text Field1', { type: 'Tx', value: 'test' });
const textWidget = new Annotations.TextWidgetAnnotation(textField, {
PageNumber: 1,
X: 102,
Y: 136,
Width: 59,
Height: 20
});
textWidget.Hidden = false;
textWidget.FillColor = new Annotations.Color('#93B9FD');
textWidget.font = new Annotations.Font({ name: 'Helvetica', size: 14 });
textWidget.TextColor = new Annotations.Color('#000000');
textWidget.quadding = 'Left-justified';
textWidget.setFieldFlag(Annotations.WidgetFlags.MULTILINE, false);
fieldManager.addField(textField);
annotationManager.addAnnotation(textWidget);Check boxes could be created, customized, named, filled, downloaded, hidden, and deleted. Except for the field, ComPDF for Web provides options to set the shape of the marker that appears inside the check box including check, circle, cross, diamond, square, or star. Here is the sample code below to set edit a check box.
const fieldManager = annotationManager.getFieldManager();
const checkBoxField = new Annotations.Forms.Field('Check Box1', { type: 'Btn', value: 'Off' });
const checkBoxWidget = new Annotations.CheckButtonWidgetAnnotation(checkBoxField, {
PageNumber: 1,
X: 110,
Y: 190,
Width: 20,
Height: 20
});
checkBoxWidget.Hidden = false;
checkBoxWidget.StrokeColor = new Annotations.Color('#43474D');
checkBoxWidget.FillColor = new Annotations.Color('#93B9FD');
checkBoxWidget.StrokeThickness = 1;
// Check style: '4' check, 'l' circle, '8' cross, 'u' diamond, 'n' square, 'H' star.
checkBoxWidget.captions.Normal = '4';
fieldManager.addField(checkBoxField);
annotationManager.addAnnotation(checkBoxWidget);Radio buttons could be created, customized, named, filled, downloaded, hidden, and deleted. Except for the field, ComPDF for Web provides options to set the shape of the marker that appears inside the radio button including check, circle, cross, diamond, square, or star. Here is the sample code below to edit a radio button.
const fieldManager = annotationManager.getFieldManager();
const radioField = new Annotations.Forms.Field('Group1', { type: 'Btn', value: 'Off' });
const radioWidget = new Annotations.RadioButtonWidgetAnnotation(radioField, {
PageNumber: 1,
X: 150,
Y: 190,
Width: 20,
Height: 20
});
radioWidget.Hidden = false;
radioWidget.StrokeColor = new Annotations.Color('#43474D');
radioWidget.StrokeThickness = 1;
radioWidget.FillColor = new Annotations.Color('#93B9FD');
// Check style: '4' check, 'l' circle, '8' cross, 'u' diamond, 'n' square, 'H' star.
radioWidget.captions.Normal = 'l';
fieldManager.addField(radioField);
annotationManager.addAnnotation(radioWidget);A list box could be created, customized, named, selected, downloaded, hidden, and deleted. Except for the field, ComPDF for Web provides options to change the text color, background color, and font in the list box. Here is the sample code below to edit a list box.
const fieldManager = annotationManager.getFieldManager();
const listBoxField = new Annotations.Forms.Field('List Box1', { type: 'Ch', value: 'Item1' });
const listBoxWidget = new Annotations.ListWidgetAnnotation(listBoxField, {
PageNumber: 1,
X: 356,
Y: 176,
Width: 58,
Height: 27
});
listBoxWidget.StrokeColor = new Annotations.Color('#43474D');
listBoxWidget.StrokeThickness = 1;
listBoxWidget.font = new Annotations.Font({ name: 'Helvetica', size: 14 });
listBoxWidget.Hidden = false;
listBoxWidget.setFieldOptions([
{ value: 'Item1', displayValue: 'Item1' },
{ value: 'Item2', displayValue: 'Item2' }
]);
listBoxWidget.TextColor = new Annotations.Color('#000000');
listBoxWidget.FillColor = new Annotations.Color('#93B9FD');
fieldManager.addField(listBoxField);
annotationManager.addAnnotation(listBoxWidget);Combo boxes could be created, customized, named, selected, downloaded, hidden, and deleted. Except for the field, ComPDF for Web provides options to change the text color, background color, and font in the combo box. Here is the sample code below to edit a combo box.
const fieldManager = annotationManager.getFieldManager();
const comboBoxField = new Annotations.Forms.Field('Combo Box1', { type: 'Ch', value: 'Item1' });
const comboBoxWidget = new Annotations.ChoiceWidgetAnnotation(comboBoxField, {
PageNumber: 1,
X: 356,
Y: 176,
Width: 58,
Height: 27
});
comboBoxWidget.StrokeColor = new Annotations.Color('#43474D');
comboBoxWidget.StrokeThickness = 1;
comboBoxWidget.font = new Annotations.Font({ name: 'Helvetica', size: 14 });
comboBoxWidget.Hidden = false;
comboBoxWidget.setFieldOptions([
{ value: 'Item1', displayValue: 'Item1' },
{ value: 'Item2', displayValue: 'Item2' }
]);
comboBoxWidget.TextColor = new Annotations.Color('#000000');
comboBoxWidget.FillColor = new Annotations.Color('#93B9FD');
fieldManager.addField(comboBoxField);
annotationManager.addAnnotation(comboBoxWidget);Push buttons could be created, customized, named, downloaded, hidden, and deleted. Except for the field, ComPDF for Web provides options to change the text color, background color, and font in the push button or set an action to go to the page or open a web link. Here is the sample code below to edit a push button.
const fieldManager = annotationManager.getFieldManager();
// Go To Pages.
const pageButtonField = new Annotations.Forms.Field('Button1', { type: 'Btn' });
const pageButton = new Annotations.PushButtonWidgetAnnotation(pageButtonField, {
PageNumber: 1,
X: 356,
Y: 176,
Width: 58,
Height: 27
});
pageButton.FillColor = new Annotations.Color('#93B9FD');
pageButton.StrokeColor = new Annotations.Color('#43474D');
pageButton.StrokeThickness = 1;
pageButton.font = new Annotations.Font({ name: 'Helvetica', size: 14 });
pageButton.Hidden = false;
pageButton.TextColor = new Annotations.Color('#000000');
pageButton.captions.Normal = 'OK';
pageButton.addAction('U', new Actions.GoTo({ dest: { page: 2 } }));
fieldManager.addField(pageButtonField);
annotationManager.addAnnotation(pageButton);
// Open a Web Link.
const linkButtonField = new Annotations.Forms.Field('Button1', { type: 'Btn' });
const linkButton = new Annotations.PushButtonWidgetAnnotation(linkButtonField, {
PageNumber: 1,
X: 356,
Y: 176,
Width: 58,
Height: 27
});
linkButton.FillColor = new Annotations.Color('#93B9FD');
linkButton.StrokeColor = new Annotations.Color('#43474D');
linkButton.StrokeThickness = 1;
linkButton.font = new Annotations.Font({ name: 'Helvetica', size: 14 });
linkButton.Hidden = false;
linkButton.TextColor = new Annotations.Color('#000000');
linkButton.captions.Normal = 'OK';
linkButton.addAction('U', new Actions.URI({ uri: 'http://example.com' }));
fieldManager.addField(linkButtonField);
annotationManager.addAnnotation(linkButton);