itext5 PDF Radio回填
已知
1.pdf文件中一组单选框都是RADIO1
2.后端存入的是用户选中的表单元素的索引
问题
下载pdf的时候回填RADIO1
解决方案
目前没有在网上找到合适的文档说明,在自己不断摸索中发现:给设置值的时候直接传入索引即可,这种做法的缺点是会丢失其他radio单选框的样式,但是无伤大雅
if ("radio".equals(type)) {
boolean numberFlag = NumberUtil.isNumber(content.getStr(key));
// 数字则按照索引进行勾选
if (numberFlag) {
for (String module : s.getFields().keySet()) {
AcroFields.Item item = s.getFields().get(module);
for (int i1 = 0; i1 < item.size(); i1++) {
if (Objects.equals(String.valueOf(item.getTabOrder(i1)), content.getStr(key))) {
s.setField(module, String.valueOf(i1));//此处是按照索引回填pdf,key为pdf的表单名称,value为要选中的索引
}
}
}
}
content.getStr(key)是存的索引,上面的代码是从Radio1的那些可选项(即item)中找到索引一样的排在第几个
完整需求
客户要求提交订单后填写在线表单,并且支持下载成pdf.
商品的一级类目决定了使用哪种pdf模板.
在线表单使用的是http://cdn.kcz66.com
数据上看
一级类目绑定pdf文件,绑定在线表单json,绑定在线表单项
因为有栅格布局什么的,所以单独有一列存储了表单项
订单绑定模板 绑定表单项json
表单项json指的是用户填的在线表单的值
关键实现
从pdf读取在线表单基础格式
用IText读取到表单项 组成json以供在线表单导入
由pdf的表单名称做唯一标识,单选选项对应的是索引tabOrder,多选和输入框其实没有组的概念
/**
* 从申请表url中获取<a href="http://cdn.kcz66.com/form-design.html">在线表单</a><b>导入</b>使用的json
*
* @param url 申请表链接
* @param checkboxGroupNameList 多选分组名称
*/
private String getJsonOnlineForm(String url, List<String> checkboxGroupNameList) {
/*定义字体*/
BaseFont baseFont;
PdfStamper ps;
try {
baseFont = BaseFont.createFont("HeiseiKakuGo-W5", "UniJIS-UCS2-H", BaseFont.NOT_EMBEDDED);
PdfReader reader = new PdfReader(url);
/*创建pdf模板,参数reader bos*/
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ps = new PdfStamper(reader, bos);
} catch (DocumentException | IOException e) {
log.error("读取失败", e);
throw new ModuleBaseException("读取失败");
}
ArrayList<BaseFont> fontArrayList = new ArrayList<>();
fontArrayList.add(baseFont);
/*读取模板文本域并封装数据(注意名字应与文本域中的名字一致)*/
AcroFields s = ps.getAcroFields();
/*定义字体,若不定义,会使用模板中的文本域设置的字体*/
s.setSubstitutionFonts(fontArrayList);
Map<String, AcroFields.Item> fields = s.getFields();
String prefix ="{\"list\":[{\"type\":\"grid\",\"label\":\"栅格布局\",\"columns\":[{\"span\":24,\"list\":[";
HashMap<String, List<String>> checkBoxMap = new HashMap<>();
List<String> fieldList = new ArrayList<>();
// 申请表标头
fieldList.add("{\"type\":\"html\",\"label\":\"申请表信息\",\"icon\":\"icon-ai-code\",\"options\":{\"noFormItem\":true,\"hidden\":false,\"defaultValue\":\"<strong>申请表信息</strong>\"},\"key\":\"html_1714380202836\"}");
Set<String> fieldNameSet = fields.keySet();
for (String fieldName : fieldNameSet) {
AcroFields.Item item = fields.get(fieldName);
String itemJson;
int size = item.size();
//多选和输入框
if (size == 1) {
// 处理多选
// checkboxGroupNameList:前端传入的分组名称
String groupName = checkboxGroupNameList.stream().filter(fieldName::startsWith).findFirst().orElse(null);
if (StrUtil.isNotBlank(groupName)) {
// 放到checkBoxMap 最后处理
if (checkBoxMap.containsKey(groupName)) {
checkBoxMap.get(groupName).add(fieldName);
}else{
checkBoxMap.put(groupName, CollUtil.newArrayList(fieldName));
}
continue;
}else{
// 处理输入框
itemJson = handleText(fieldName);
}
}else {// 单选
itemJson = handleRadio(fieldName, size, item);
}
fieldList.add(itemJson);
}
// 有多选则处理多选
if (CollUtil.isNotEmpty(checkBoxMap)) {
handleCheckBox(checkBoxMap, fieldList);
}
String itemJson = CollUtil.join(fieldList, ",");
String suffix="]}],\"options\":{\"noFormItem\":true,\"gutter\":0},\"key\":\"grid_1714375047124\"}],\"config\":{\"layout\":\"horizontal\",\"labelCol\":{\"xs\":4,\"sm\":4,\"md\":4,\"lg\":4,\"xl\":4,\"xxl\":4},\"labelWidth\":100,\"labelLayout\":\"flex\",\"wrapperCol\":{\"xs\":18,\"sm\":18,\"md\":18,\"lg\":18,\"xl\":18,\"xxl\":18},\"hideRequiredMark\":false,\"customStyle\":\"\"}}";
return prefix + itemJson + suffix;
}
/**
* 处理多选
*/
private void handleCheckBox(HashMap<String, List<String>> checkboxMap, List<String> fieldList) {
for (String groupName : checkboxMap.keySet()) {
List<String> optionList = CollUtil.newArrayList();
for (int i = 0; i < checkboxMap.get(groupName).size(); i++) {
String checkboxItemName = checkboxMap.get(groupName).get(i);
optionList.add(
"{ " +
" \"label\": \"选项" + (i + 1) + "\", " +
" \"value\": \"" + checkboxItemName + "\" " +
"} "
);
}
String checkBox =
"{ " +
" \"type\": \"checkbox\", " +
" \"label\": \"" + groupName + "\", " +
" \"options\": { " +
" \"disabled\": false, " +
" \"showLabel\": true, " +
" \"hidden\": false, " +
" \"defaultValue\": [], " +
" \"dynamicKey\": \"\", " +
" \"dynamic\": false, " +
" \"options\": [ " +
CollUtil.join(optionList, ",") +
" ] " +
" }, " +
" \"model\": \"" + groupName + "\", " +
" \"key\": \"" + groupName + "\", " +
" \"help\": \"\", " +
" \"rules\": [ " +
" { " +
" \"required\": false, " +
" \"message\": \"必填项\" " +
" } " +
" ] " +
"}";
fieldList.add(checkBox);
}
}
/**
* 处理输入框
* @param fieldName
* @return
*/
private String handleText(String fieldName) {
return
"{" +
" \"type\": \"input\"," +
" \"label\": \"" + fieldName + "\"," +
" \"options\": {" +
" \"type\": \"text\"," +
" \"width\": \"100%\"," +
" \"defaultValue\": \"\"," +
" \"placeholder\": \"请输入\"," +
" \"clearable\": false," +
" \"maxLength\": null," +
" \"addonBefore\": \"\"," +
" \"addonAfter\": \"\"," +
" \"showLabel\": true," +
" \"hidden\": false," +
" \"disabled\": false" +
" }," +
" \"model\": \"" + fieldName + "\"," +
" \"key\": \"" + fieldName + "\"," +
" \"help\": \"\"," +
" \"rules\": [" +
" {" +
" \"required\": false," +
" \"message\": \"必填项\"" +
" }" +
" ]" +
"}";
}
/**
* 处理单选
*/
private String handleRadio(String fieldName, int size, AcroFields.Item item) {
String itemJson;
List<String> radioPotion = new ArrayList<>();
for (int i = 0; i < size; i++) {
radioPotion.add(
"{" +
" \"label\": \"选项"+ (i + 1) + "\"," +
" \"value\": \"" + item.getTabOrder(i) + "\"" +
"}"
);
}
itemJson =
"{" +
" \"type\": \"radio\"," +
" \"label\": \""+ fieldName +"\"," +
" \"options\": {" +
" \"disabled\": false," +
" \"showLabel\": true," +
" \"hidden\": false," +
" \"defaultValue\": \"\"," +
" \"dynamicKey\": \"\"," +
" \"dynamic\": false," +
" \"options\": [" +
CollUtil.join(radioPotion, ",") +
" ]" +
" }," +
" \"model\": \"" + fieldName + "\"," +
" \"key\": \"" + fieldName + "\"," +
" \"help\": \"\"," +
" \"rules\": [" +
" {" +
" \"required\": false," +
" \"message\": \"必填项\"" +
" }" +
" ]" +
"}";
return itemJson;
}
用户拿到json后跳转到外部网址并且把json导入在线表单,这块是前端实现的.
然后用户在在线表单页面拖拖拽拽,更改布局和表单名称.
将得到的json存入数据库,在存入时会解析json 把栅格布局给剔除,留下最简单的表单项格式
/**
* 从模板json中读取到表单属性(过滤掉了布局代码)
* @param templateContent 模板
* @return
*/
private String getFieldConfig(String templateContent) {
List<JSONObject> formItemList = CollUtil.newArrayList();
JSONObject jsonObject = JSONObject.parseObject(templateContent);
// 递归读取掉 所有的list中的信息放到result
readJson(jsonObject, formItemList);
// 过滤一下含有model的是表单输入项(输入框/单选/多选)
formItemList = formItemList.stream().filter(p -> p.containsKey("model")).collect(Collectors.toList());
JSONArray templateConfig = new JSONArray();
templateConfig.addAll(formItemList);
return templateConfig.toJSONString();
}
/**
* 递归读取在线表单的json,拿到所有list中的值
*/
private void readJson(JSONObject jsonObject, List<JSONObject> formItemList) {
if (jsonObject.containsKey("list")) {
JSONArray jsonArray = jsonObject.getJSONArray("list");
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
formItemList.add(jsonObject1);
if (jsonObject1.containsKey("columns")) {
JSONArray jsonArray1 = jsonObject1.getJSONArray("columns");
for (int i1 = 0; i1 < jsonArray1.size(); i1++) {
JSONObject jsonObject2 = jsonArray1.getJSONObject(i1);
readJson(jsonObject2, formItemList);
}
}
}
}
}
下单时会存储{"RADIO1":"70","Funktionalität17":"123","checkbox_1714467797378":["eingeschränkt86","öffentlich9"]}
RADIO1是单选选中的索引
Funktionalität17是输入框输入了123
checkbox_1714467797378是多选框选中了两个多选框
回填pdf
// ---------读取PDF模板 开始-----------
/*创建一个pdf读取对象*/
PdfReader reader = new PdfReader(template.getExportTemplate());//template.getExportTemplate()是oss地址
/*创建pdf模板,参数reader bos*/
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfStamper ps = new PdfStamper(reader, bos);
/*定义字体*/
BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
ArrayList<BaseFont> fontArrayList = new ArrayList<>();
fontArrayList.add(baseFont);
/*读取模板文本域并封装数据(注意名字应与文本域中的名字一致)*/
AcroFields s = ps.getAcroFields();
/*定义字体,若不定义,会使用模板中的文本域设置的字体*/
s.setSubstitutionFonts(fontArrayList);
// ---------读取PDF模板 结束-----------
// ----------填充PDF数据 开始------------
JSONObject content = JSONUtil.parseObj(application.getApplicationContent());//订单读到的表单项json 上文提到的{"RADIO1":"70","Funktionalität17":"123","checkbox_1714467797378":["eingeschränkt86","öffentlich9"]}`
JSONArray fieldConfig = JSONUtil.parseArray(template.getFieldConfig());//从模板读到的不含布局的表单项数组
for (int i = 0; i < fieldConfig.size(); i++) {
JSONObject jsonObject = fieldConfig.getJSONObject(i);
String key = jsonObject.getStr("model");
String type = jsonObject.getStr("type");
if ("text".equals(type)||"input".equals(type)) {
s.setField(key, content.getStr(key));
} else if ("checkbox".equals(type)) {
JSONArray array = content.getJSONArray(key);
if (array != null) {
for (Object o : array) {
if (key.equals("symbols")) {// symbols用于洗涤打勾
s.setField(o.toString(), "Yes", true);//此处的Yes是pdf的导出值,约定导出值为Yes
}
}
}
}else if ("radio".equals(type)) {
boolean numberFlag = NumberUtil.isNumber(content.getStr(key));
// 数字则按照索引进行勾选
if (numberFlag) {
for (String module : s.getFields().keySet()) {
AcroFields.Item item = s.getFields().get(module);
for (int i1 = 0; i1 < item.size(); i1++) {
if (Objects.equals(String.valueOf(item.getTabOrder(i1)), content.getStr(key))) {
s.setField(module, String.valueOf(i1));//此处是按照索引回填pdf,key为pdf的表单名称,value为要选中的索引
}
}
}
}
}