Browse Source

itext对于部分文档水印不能处理问题解决

zoro 1 year ago
parent
commit
3d984586b1

+ 5 - 5
src/main/java/com/shcd/ghjg_pdf/service/impl/PDFServiceITextImpl.java

@@ -19,9 +19,9 @@ import java.nio.file.Paths;
 
 @Component
 public class PDFServiceITextImpl {
-    @PostConstruct
+    //@PostConstruct
     public void pdfEncode() throws Exception {
-        PdfReader pdfReader = new PdfReader("E:\\pdf\\DM8_DIsql使用手册.pdf");
+        PdfReader pdfReader = new PdfReader("E:\\pdf\\SMU749-050-09JS完整稳性计算报告.pdf");
         PdfStamper pdfStamper = new PdfStamper(pdfReader, Files.newOutputStream(Paths.get("E:\\pdf\\encrypted.pdf")));
         for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
             PdfContentByte underContent = pdfStamper.getUnderContent(i);
@@ -34,8 +34,8 @@ public class PDFServiceITextImpl {
         }
         pdfStamper.close();
         pdfReader.close();
-
-        PdfReader reader = new PdfReader("E:\\pdf\\encrypted.pdf");
+        System.out.println("finish");
+        /*PdfReader reader = new PdfReader("E:\\pdf\\encrypted.pdf");
         //HtmlConverter.convertToHtml(reader, new FileWriter("E:\\pdf\\encrypted.html"));
 
         PdfStamper stamper = new PdfStamper(reader, Files.newOutputStream(Paths.get("E:\\pdf\\encryptedwithJS.pdf")));
@@ -55,6 +55,6 @@ public class PDFServiceITextImpl {
         }
         stamper.flush();
         stamper.close();
-        reader.close();
+        reader.close();*/
     }
 }

+ 21 - 7
src/main/java/com/shcd/ghjg_pdf/service/impl/PDFServicePDFBOXImpl.java

@@ -1,27 +1,35 @@
 package com.shcd.ghjg_pdf.service.impl;
 
+import com.itextpdf.text.DocumentException;
+import com.itextpdf.text.pdf.PdfReader;
+import com.itextpdf.text.pdf.PdfStamper;
+import com.itextpdf.text.pdf.PdfWriter;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDPageContentStream;
 import org.apache.pdfbox.pdmodel.font.PDType1Font;
 import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.Date;
 
 @Component
-public class PDFServicePDFBOXImpl{
-    //@PostConstruct
-    public void PDFEncode() throws IOException {
-        PDDocument load = PDDocument.load(new File("E:\\pdf\\DM8_DIsql使用手册.pdf"));
+public class PDFServicePDFBOXImpl {
+    @PostConstruct
+    public void PDFEncode() throws IOException, DocumentException {
+        PDDocument load = PDDocument.load(new File("E:\\pdf\\SMU749-050-09JS完整稳性计算报告.pdf"));
         for (int i = 0; i < load.getNumberOfPages(); i++) {
             PDPageContentStream contentStream = new PDPageContentStream(load, load.getPage(i), PDPageContentStream.AppendMode.APPEND, true, true);
             // 设置字体和字号
             contentStream.setFont(PDType1Font.HELVETICA_BOLD, 36);
             //设置透明度
-            contentStream.setNonStrokingColor(200,200, 200, 150);
+            contentStream.setNonStrokingColor(200, 200, 200, 150);
             //添加水印
             contentStream.beginText();
-            contentStream.newLineAtOffset(100,100);
+            contentStream.newLineAtOffset(100, 100);
             Date date = new Date();
             int year = date.getYear() + 1900;
             int month = date.getMonth() + 1;
@@ -30,7 +38,13 @@ public class PDFServicePDFBOXImpl{
             contentStream.endText();
             contentStream.close();
         }
-        load.save("E:\\pdf\\DM8_DIsql使用手册_水印.pdf");
+        load.save("E:\\pdf\\SMU749-050-09JS完整稳性计算报告水印版.pdf");
         load.close();
+        PdfReader reader = new PdfReader("E:\\pdf\\SMU749-050-09JS完整稳性计算报告水印版.pdf");
+        PdfStamper stamper = new PdfStamper(reader, Files.newOutputStream(Paths.get("E:\\pdf\\SMU749-050-09JS完整稳性计算报告水印密码版.pdf")));
+        stamper.setEncryption("123456".getBytes(), "12345".getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_40);
+        stamper.flush();
+        stamper.close();
+        reader.close();
     }
 }