@GetMapping(value = "/dypdf/", produces = MediaType.APPLICATION_PDF_VALUE)
    public ResponseEntity<InputStreamResource> dypdf(@RequestParam(value = "ztId") int ztId, @RequestParam(value = "year") int year,
                                                     @RequestParam(value = "month") int month, @RequestParam(value = "gzmbglId") int gzmbglId,
                                                     @RequestParam(value = "dyfs") String dyfs) throws DocumentException, IOException, WriterException {

        List<RslGzmbnrView> btList = gzmbnrglRepository.findAllByZtIdAndEnableAndGzmbglId(ztId, true, gzmbglId).stream().sorted(Comparator.comparing(Gzmbnrgl::getXh)).map(this::toRslGzmbnrView).collect(Collectors.toList());
        Gzmbgl gzmbgl = gzmbglRepository.findById(gzmbglId).orElseThrow();
        Bmrydygl bmrydygl = bmrydyglRepository.findByZtIdAndUserId(ztId, user.getId());

        List<JjjtbmPdfView> views = getJjjtbmPdfView(ztId, year, month, gzmbglId, bmrydygl);
        String qj = year + "年" + month + "月";

        var headers = new HttpHeaders();
        headers.add("Content-Disposition", "inline; filename=demo.pdf");
//不加水印
//        var bis = createDocument(btList, views, gzmbgl, bmrydygl, qj, dyfs);
        //加水印
        PdfReader reader = new PdfReader(createDocument(btList, views, gzmbgl, bmrydygl, qj, dyfs));
        var out = new ByteArrayOutputStream();
        PdfStamper stamper = new PdfStamper(reader, out);
        int total = reader.getNumberOfPages();
     //每一页都需要生成水印,从第一页开始
for (int i = 1; i <= total; i++) { PdfGState gs = new PdfGState(); // 透明度设置 gs.setFillOpacity(0.3f); // 水印在之前文本下 var under = stamper.getUnderContent(i); //水印在之前文本上 // under = stamp.getOverContent(i); under.setGState(gs); //一定需要setFontAndSize,要不然会报错
under.setFontAndSize(BaseFont.createFont("fonts/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 40);
//两个100表示坐标 under.showTextAligned(PdfContentByte.ALIGN_CENTER, "xxxx集团有限公司", 100, 100, 0); } stamper.close();// 关闭 var bis = new ByteArrayInputStream(out.toByteArray()); //图片水印 // var bis = createDocument1(btList, views, gzmbgl, bmrydygl, qj, dyfs, getClass().getResource("/p206.png")); return ResponseEntity .ok() .headers(headers) .contentType(MediaType.APPLICATION_PDF) .body(new InputStreamResource(bis)); } //自定义生成document private ByteArrayInputStream createDocument(List<RslGzmbnrView> btList, List<JjjtbmPdfView> views, Gzmbgl gzmbgl, Bmrydygl bmrydygl, String qj, String dyfs) throws DocumentException, IOException { /* Rectangle rectangle = new Rectangle(637.8f, 226.77f); var document = new Document(rectangle);*/ var document = new Document(PageSize.A4); var out = new ByteArrayOutputStream(); document.setPageSize(PageSize.A4); document.setMargins(10f, 10f, 10f, 10f); PdfWriter.getInstance(document, out); document.open(); //自定义生成表格 createTable(document, btList, views, gzmbgl, bmrydygl, qj, dyfs); document.close(); return new ByteArrayInputStream(out.toByteArray()); } private ByteArrayInputStream createDocument1(List<RslGzmbnrView> btList, List<JjjtbmPdfView> views, Gzmbgl gzmbgl, Bmrydygl bmrydygl, String qj, String dyfs, URL mark) throws DocumentException, WriterException, IOException { var out = new ByteArrayOutputStream(); // 原始pdf PdfReader reader = new PdfReader(createDocument(btList, views, gzmbgl, bmrydygl, qj, dyfs)); // 带水印pdf PdfStamper stamper = new PdfStamper(reader, out); Image water = Image.getInstance(); // 坐标 water.setAbsolutePosition(0, 0); /* // 旋转 弧度 water.setRotation(0); // 旋转 角度 water.setRotationDegrees(45);*/ // 依照比例缩放 water.scalePercent(1); // 原pdf文件的总页数 int pageSize = reader.getNumberOfPages(); for (int i = 1; i <= pageSize; i++) { PdfGState gs = new PdfGState(); // 透明度设置 gs.setFillOpacity(0.9f); // 水印在之前文本下 var under = stamper.getUnderContent(i); //水印在之前文本上 // under = stamp.getOverContent(i); // 图片水印 透明度 under.setGState(gs); // 图片水印 under.addImage(water); } stamper.close();// 关闭 return new ByteArrayInputStream(out.toByteArray()); }