Explorar el Código

pdf 判断字体反向

caozixuan hace 3 años
padre
commit
464610637d

+ 21 - 9
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/PdfUtil.java

@@ -1,5 +1,6 @@
 package com.qmth.distributed.print.business.util;
 
+import com.alibaba.fastjson.JSON;
 import com.itextpdf.text.Document;
 import com.itextpdf.text.Rectangle;
 import com.itextpdf.text.pdf.*;
@@ -165,21 +166,15 @@ public class PdfUtil {
         PageSizeEnum pageSizeEnum = null;
         for (int i = 1; i <= totalPages; i++) {
             Rectangle pageSize = reader.getPageSize(i);
+            System.out.println(JSON.toJSONString(pageSize));
             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);
             }
         }
 
@@ -272,4 +267,21 @@ 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();
+        }
+
+    }
 }

+ 3 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/enums/PageSizeEnum.java

@@ -111,6 +111,9 @@ public enum PageSizeEnum {
             if (standardWidth - deviation <= width && width <= standardWidth + deviation && standardHeight - deviation <= height && height <= standardHeight + deviation){
                 return e;
             }
+            if (standardWidth - deviation <= height && height <= standardWidth + deviation && standardHeight - deviation <= width && width <= standardHeight + deviation){
+                throw ExceptionResultEnum.ERROR.exception("请检查上传的【"+ e.name() + "】纸张字体方向是否反了");
+            }
         }
         throw ExceptionResultEnum.ERROR.exception("pdf格式不符合标准规范,请提供标准的pdf(可使用wps、office的word转换)");
     }