|
@@ -3,8 +3,23 @@ package com.dayou.utils;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.dayou.dto.ItemData;
|
|
|
import com.dayou.entity.PersonalExample;
|
|
|
+import com.dayou.exception.ErrorCode;
|
|
|
import com.dayou.vo.CompareExampleItemVO;
|
|
|
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
+import org.apache.poi.util.Units;
|
|
|
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
|
+import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
|
|
+import org.apache.poi.xwpf.usermodel.XWPFRun;
|
|
|
+import org.apache.xmlbeans.XmlException;
|
|
|
+import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
|
|
|
+import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor;
|
|
|
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing;
|
|
|
+import org.springframework.core.io.Resource;
|
|
|
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
@@ -194,4 +209,102 @@ public class PersonalUtils {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 添加公章
|
|
|
+ * @param inputStream 需要盖章的文件流
|
|
|
+ * @param resource 公章图片资源
|
|
|
+ * @param originFilePath 原文件路经
|
|
|
+ */
|
|
|
+ public static void addPublicStamp(InputStream inputStream,Resource resource,String originFilePath){
|
|
|
+ try {
|
|
|
+ XWPFDocument document = new XWPFDocument(inputStream);
|
|
|
+ XWPFRun targetRun = stampingPoint(document,1);
|
|
|
+ if (targetRun == null){
|
|
|
+ targetRun = stampingPoint(document,13);
|
|
|
+ if(targetRun == null){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ targetRun.addPicture(resource.getInputStream(), XWPFDocument.PICTURE_TYPE_PNG, "stamp.png", Units.toEMU(150), Units.toEMU(150));
|
|
|
+ CTDrawing drawing = targetRun.getCTR().getDrawingArray(0);
|
|
|
+ CTGraphicalObject graphic = drawing.getInlineArray(0).getGraphic();
|
|
|
+ //拿到新插入的图片替换添加CTAnchor 设置浮动属性 删除inline属性
|
|
|
+ CTAnchor anchor = getAnchorWithGraphic(graphic, "stamp",
|
|
|
+ Units.toEMU(150), Units.toEMU(150),//图片大小
|
|
|
+ Units.toEMU(260), Units.toEMU(-90), true);//相对当前段落位置 需要计算段落已有内容的左偏移
|
|
|
+ drawing.setAnchorArray(new CTAnchor[]{anchor});//添加浮动属性
|
|
|
+ drawing.removeInline(0);//删除行内属性
|
|
|
+
|
|
|
+ File absoluteFile = new File(originFilePath);
|
|
|
+ FileOutputStream out = new FileOutputStream(absoluteFile);
|
|
|
+ document.write(out);
|
|
|
+ out.close();
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } catch (InvalidFormatException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static XWPFRun stampingPoint( XWPFDocument document,int point) throws IOException {
|
|
|
+ List<XWPFParagraph> paragraphs = document.getParagraphs();
|
|
|
+ XWPFParagraph paragraph = paragraphs.get(paragraphs.size() - point);
|
|
|
+ List<XWPFRun> runs = paragraph.getRuns();
|
|
|
+ XWPFRun targetRun = null;
|
|
|
+ if (runs.size()>0){
|
|
|
+ targetRun = runs.get(runs.size() - 1);
|
|
|
+ if (targetRun.getEmbeddedPictures().size()==0){
|
|
|
+ if (DateUtils.checkIsDate(targetRun.toString())){
|
|
|
+ return targetRun;
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ ErrorCode.throwBusinessException(ErrorCode.CUSTOM_ERROR,"文件已加盖公章.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param ctGraphicalObject 图片数据
|
|
|
+ * @param deskFileName 图片描述
|
|
|
+ * @param width 宽
|
|
|
+ * @param height 高
|
|
|
+ * @param leftOffset 水平偏移 left
|
|
|
+ * @param topOffset 垂直偏移 top
|
|
|
+ * @param behind 文字上方,文字下方
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private static CTAnchor getAnchorWithGraphic(CTGraphicalObject ctGraphicalObject,
|
|
|
+ String deskFileName, int width, int height,
|
|
|
+ int leftOffset, int topOffset, boolean behind) {
|
|
|
+ System.out.println(">>width>>"+width+"; >>height>>>>"+height);
|
|
|
+ String anchorXML =
|
|
|
+ "<wp:anchor xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" "
|
|
|
+ + "simplePos=\"0\" relativeHeight=\"0\" behindDoc=\"" + ((behind) ? 1 : 0) + "\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\">"
|
|
|
+ + "<wp:simplePos x=\"0\" y=\"0\"/>"
|
|
|
+ + "<wp:positionH relativeFrom=\"column\">"
|
|
|
+ + "<wp:posOffset>" + leftOffset + "</wp:posOffset>"
|
|
|
+ + "</wp:positionH>"
|
|
|
+ + "<wp:positionV relativeFrom=\"paragraph\">"
|
|
|
+ + "<wp:posOffset>" + topOffset + "</wp:posOffset>" +
|
|
|
+ "</wp:positionV>"
|
|
|
+ + "<wp:extent cx=\"" + width + "\" cy=\"" + height + "\"/>"
|
|
|
+ + "<wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/>"
|
|
|
+ + "<wp:wrapNone/>"
|
|
|
+ + "<wp:docPr id=\"1\" name=\"Drawing 0\" descr=\"" + deskFileName + "\"/><wp:cNvGraphicFramePr/>"
|
|
|
+ + "</wp:anchor>";
|
|
|
+
|
|
|
+ CTDrawing drawing = null;
|
|
|
+ try {
|
|
|
+ drawing = CTDrawing.Factory.parse(anchorXML);
|
|
|
+ } catch (XmlException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ CTAnchor anchor = drawing.getAnchorArray(0);
|
|
|
+ anchor.setGraphic(ctGraphicalObject);
|
|
|
+ return anchor;
|
|
|
+ }
|
|
|
+
|
|
|
}
|