Browse Source

word 合并 utils

zhongwei
段飞宇 2 years ago
parent
commit
186363dde4
  1. 14
      hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/utils/MergeDocUtils.java
  2. 84
      hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/utils/PdfUtils.java

14
hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/utils/MergeDocUtils.java

@ -54,13 +54,19 @@ public class MergeDocUtils {
for (int i = 0; i < documentList.size(); i++) {
doc = documentList.get(0);
if (i != 0) {
//documentList.get(i).createParagraph().setPageBreak(true);//实现了分页效果。//但是会出现在首行为空的情况
documentList.get(i).createParagraph().createRun().addBreak(BreakType.PAGE);//现了分页效果。使用这种方式不会出现留白的情况
if (i == 0) {
//首页直接分页,不再插入首页文档内容
documentList.get(i).createParagraph().createRun().addBreak(BreakType.PAGE);
} else if (i == documentList.size() - 1) {
//尾页不再分页,直接插入最后文档内容
appendBody(doc, documentList.get(i));
} else {
documentList.get(i).createParagraph().createRun().addBreak(BreakType.PAGE);
appendBody(doc, documentList.get(i));
}
}
doc.write(dest);//输出合并之后的文件
//输出合并之后的文件
doc.write(dest);
}

84
hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/utils/PdfUtils.java

@ -1,6 +1,7 @@
package com.hnac.hzims.ticket.utils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
@ -209,6 +210,89 @@ public class PdfUtils {
return result;
}
/**
* 对象转化为Map 并设置默认值
*
* @param obj
* @return
* @throws Exception
*/
public static Map<String, Object> objectToMapResult(Object obj, Map<String, Object> map) {
Map<String, Object> result = new HashMap<>();
if (ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) {
Class clazz = obj.getClass();
List<Field> fieldList = new ArrayList<>();
while (clazz != null) {
fieldList.addAll(Arrays.asList(clazz.getDeclaredFields()));
clazz = clazz.getSuperclass();
}
fieldList.forEach(field -> {
field.setAccessible(true);
DefaultValue defaultValue = field.getAnnotation(DefaultValue.class);
Object value;
try {
value = field.get(obj);
} catch (IllegalAccessException e) {
throw new ServiceException("获取属性性出错");
}
//若为list则不处理
if (value instanceof List) {
return;
}
//若为时间格式则进行格式化
if (value instanceof LocalDateTime) {
value = DateUtil.format((LocalDateTime) value, TicketConstants.TICKET_DATE_PATTERN);
}
if (value instanceof Date) {
value = DateUtil.format((Date) value, TicketConstants.TICKET_DATE_PATTERN);
}
if (map.containsKey(field.getName())) {
return;
}
//属性上是否加入DefaultValue注解 若加入 则判断是否定义属性名以及值 若未定义则取原属性名及值
if (ObjectUtil.isNotEmpty(defaultValue)) {
result.put(StringUtil.isNoneBlank(defaultValue.name()) ? defaultValue.name() : field.getName(),
ObjectUtil.isNotEmpty(defaultValue.value()) ? defaultValue.value() : value);
} else {
if ("initialPrincipalName".equals(field.getName())
|| "principalChangeTime".equals(field.getName())
|| "principalName".equals(field.getName())
|| "jobReceiver".equals(field.getName())
|| "jobReceiverDateTime".equals(field.getName())
|| "watchPrincipalEndTime".equals(field.getName())
|| "licenseTime".equals(field.getName())
|| "licensorName".equals(field.getName())
|| "signerDateTime".equals(field.getName())
|| "classGroupName".equals(field.getName())
|| "code".equals(field.getName())
|| "signerName".equals(field.getName())
|| "watchPrincipalName".equals(field.getName()))
{
result.put(field.getName(), Optional.ofNullable(value).orElse("\u00A0 "));
} else if (
"groundNum".equals(field.getName()) ||
"dismantleNum".equals(field.getName()) ||
"retainNum".equals(field.getName())
) {
result.put(field.getName(), Optional.ofNullable(value).orElse("\u00A0 "));
} else {
//如果导出word为null会出现{{ ,value设置"\u00A0"
result.put(field.getName(), Optional.ofNullable(value).orElse("\u00A0"));
}
}
});
}
return result;
}
/**
* 读取本地pdf,这里设置的是预览
*/

Loading…
Cancel
Save