From 186363dde4ea334c7f4fdd2094bfb83b77a60ac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AE=B5=E9=A3=9E=E5=AE=87?= <2684146481@qq.com> Date: Fri, 26 May 2023 17:35:30 +0800 Subject: [PATCH] =?UTF-8?q?word=20=E5=90=88=E5=B9=B6=20utils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/hnac/hzims/ticket/utils/MergeDocUtils.java | 14 ++-- .../java/com/hnac/hzims/ticket/utils/PdfUtils.java | 84 ++++++++++++++++++++++ 2 files changed, 94 insertions(+), 4 deletions(-) diff --git a/hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/utils/MergeDocUtils.java b/hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/utils/MergeDocUtils.java index 209f4fb..3849457 100644 --- a/hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/utils/MergeDocUtils.java +++ b/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); } diff --git a/hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/utils/PdfUtils.java b/hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/utils/PdfUtils.java index ef57581..be9a22b 100644 --- a/hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/utils/PdfUtils.java +++ b/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 objectToMapResult(Object obj, Map map) { + Map result = new HashMap<>(); + if (ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) { + Class clazz = obj.getClass(); + List 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,这里设置的是预览 */