diff --git a/casic-metering-model/src/main/java/com/casic/missiles/dto/minio/ImageConfig.java b/casic-metering-model/src/main/java/com/casic/missiles/dto/minio/ImageConfig.java new file mode 100644 index 0000000..563227f --- /dev/null +++ b/casic-metering-model/src/main/java/com/casic/missiles/dto/minio/ImageConfig.java @@ -0,0 +1,59 @@ +package com.casic.missiles.dto.minio; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +/** + * @author cz + */ +@Data +public class ImageConfig { + + /** + * 文件名称 + */ + @ApiModelProperty("文件名称") + private String filedName; + + /** + * 审批标志 + */ + @ApiModelProperty("审批标志") + private String approvalFlag; + + /** + * 文档参数名称 + */ + @ApiModelProperty("所在文件参数名称") + private String docName; + /** + * 表位置 + */ + @ApiModelProperty("表位置") + private Integer tableIndex; + + /** + * 行位置 + */ + @ApiModelProperty("行位置") + private Integer rowIndex; + + /** + * 列位置 + */ + @ApiModelProperty("列位置") + private Integer colIndex; + + /** + * 行合并数 + */ + @ApiModelProperty("行合并数") + private Integer rowSpan; + + /** + * 列合并列数 + */ + @ApiModelProperty("列合并列数") + private Integer colSpan; + +} diff --git a/casic-metering-model/src/main/java/com/casic/missiles/dto/minio/ImageConfig.java b/casic-metering-model/src/main/java/com/casic/missiles/dto/minio/ImageConfig.java new file mode 100644 index 0000000..563227f --- /dev/null +++ b/casic-metering-model/src/main/java/com/casic/missiles/dto/minio/ImageConfig.java @@ -0,0 +1,59 @@ +package com.casic.missiles.dto.minio; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +/** + * @author cz + */ +@Data +public class ImageConfig { + + /** + * 文件名称 + */ + @ApiModelProperty("文件名称") + private String filedName; + + /** + * 审批标志 + */ + @ApiModelProperty("审批标志") + private String approvalFlag; + + /** + * 文档参数名称 + */ + @ApiModelProperty("所在文件参数名称") + private String docName; + /** + * 表位置 + */ + @ApiModelProperty("表位置") + private Integer tableIndex; + + /** + * 行位置 + */ + @ApiModelProperty("行位置") + private Integer rowIndex; + + /** + * 列位置 + */ + @ApiModelProperty("列位置") + private Integer colIndex; + + /** + * 行合并数 + */ + @ApiModelProperty("行合并数") + private Integer rowSpan; + + /** + * 列合并列数 + */ + @ApiModelProperty("列合并列数") + private Integer colSpan; + +} diff --git a/casic-metering-service/src/main/java/com/casic/missiles/service/listeners/register/DocPictureProvider.java b/casic-metering-service/src/main/java/com/casic/missiles/service/listeners/register/DocPictureProvider.java new file mode 100644 index 0000000..dbe2a65 --- /dev/null +++ b/casic-metering-service/src/main/java/com/casic/missiles/service/listeners/register/DocPictureProvider.java @@ -0,0 +1,140 @@ +package com.casic.missiles.service.listeners.register; + +import com.casic.missiles.dto.certificate.CropPicture; +import com.casic.missiles.dto.certificate.ElectronicImage; +import com.itextpdf.text.BadElementException; +import com.itextpdf.text.Image; +import com.itextpdf.text.Rectangle; +import com.itextpdf.text.pdf.PdfContentByte; +import com.itextpdf.text.pdf.PdfReader; +import com.itextpdf.text.pdf.PdfStamper; +import com.spire.doc.Document; +import com.spire.doc.FieldType; +import com.spire.doc.HeaderFooter; +import com.spire.doc.documents.HorizontalAlignment; +import com.spire.doc.documents.Paragraph; +import com.spire.doc.documents.TextWrappingStyle; +import com.spire.doc.fields.DocPicture; +import lombok.extern.slf4j.Slf4j; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.List; + +/** + * @author cz + */ +@Slf4j +public class DocPictureProvider { + + + /** + * 根据页数对图片的平等切割,返回切割后的图片 + * + * @param pageCount + * @param stampSealImgUrl + * @return + */ + public static List crosswiseCutting(int pageCount, String stampSealImgUrl) { + String[] imageCutOffContents = stampSealImgUrl.split("\\."); + CropPicture cropPicture = CropPicture.builder() + .targetImgUrl(stampSealImgUrl) + .cutCols(pageCount) + .cutRows(1) + .subfileUrl(imageCutOffContents[imageCutOffContents.length - 2]) + .subfileType(imageCutOffContents[imageCutOffContents.length - 1]) + .build(); + List subfileUrls = null; + try { + subfileUrls = ImageTool.doCrosswiseCutting(cropPicture); + } catch (IOException ex) { + ex.getStackTrace(); + } + return subfileUrls; + } + + /** + * 骑缝章只存在一个章在进行打印 + */ + public static void wordFullSeam(String pdfNewUrl, String stampSealImgUrl, String outFilePath) { + try { + PdfReader reader = new PdfReader(pdfNewUrl);//选择需要印章的pdf + PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outFilePath));//加完印章后的pdf + Rectangle pageSize = reader.getPageSize(1);//获得第一页 + float height = pageSize.getHeight(); + float width = pageSize.getWidth(); + int nums = reader.getNumberOfPages(); + List subFileUrls = DocPictureProvider.crosswiseCutting(nums, stampSealImgUrl); + Image[] nImage = subImages(subFileUrls, nums);//生成骑缝章切割图片 + for (int n = 1; n <= nums; n++) { + PdfContentByte over = stamp.getOverContent(n);//设置在第几页打印印章 + Image img = nImage[n - 1];//选择图片 + img.scalePercent(40, 40); + img.setAbsolutePosition(width - img.getWidth() *4/ 10, height / 2 - img.getHeight()*4 / 20);//控制图片位置 + over.addImage(img); + } + stamp.close(); + } catch (Exception ex) { + log.error("骑缝章生成异常,异常信息为{}", ex); + } + } + + private static Image[] subImages(List subFileUrls, int n) throws IOException, BadElementException { + Image[] nImage = new Image[n]; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + for (int i = 0; i < n; i++) { + BufferedImage img = ImageIO.read(new File(subFileUrls.get(i))); + String[] framges = subFileUrls.get(i).split("\\."); + ImageIO.write(img, framges[framges.length - 1], out); + nImage[i] = Image.getInstance(out.toByteArray()); + out.flush(); + out.reset(); + } + return nImage; + } + + /** + * 填充图片信息 + * + * @param paragraph + * @param absolutePathImage + */ + public static void populateDocPicture(Paragraph paragraph, ElectronicImage absolutePathImage) { + DocPicture docPicture = paragraph.appendPicture(absolutePathImage.getElectronicSealImageUrl()); +// 指定电子章位置 +// 水平位置 + docPicture.setHorizontalPosition(absolutePathImage.getHorizontal()); +// 垂直位置 + docPicture.setVerticalPosition(absolutePathImage.getVertical()); +// 设置电子章大小 + docPicture.setWidth(absolutePathImage.getStampWidth()); + docPicture.setHeight(absolutePathImage.getStampHeight()); +// 设置图片位于文字顶层 + docPicture.setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text); + } + + /** + * 添加页眉 + * + * @param document + */ + public static void headerFooter(Document document) { + int sectionCounts= document.getSections().getCount(); + for(int i=0;i crosswiseCutting(int pageCount, String stampSealImgUrl) { + String[] imageCutOffContents = stampSealImgUrl.split("\\."); + CropPicture cropPicture = CropPicture.builder() + .targetImgUrl(stampSealImgUrl) + .cutCols(pageCount) + .cutRows(1) + .subfileUrl(imageCutOffContents[imageCutOffContents.length - 2]) + .subfileType(imageCutOffContents[imageCutOffContents.length - 1]) + .build(); + List subfileUrls = null; + try { + subfileUrls = ImageTool.doCrosswiseCutting(cropPicture); + } catch (IOException ex) { + ex.getStackTrace(); + } + return subfileUrls; + } + + /** + * 骑缝章只存在一个章在进行打印 + */ + public static void wordFullSeam(String pdfNewUrl, String stampSealImgUrl, String outFilePath) { + try { + PdfReader reader = new PdfReader(pdfNewUrl);//选择需要印章的pdf + PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outFilePath));//加完印章后的pdf + Rectangle pageSize = reader.getPageSize(1);//获得第一页 + float height = pageSize.getHeight(); + float width = pageSize.getWidth(); + int nums = reader.getNumberOfPages(); + List subFileUrls = DocPictureProvider.crosswiseCutting(nums, stampSealImgUrl); + Image[] nImage = subImages(subFileUrls, nums);//生成骑缝章切割图片 + for (int n = 1; n <= nums; n++) { + PdfContentByte over = stamp.getOverContent(n);//设置在第几页打印印章 + Image img = nImage[n - 1];//选择图片 + img.scalePercent(40, 40); + img.setAbsolutePosition(width - img.getWidth() *4/ 10, height / 2 - img.getHeight()*4 / 20);//控制图片位置 + over.addImage(img); + } + stamp.close(); + } catch (Exception ex) { + log.error("骑缝章生成异常,异常信息为{}", ex); + } + } + + private static Image[] subImages(List subFileUrls, int n) throws IOException, BadElementException { + Image[] nImage = new Image[n]; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + for (int i = 0; i < n; i++) { + BufferedImage img = ImageIO.read(new File(subFileUrls.get(i))); + String[] framges = subFileUrls.get(i).split("\\."); + ImageIO.write(img, framges[framges.length - 1], out); + nImage[i] = Image.getInstance(out.toByteArray()); + out.flush(); + out.reset(); + } + return nImage; + } + + /** + * 填充图片信息 + * + * @param paragraph + * @param absolutePathImage + */ + public static void populateDocPicture(Paragraph paragraph, ElectronicImage absolutePathImage) { + DocPicture docPicture = paragraph.appendPicture(absolutePathImage.getElectronicSealImageUrl()); +// 指定电子章位置 +// 水平位置 + docPicture.setHorizontalPosition(absolutePathImage.getHorizontal()); +// 垂直位置 + docPicture.setVerticalPosition(absolutePathImage.getVertical()); +// 设置电子章大小 + docPicture.setWidth(absolutePathImage.getStampWidth()); + docPicture.setHeight(absolutePathImage.getStampHeight()); +// 设置图片位于文字顶层 + docPicture.setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text); + } + + /** + * 添加页眉 + * + * @param document + */ + public static void headerFooter(Document document) { + int sectionCounts= document.getSections().getCount(); + for(int i=0;i\n" + + " \n" + + " \n" + + " Aspose.Total for Java\n" + + " Aspose.Words for Java\n" + + " \n" + + " Enterprise\n" + + " 20991231\n" + + " 20991231\n" + + " 8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7\n" + + " \n" + + " sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=\n" + + ""; + try { + InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8")); + License aposeLic = new License(); + aposeLic.setLicense(license); + long old = System.currentTimeMillis(); + //新建一个pdf文档 + File file = new File(pdfPath); + FileOutputStream os = new FileOutputStream(file); + //Address是将要被转化的word文档 + Document doc = new Document(wordPath); + //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换 + doc.save(os, SaveFormat.PDF); + long now = System.currentTimeMillis(); + os.close(); + //转化用时 + System.out.println("Word 转 Pdf 共耗时:" + ((now - old) / 1000.0) + "秒"); + } catch (Exception e) { + System.out.println("Word 转 Pdf 失败..."); + e.printStackTrace(); + } + } +} +