Springboot使用itext及documents4j操作pdf(word转pdf、pdf加水印(文字或图片,可指定位置)、pdf加密(打开密码,编辑密码))

pom.xml引入


        
            com.documents4j
            documents4j-local
            1.0.3
        
        
            com.documents4j
            documents4j-transformer-msoffice-word
            1.0.3
        

        
        
            com.itextpdf
            itextpdf
            5.5.11
        
        
            com.itextpdf
            itext-asian
            5.2.0
        

创建PDF操作工具类PdfUtils

package com.ruoyi.common.utils.pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.List;

public class PdfUtils {

    /**
     * pdf加密
     *
     * @param filePath  输入文件路径
     * @param savePath  输出文件路径
     * @param password  密码
     * @return
     */
    public static boolean pdfEncrypt(String filePath, String savePath, String password){
        try{
            PdfReader reader = new PdfReader(filePath);
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(savePath));

            //ALLOW_PRINTING	文档允许打印
            //ALLOW_DEGRADED_PRINTING	允许用户打印文档,但不提供allow_printing质量(128位加密)
            //ALLOW_MODIFY_CONTENTS	允许用户修改内容,例如 更改页面内容,或插入或删除页
            //ALLOW_ASSEMBLY	允许用户插入、删除和旋转页面和添加书签。页面的内容不能更改,除非也授予allow_modify_contents权限,(128位加密)
            //ALLOW_COPY	允许用户复制或以其他方式从文档中提取文本和图形,包括使用辅助技术。例如屏幕阅读器或其他可访问设备
            //ALLOW_SCREENREADERS	允许用户提取文本和图形以供易访问性设备使用,(128位加密)
            //ALLOW_MODIFY_ANNOTATIONS	允许用户添加或修改文本注释和交互式表单字段
            //ALLOW_FILL_IN	允许用户填写表单字段,(128位加密)

            //访问者密码,拥有者密码(权限密码让pdf文件无法被修改),访问者权限,加密方式。
            stamper.setEncryption(null, password.getBytes(),PdfWriter.ALLOW_COPY|PdfWriter.ALLOW_DEGRADED_PRINTING,PdfWriter.STANDARD_ENCRYPTION_128);
            stamper.close();
            reader.close();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * pdf添加水印
     *
     * @param inputFile     需要添加水印的文件
     * @param outputFile    添加完水印的文件存放路径
     * @param waterMarkName 需要添加的水印文字
     * @param opacity       水印字体透明度
     * @param fontsize      水印字体大小
     * @param angle         水印倾斜角度(0-360)
     * @param heightdensity 数值越大每页竖向水印越少
     * @param widthdensity  数值越大每页横向水印越少
     * @param cover         是否覆盖
     * @return
     */
    public static boolean addwaterMark(String inputFile, String outputFile, String waterMarkName,
                                       float opacity, int fontsize, int angle, int heightdensity, int widthdensity, boolean cover) {
        if (!cover) {
            File file = new File(outputFile);
            if (file.exists()) {
                return true;
            }
        }
        File file = new File(inputFile);
        if (!file.exists()) {
            return false;
        }

        PdfReader reader = null;
        PdfStamper stamper = 

你可能感兴趣的:(SpringBoot,java,itext)