diff --git a/src/main/java/com/emr/service/CommomService.java b/src/main/java/com/emr/service/CommomService.java index 6ad1b1c..3f4ad11 100644 --- a/src/main/java/com/emr/service/CommomService.java +++ b/src/main/java/com/emr/service/CommomService.java @@ -617,12 +617,6 @@ public class CommomService { Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER"); List outs = new LinkedList<>(); if (StringUtils.isNotBlank(names)) { - if (new File(WATERPICPATH).isDirectory()) { - FileUtils.deleteDirectory(new File(WATERPICPATH)); - } - if (!new File(WATERTIFTOJPGPATH).isDirectory()) { - FileUtils.deleteDirectory(new File(WATERTIFTOJPGPATH)); - } String[] rootPathList = rootPaths.split(","); String[] nameList = names.split(","); String[] sourceList = sources.split(","); diff --git a/src/main/java/com/emr/util/img2PdfUtil.java b/src/main/java/com/emr/util/img2PdfUtil.java index c3b9b38..407c396 100644 --- a/src/main/java/com/emr/util/img2PdfUtil.java +++ b/src/main/java/com/emr/util/img2PdfUtil.java @@ -467,6 +467,95 @@ public class img2PdfUtil { String imgFile, int imgWidth, int imgHeight, int imgX, int imgY) { PdfReader reader = null; PdfStamper stamper = null; + try { + reader = new PdfReader(bos.toByteArray()); + bos.reset(); + // 加完水印的文件 + stamper = getStamper(reader, bos, response); + + // 设置字体 + BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); + + // 循环对每页插入水印 + PdfContentByte content; + PdfGState gs = new PdfGState(); + // PDF总页数 + int total = reader.getNumberOfPages(); + for (int i = 1; i <= total; i++) { + //upOrUnder = 1为在文本之上 + content = getWatermarkContent(upOrUnder, stamper, i); + gs.setFillOpacity(transparent); + content.setGState(gs); + addImageWatermarkIfNecessary(content, isImg, imgFile, imgWidth, imgHeight, imgX, imgY);//图片水印 + addTextWatermark(content, effective, font, text, textColor, textSize, textRotation, textX, textY);//文字水印 + } + } catch (IOException | DocumentException e) { + logError(e); + } finally { + closeResources(stamper, reader); + } + } + + private static PdfStamper getStamper(PdfReader reader, ByteArrayOutputStream bos, HttpServletResponse response) throws IOException, DocumentException { + return response != null ? new PdfStamper(reader, response.getOutputStream()) : new PdfStamper(reader, bos); + } + + private static PdfContentByte getWatermarkContent(int upOrUnder, PdfStamper stamper, int pageIndex) { + return upOrUnder == 1 ? stamper.getOverContent(pageIndex) : stamper.getUnderContent(pageIndex); + } + + private static void addImageWatermarkIfNecessary(PdfContentByte content, Short isImg, String imgFile, int imgWidth, int imgHeight, int imgX, int imgY) throws DocumentException, IOException { + if (isImg != null && isImg == 1) { + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); + String imgStr = getValidImagePath(request, imgFile); + if (StringUtils.isNotBlank(imgStr) && new File(imgStr).isFile()) { + com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(imgStr); + image.setAbsolutePosition(imgX, imgY); + image.scaleToFit(imgWidth, imgHeight); + content.addImage(image); + } + } + } + + private static String getValidImagePath(HttpServletRequest request, String imgFile) { + String tomcatPath = request.getSession().getServletContext().getRealPath("/"); + return tomcatPath + "static/pdfWaterSet/upload/" + imgFile; + } + + private static void addTextWatermark(PdfContentByte content, Short effective , BaseFont font, String text, String textColor, int textSize, int textRotation, int textX, int textY) { + if (effective == 1 && StringUtils.isNotBlank(text)) { + Color color = toColorFromString(textColor); + content.beginText(); + content.setColorFill(color); + content.setFontAndSize(font, textSize); + content.setTextMatrix(textX, textY); + content.showTextAligned(Element.ALIGN_LEFT, text, textX, textY, textRotation); + content.endText(); + } + } + + private static void logError(Exception e) { + // 记录异常信息,例如日志记录 + e.printStackTrace(); + } + + private static void closeResources(PdfStamper stamper, PdfReader reader) { + if (stamper != null) { + try { + stamper.close(); + } catch (IOException | DocumentException e) { + e.printStackTrace(); + } + } + if (reader != null) { + reader.close(); + } + } + /*public static void addWaterMark(ByteArrayOutputStream bos, HttpServletResponse response, int upOrUnder, float transparent, String text, int textX, int textY, + String textColor, int textSize, int textRotation, Short effective, Short isImg, + String imgFile, int imgWidth, int imgHeight, int imgX, int imgY) { + PdfReader reader = null; + PdfStamper stamper = null; try { reader = new PdfReader(bos.toByteArray()); bos.reset(); // 重置 bos,以便 PdfStamper 可以写入 @@ -554,7 +643,7 @@ public class img2PdfUtil { reader.close(); } } - } + }*/ public static void addWaterMark(ByteArrayOutputStream bos, HttpServletResponse response, int upOrUnder, float transparent, String text, int textX, int textY, String textColor, int textSize, int textRotation, Short effective, Short isImg, String imgFile, int imgWidth, int imgHeight, int imgX, int imgY,String text1) {