package com.platform.common.utils;@b@@b@import org.apache.pdfbox.Loader;@b@import org.apache.pdfbox.pdmodel.*;@b@import org.apache.pdfbox.pdmodel.font.PDType1Font;@b@import org.apache.pdfbox.pdmodel.font.Standard14Fonts;@b@import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;@b@import org.apache.pdfbox.util.Matrix;@b@@b@import java.awt.*;@b@import java.io.File;@b@import java.io.IOException;@b@@b@/**@b@ * @author 李鹏军@b@ */@b@public class PdfUtils {@b@ /**@b@ * pdf添加文字水印@b@ *@b@ * @param filePath 原PDF位置@b@ * @param attrName PDF文件名称@b@ * @param text 要添加的水印@b@ * @return 添加水印的文件路径@b@ * @throws Exception Exception@b@ */@b@ public static String addWatermark(String filePath, String attrName, String text) {@b@ String result = filePath + File.separator + "autoMail" + File.separator + attrName;@b@ File file = new File(filePath + attrName);@b@ try {@b@ float formWidth = 35f;@b@ float formHeight = 35f;@b@ float fontSize = 25;@b@ int length1 = 14;@b@ int length2 = 22;@b@ int length3 = 46;@b@ int length4 = 62;@b@ if (text.length() <= length1) {@b@ formWidth = 2.4f;@b@ formHeight = 2.4f;@b@ fontSize = 50;@b@ } else if (text.length() <= length2) {@b@ formWidth = 4;@b@ formHeight = 4;@b@ fontSize = 50;@b@ } else if (text.length() <= length3) {@b@ formWidth = 10;@b@ formHeight = 10;@b@ fontSize = 40;@b@ } else if (text.length() <= length4) {@b@ formWidth = 20;@b@ formHeight = 20;@b@ }@b@ PDDocument document = Loader.loadPDF(file);@b@ document.setAllSecurityToBeRemoved(true);@b@ for (PDPage page : document.getPages()) {@b@ float width = page.getBBox().getWidth();@b@ float height = page.getBBox().getHeight();@b@@b@ PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);@b@ //pdf扩展图形对象@b@ PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();@b@ // 透明度@b@ graphicsState.setNonStrokingAlphaConstant(0.2f);@b@ graphicsState.setAlphaSourceFlag(true);@b@ cs.setGraphicsStateParameters(graphicsState);@b@ cs.setNonStrokingColor(Color.red);@b@ cs.beginText();@b@ cs.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA_BOLD), fontSize);@b@ // 获取旋转实例@b@ cs.setTextMatrix(Matrix.getRotateInstance(Math.toRadians(45), width / formWidth, height / formHeight));@b@ cs.showText(text);@b@ cs.endText();@b@ cs.close();@b@ }@b@ document.save(result);@b@ document.close();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ return result;@b@ }@b@@b@ /**@b@ * pdf去除水印@b@ */@b@ public static boolean removeWatermark(File file) {@b@ try {@b@ PDDocument document = Loader.loadPDF(file);@b@ // 移除加密@b@ document.setAllSecurityToBeRemoved(true);@b@ PDPageTree pages = document.getPages();@b@ for (PDPage page : pages) {@b@ PDResources resources = page.getResources();@b@ resources.getExtGStateNames().forEach(ext -> {@b@ PDExtendedGraphicsState gState = resources.getExtGState(ext);@b@ gState.setNonStrokingAlphaConstant(0.0f);@b@ });@b@ }@b@ String folderPath = file.getParent();@b@ String fileName = file.getName().substring(0, file.getName().lastIndexOf("."));@b@ document.save(folderPath + File.separator + fileName + "_无水印" + ".pdf");@b@ document.close();@b@ return true;@b@ } catch (IOException ex) {@b@ ex.printStackTrace();@b@ return false;@b@ }@b@ }@b@@b@ /**@b@ * pdf去除加密@b@ */@b@ public static boolean removePwd(File file) {@b@ try {@b@ PDDocument document = Loader.loadPDF(file);@b@ // 移除加密@b@ document.setAllSecurityToBeRemoved(true);@b@ String folderPath = file.getParent();@b@ String fileName = file.getName().substring(0, file.getName().lastIndexOf("."));@b@ document.save(folderPath + File.separator + fileName + "_无加密" + ".pdf");@b@ document.close();@b@ return true;@b@ } catch (IOException ex) {@b@ ex.printStackTrace();@b@ return false;@b@ }@b@ }@b@@b@ public static void main(String[] args) {@b@ removeWatermark(new File("/Users/pengjun/828.pdf"));@b@ }@b@}