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.
This sample shows how to print form list information, set up interactive forms (including Text Field, Check Box, Radio Button, Push Button, List Box and Combo Button), and fill out form information
// Import the JS file of ComPDFKit Web Demo.
import ComPDFKitViewer from "@compdfkit_pdf_sdk/webviewer";
const viewer = document.getElementById('webviewer');
ComPDFKitViewer({
path: '/',
initialDoc: 'Your PDF Url',
licenseKey: 'Input your license here'
}, viewer)
.then((instance) => {
const { documentViewer } = instance.Core;
documentViewer.addEventListener('documentLoaded', () => {
console.log('ComPDFKit Web Demo loaded');
// Sample: Create forms.
CreateForms(instance.Core);
})
})
function CreateForms(core) {
CreateTextField(core);
CreatCheckBox(core);
CreateRadioButton(core);
CreateListBox(core);
CreateComboBox(core);
CreatePushButton(core);
}
// Create Text Field.
function CreateTextField({ annotationManager, Annotations }) {
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);
}
// Create Check Box.
function CreatCheckBox({ annotationManager, Annotations }) {
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);
}
// Create Radio Button.
function CreateRadioButton({ annotationManager, Annotations }) {
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);
}
// Create List Box.
function CreateListBox({ annotationManager, Annotations }) {
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);
}
// Create Combo Box.
function CreateComboBox({ annotationManager, Annotations }) {
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);
}
// Create Push Button.
function CreatePushButton({ annotationManager, Annotations, Actions }) {
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);
}