caozixuan 2 年之前
父节点
当前提交
499aed1dfb

+ 7 - 45
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/PdfUtil.java

@@ -151,13 +151,11 @@ public class PdfUtil {
     /**
      * 获取pdf格式
      *
-     * @param file
-     * @return
-     * @throws IOException
+     * @param file pdf
+     * @return pdf纸张格式
+     * @throws IOException 异常
      */
     public static PageSizeEnum getPdfFormat(File file) throws IOException {
-        float min;
-        float max;
         Optional.ofNullable(file).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("文件不能为空"));
         PdfReader reader = new PdfReader(ByteArray.fromFile(file).value());
         int totalPages = reader.getNumberOfPages();
@@ -166,25 +164,18 @@ public class PdfUtil {
             Rectangle pageSize = reader.getPageSize(i);
             float width = pageSize.getWidth();
             float height = pageSize.getHeight();
-            if (width <= height) {
-                min = width;
-                max = height;
-            } else {
-                min = height;
-                max = width;
-            }
             if (pageSizeEnum != null) {
-                if (!pageSizeEnum.equals(PageSizeEnum.convertToEnum(min, max))) {
+                if (!pageSizeEnum.equals(PageSizeEnum.convertToEnum(width, height))) {
                     throw ExceptionResultEnum.ERROR.exception("上传的试卷pdf中存在纸张格式不一样的");
                 }
             } else {
-                pageSizeEnum = PageSizeEnum.convertToEnum(min, max);
+                pageSizeEnum = PageSizeEnum.convertToEnum(width, height);
             }
             PdfDictionary pdfDictionary = reader.getPageN(i);
             PdfObject rotate = pdfDictionary.get(PdfName.ROTATE);
 //            log.info("fileName:{},width:{},height:{},pageSizeEnum:{},rotateInt:{}", file.getName(), width, height, pageSizeEnum, rotate);
             if (Objects.nonNull(rotate)) {
-                Integer rotateInt = Integer.parseInt(rotate.toString());
+                int rotateInt = Integer.parseInt(rotate.toString());
                 if (pageSizeEnum == PageSizeEnum.A3 && (rotateInt != 0 && rotateInt != 90)) {
                     throw ExceptionResultEnum.ERROR.exception("上传的A3样式pdf请调整方向");
                 } else if (pageSizeEnum == PageSizeEnum.A4 && rotateInt > 0) {
@@ -195,17 +186,6 @@ public class PdfUtil {
         return pageSizeEnum;
     }
 
-//    public static void main(String[] args) {
-//        String[] files = {
-//                "/Users/king/Downloads/测试附件/101_148382537591291904_signAndSticker.pdf",
-//                "/Users/king/Downloads/pdf/3120ee49668e4bf48fd8c161a2abb73b的副本A3.pdf",
-//                "/Users/king/Downloads/测试附件/101_158277997340655616_paperPackage.pdf"
-////                "/Users/king/Downloads/测试附件/101_148382537591291904_signAndSticker.pdf"
-//        };
-//        String outputPath = "/Users/king/Downloads/pdf/merge1.pdf";
-//        PdfUtil.mergePdf(files, outputPath);
-//    }
-
     /**
      * 两页打印到一页(A4 -> A3)
      *
@@ -219,8 +199,7 @@ public class PdfUtil {
         try {
 
             //读取PDF
-            InputStream pdf = streamOfPDFFile;
-            PdfReader pdfReader = new PdfReader(pdf);
+            PdfReader pdfReader = new PdfReader(streamOfPDFFile);
             int totalPages = pdfReader.getNumberOfPages();
 
             //创建PDF Writer
@@ -281,21 +260,4 @@ public class PdfUtil {
             }
         }
     }
-
-    public static void main(String[] args) {
-        File file2 = new File("E:\\file\\1.pdf");
-        try {
-            System.out.println(PdfUtil.getPdfFormat(file2));
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-
-        File file1 = new File("E:\\file\\1左.pdf");
-        try {
-            System.out.println(PdfUtil.getPdfFormat(file1));
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-
-    }
 }