dcs 2 months ago
parent
commit
73e97dbf7c

+ 1 - 1
virgo.file/src/main/java/com/bosshand/virgo/file/controller/WorkarkContractController.java

@@ -162,7 +162,7 @@ public class WorkarkContractController {
         OutputStream outputStream = null;
         OutputStream outputStream = null;
         try {
         try {
             WorkarkContractPdf workarkContractPdf = workarkContractService.getWorkarkContractPdf(pdfId);
             WorkarkContractPdf workarkContractPdf = workarkContractService.getWorkarkContractPdf(pdfId);
-            data = workarkContractService.getWorkarkContractPdfData(pdfId);
+            data = workarkContractService.showPdf(pdfId);
             response.setCharacterEncoding("UTF-8");
             response.setCharacterEncoding("UTF-8");
             response.setContentType("application/x-msdownload");
             response.setContentType("application/x-msdownload");
             response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(workarkContractPdf.getName(), "UTF-8"));
             response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(workarkContractPdf.getName(), "UTF-8"));

+ 27 - 0
virgo.file/src/main/java/com/bosshand/virgo/file/service/WorkarkContractService.java

@@ -1,5 +1,6 @@
 package com.bosshand.virgo.file.service;
 package com.bosshand.virgo.file.service;
 
 
+import com.bosshand.virgo.core.utils.FileUtil;
 import com.bosshand.virgo.exception.ServiceException;
 import com.bosshand.virgo.exception.ServiceException;
 import com.bosshand.virgo.file.dao.WorkarkContractDao;
 import com.bosshand.virgo.file.dao.WorkarkContractDao;
 import com.bosshand.virgo.file.dao.WorkarkContractPdfDao;
 import com.bosshand.virgo.file.dao.WorkarkContractPdfDao;
@@ -9,6 +10,7 @@ import com.bosshand.virgo.file.model.WorkarkContractPdf;
 import com.bosshand.virgo.file.model.WorkarkContractPdfSeal;
 import com.bosshand.virgo.file.model.WorkarkContractPdfSeal;
 import com.bosshand.virgo.file.util.AliyunOSSUtil;
 import com.bosshand.virgo.file.util.AliyunOSSUtil;
 import com.bosshand.virgo.file.util.Doc2Pdf;
 import com.bosshand.virgo.file.util.Doc2Pdf;
+import com.bosshand.virgo.file.util.PdfKeywordFinder;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
@@ -158,4 +160,29 @@ public class WorkarkContractService {
     }
     }
 
 
 
 
+    public byte[] showPdf(long pdfId) {
+
+        List<WorkarkContractPdfSeal> pdfSealList = getPdfSealList(pdfId);
+
+        byte[] workarkContractPdfData = getWorkarkContractPdfData(pdfId);
+
+        byte[] contractPdf = null;
+
+        for (WorkarkContractPdfSeal pdfSeal : pdfSealList) {
+            byte[] bytes = FileUtil.readFromUrl(pdfSeal.getPictureAddress());
+            if(contractPdf != null){
+                contractPdf = PdfKeywordFinder.addPicturesPage(contractPdf,
+                        bytes, pdfSeal.getPageSize(), pdfSeal.getPageLeft(), pdfSeal.getPageTop(), pdfSeal.getPictureWidth(), pdfSeal.getPictureHeight());
+
+            } else {
+            contractPdf = PdfKeywordFinder.addPicturesPage(workarkContractPdfData,
+                    bytes, pdfSeal.getPageSize(), pdfSeal.getPageLeft(), pdfSeal.getPageTop(), pdfSeal.getPictureWidth(), pdfSeal.getPictureHeight());
+            }
+        }
+
+        return contractPdf;
+
+    }
+
+
 }
 }

+ 34 - 0
virgo.file/src/main/java/com/bosshand/virgo/file/util/PdfKeywordFinder.java

@@ -3,6 +3,7 @@ package com.bosshand.virgo.file.util;
 import com.itextpdf.awt.geom.Rectangle2D.Float;
 import com.itextpdf.awt.geom.Rectangle2D.Float;
 import com.itextpdf.text.DocumentException;
 import com.itextpdf.text.DocumentException;
 import com.itextpdf.text.Image;
 import com.itextpdf.text.Image;
+import com.itextpdf.text.Rectangle;
 import com.itextpdf.text.pdf.*;
 import com.itextpdf.text.pdf.*;
 import com.itextpdf.text.pdf.parser.*;
 import com.itextpdf.text.pdf.parser.*;
 
 
@@ -335,5 +336,38 @@ public class PdfKeywordFinder {
 		}
 		}
 		return null;
 		return null;
 	}
 	}
+
+	public static byte[] addPicturesPage(byte[] pdfdata, byte[] picture, int pageSize, int left, int top, int pictureWidth, int pictureHeight) {
+		ByteArrayOutputStream out = new ByteArrayOutputStream();
+		PdfStamper pdfStamper = null;
+		try {
+			PdfReader pdfReader = new PdfReader(pdfdata);
+			pdfStamper = new PdfStamper(pdfReader, out);
+
+			if(picture != null) {
+				Image image = Image.getInstance(picture);
+				image.scaleToFit(pictureWidth, pictureHeight);
+				Rectangle aa = pdfReader.getPageSizeWithRotation(pageSize);
+				image.setAbsolutePosition(left, aa.getHeight() - top - pictureHeight);
+
+				PdfContentByte overContent = pdfStamper.getOverContent(pageSize);
+				overContent.addImage(image);
+				overContent.stroke();
+			}
+			pdfStamper.close();
+			return out.toByteArray();
+		} catch (IOException | DocumentException e) {
+			e.printStackTrace();
+		} finally {
+			if (out != null) {
+				try {
+					out.close();
+				} catch (IOException e) {
+				}
+			}
+		}
+		return null;
+	}
+
 	
 	
 }
 }