PDF 生成模板编辑器
开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。
创建表单字段的方式与添加任何其他注释一样,如在创建注释的指南中所示。但是,你需要用 Annotations.Forms.Field 创建域,使用对应的表单控件类,例如 TextWidgetAnnotation 或 CheckButtonWidgetAnnotation 等,并通过 annotationManager 的域管理器添加该域。
文本域允许用户在指定区域输入文本,常用于收集用户信息或填写表格。以下是创建文本域的示例代码:
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);复选框允许用户通过勾选或取消勾选来表示某个选项的状态。以下是创建复选框的示例代码:
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;
// 选中样式:'4' 勾、'l' 圆、'8' 叉、'u' 菱形、'n' 方块、'H' 星。
checkBoxWidget.captions.Normal = '4';
fieldManager.addField(checkBoxField);
annotationManager.addAnnotation(checkBoxWidget);单选框允许用户在一组预定义选项中选择唯一的单个选项。以下是创建单选框的示例代码:
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');
// 选中样式:'4' 勾、'l' 圆、'8' 叉、'u' 菱形、'n' 方块、'H' 星。
radioWidget.captions.Normal = 'l';
fieldManager.addField(radioField);
annotationManager.addAnnotation(radioWidget);列表框允许用户从预定义的选项列表中选择一个或多个项目,提供便捷的数据选择功能。以下是创建列表框的示例代码:
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);可以下拉的区域,选中下拉的一项将显示在该区域中。以下是创建下拉菜单的示例代码:
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);按钮允许用户在 PDF 页面上执行操作,如页面跳转和超链接跳转。以下是创建按钮的示例代码:
const fieldManager = annotationManager.getFieldManager();
// 跳转页面。
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);
// 打开一个链接。
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);