Explorar o código

3.2.0-增加考号位数、增加试卷水印

xiaof %!s(int64=2) %!d(string=hai) anos
pai
achega
60771b67f1

+ 11 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/entity/BasicExamRule.java

@@ -63,6 +63,9 @@ public class BasicExamRule extends BaseEntity implements Serializable {
     @TableField("print_method")
     private PrintMethodEnum printMethod;
 
+    @TableField("exam_number_digit")
+    private Integer examNumberDigit;
+
     public static long getSerialVersionUID() {
         return serialVersionUID;
     }
@@ -122,4 +125,12 @@ public class BasicExamRule extends BaseEntity implements Serializable {
     public void setPrintMethod(PrintMethodEnum printMethod) {
         this.printMethod = printMethod;
     }
+
+    public Integer getExamNumberDigit() {
+        return examNumberDigit;
+    }
+
+    public void setExamNumberDigit(Integer examNumberDigit) {
+        this.examNumberDigit = examNumberDigit;
+    }
 }

+ 4 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/BasicExamRuleServiceImpl.java

@@ -65,7 +65,7 @@ public class BasicExamRuleServiceImpl extends ServiceImpl<BasicExamRuleMapper, B
             QueryWrapper<ExamPrintPlan> queryWrapper = new QueryWrapper<>();
             queryWrapper.lambda().eq(ExamPrintPlan::getSchoolId, schoolId).ne(ExamPrintPlan::getStatus, PrintPlanStatusEnum.END);
             List<ExamPrintPlan> examPrintPlanList = examPrintPlanService.list(queryWrapper);
-            if (examPrintPlanList != null && examPrintPlanList.size() > 0) {
+            if (examPrintPlanList != null && !examPrintPlanList.isEmpty()) {
                 // -- 可直接抛出异常下面的判断均可不用执行
                 BasicExamRule basicExamRule = this.getById(examRule.getId());
                 // 匹配扩展字段是否有变动
@@ -95,6 +95,9 @@ public class BasicExamRuleServiceImpl extends ServiceImpl<BasicExamRuleMapper, B
                         throw ExceptionResultEnum.ERROR.exception("有未结束的印刷计划,不能修改扩展字段");
                     }
                 }
+                if (!basicExamRule.getExamNumberDigit().equals(examRule.getExamNumberDigit())) {
+                    throw ExceptionResultEnum.ERROR.exception("有未结束的印刷计划,不能修改考号位数");
+                }
                 if (!basicExamRule.getReview().equals(examRule.getReview())) {
                     throw ExceptionResultEnum.ERROR.exception("有未结束的印刷计划,不能修改入库审核控制");
                 }

+ 3 - 2
distributed-print-business/src/main/java/com/qmth/distributed/print/business/templete/service/impl/TaskLogicServiceImpl.java

@@ -363,7 +363,8 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                         if (examStudentList != null && examStudentList.size() > 0) {
                             for (ExamStudent t : examStudentList) {
                                 if (Objects.nonNull(pdfDto)) {
-                                    createPdfUtil.getExamStudentPaperPdf(t.getPaperType(), paperPdfDto, studentPaperPdfList);
+                                    String[] waterMarkNames = {t.getStudentName(), t.getTicketNumber()};
+                                    createPdfUtil.getExamStudentPaperPdf(t.getPaperType(), paperPdfDto, studentPaperPdfList, waterMarkNames);
                                 }
                             }
                         } else if (examDetail.getPrintCount() != null) {
@@ -373,7 +374,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                                 int seq = atomicInteger.getAndIncrement();
                                 int mod = seq % examTaskDetail.getDrawCount();
                                 if (Objects.nonNull(pdfDto)) {
-                                    createPdfUtil.getExamStudentPaperPdf(paperTypes.get(mod), paperPdfDto, studentPaperPdfList);
+                                    createPdfUtil.getExamStudentPaperPdf(paperTypes.get(mod), paperPdfDto, studentPaperPdfList, null);
                                 }
                                 i++;
                             }

+ 5 - 5
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/CreatePdfUtil.java

@@ -758,13 +758,12 @@ public class CreatePdfUtil {
 
     /**
      * 获取试卷pdf
-     *
-     * @param stuPaperType 考生卷型
+     *  @param stuPaperType 考生卷型
      * @param paperPdfDto  抽取的试卷文件信息
      * @param pdfList      考生试卷集合
+     * @param waterMarkNames
      */
-    public PdfDto getExamStudentPaperPdf(String
-                                                 stuPaperType, List<PaperPdfDto> paperPdfDto, List<PdfDto> pdfList) throws IOException {
+    public PdfDto getExamStudentPaperPdf(String stuPaperType, List<PaperPdfDto> paperPdfDto, List<PdfDto> pdfList, String[] waterMarkNames) throws IOException {
         Set<Integer> pagesList = new HashSet<>();
         if (!CollectionUtils.isEmpty(paperPdfDto)) {
             for (PaperPdfDto dto : paperPdfDto) {
@@ -772,7 +771,8 @@ public class CreatePdfUtil {
                     int pages = dto.getPages();
                     pagesList.add(pages);
                     PdfDto pdfDto = PdfUtil.addPdfPage(dto.getFile());
-                    pdfList.add(new PdfDto(dto.getFile().getPath(), dto.getPageSize(), pdfDto.getPageCount()));
+                    File file = PdfUtil.addWaterMark(dto.getFile(), waterMarkNames, 1f, 12, 0);
+                    pdfList.add(new PdfDto(file.getPath(), dto.getPageSize(), pdfDto.getPageCount()));
                 }
             }
             int pageCount = pagesList.stream().mapToInt(Integer::intValue).sum();

+ 111 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/PdfUtil.java

@@ -1,7 +1,9 @@
 package com.qmth.distributed.print.business.util;
 
+import com.itextpdf.awt.AsianFontMapper;
 import com.itextpdf.text.Document;
-import com.itextpdf.text.PageSize;
+import com.itextpdf.text.DocumentException;
+import com.itextpdf.text.Element;
 import com.itextpdf.text.Rectangle;
 import com.itextpdf.text.pdf.*;
 import com.qmth.boot.tools.models.ByteArray;
@@ -19,8 +21,11 @@ import org.apache.commons.io.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.swing.*;
+import java.awt.*;
 import java.io.*;
 import java.time.LocalDateTime;
+import java.util.List;
 import java.util.*;
 
 /**
@@ -281,4 +286,109 @@ public class PdfUtil {
             }
         }
     }
+
+    /**
+     * pdf添加水印
+     *
+     * @param inputFile     需要添加水印的文件
+     * @param waterMarkName 需要添加的水印文字
+     * @param opacity       水印字体透明度
+     * @param fontsize      水印字体大小
+     * @param rotation      水印倾斜角度(0-360)
+     */
+    public static File addWaterMark(File inputFile, String[] waterMarkName, float opacity, int fontsize, int rotation) {
+        if (!inputFile.exists()) {
+            throw ExceptionResultEnum.ERROR.exception("试卷文件不存在");
+        }
+
+        if (waterMarkName.length == 0) {
+            return inputFile;
+        }
+
+        DictionaryConfig dictionaryConfig = SpringContextHolder.getBean(DictionaryConfig.class);
+        LocalDateTime nowTime = LocalDateTime.now();
+        StringJoiner stringJoiner = new StringJoiner("");
+        stringJoiner.add(dictionaryConfig.fssLocalPdfDomain().getConfig()).add(File.separator)
+                .add(UploadFileEnum.PAPER.getTitle()).add(File.separator)
+                .add(String.valueOf(nowTime.getYear())).add(File.separator)
+                .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
+                .add(String.format("%02d", nowTime.getDayOfMonth()))
+                .add(File.separator).add(SystemConstant.getUuid()).add(SystemConstant.PDF_PREFIX);
+        String outputPath = stringJoiner.toString();
+        PdfReader reader = null;
+        PdfStamper stamper = null;
+        File outputFile = null;
+        try {
+            outputFile = new File(outputPath);
+            if (!outputFile.exists()) {
+                outputFile.getParentFile().mkdirs();
+                outputFile.createNewFile();
+            }
+            reader = new PdfReader(new FileInputStream(inputFile));
+            stamper = new PdfStamper(reader, new FileOutputStream(outputPath));
+            BaseFont base = BaseFont.createFont(AsianFontMapper.ChineseSimplifiedFont, AsianFontMapper.ChineseSimplifiedEncoding_H, BaseFont.NOT_EMBEDDED);
+            PdfGState gs = new PdfGState();
+            //这里是透明度设置
+            gs.setFillOpacity(opacity);
+            //这里是条纹不透明度
+            gs.setStrokeOpacity(0.2f);
+            int total = reader.getNumberOfPages() + 1;
+            int textH = 0;
+            int textW = 0;
+            for (String s : waterMarkName) {
+                JLabel label = new JLabel();
+                FontMetrics metrics;
+
+                label.setText(s);
+                metrics = label.getFontMetrics(label.getFont());
+                //字符串的高,   只和字体有关
+                textH = metrics.getHeight();
+                //字符串的宽
+                textW = metrics.stringWidth(label.getText());
+            }
+
+            Rectangle pageRect;
+            PdfContentByte under;
+            //循环PDF,每页添加水印
+            for (int i = 1; i < total; i++) {
+                // 只打奇数页水印
+                if (i % 2 == 0) {
+                    continue;
+                }
+                pageRect = reader.getPageSizeWithRotation(i);
+                under = stamper.getOverContent(i);  //在内容上方添加水印
+                under.saveState();
+                under.setGState(gs);
+                under.beginText();
+                under.setFontAndSize(base, fontsize); //这里是水印字体大小
+                for (int j = 0; j < waterMarkName.length; j++) {
+                    under.showTextAligned(Element.ALIGN_LEFT, waterMarkName[j], 10, pageRect.getHeight() - 30 - (j * textH), rotation);
+                }
+                //添加水印文字
+                under.endText();
+            }
+            return outputFile;
+        } catch (IOException | DocumentException e) {
+            System.out.println("添加水印失败!错误信息为: " + e);
+            e.printStackTrace();
+        } finally {
+            //关闭流
+            if (stamper != null) {
+                try {
+                    stamper.close();
+                } catch (DocumentException e) {
+                    e.printStackTrace();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (reader != null) {
+                reader.close();
+            }
+            if (outputFile.exists()) {
+                outputFile.delete();
+            }
+        }
+        return null;
+    }
 }