首页 > 其他分享 >PDF加密的实现方法

PDF加密的实现方法

时间:2023-08-05 15:38:04浏览次数:48  
标签:pdfStamper PdfWriter 加密 fileAuth setEncryption PDF null 方法 public


通过pdfbox实现
pdfbox加密实现方式非常简单,当然这个类的功能不止加密,还有很多实现,具体参考官方demo和api https://pdfbox.apache.org/docs/2.0.13/javadocs/
pom依赖
<!-- pdfbox 目前最新版本是2.0.16 -->
<dependency>
	<groupId>org.apache.pdfbox</groupId>
	<artifactId>pdfbox</artifactId>
	<version>2.0.16</version>
</dependency>
<!--  -->
<dependency>
	<groupId>org.bouncycastle</groupId>
	<artifactId>bcprov-jdk15on</artifactId>
	<version>1.57</version>
</dependency>
PDFEncryptUtils.java
/**
 * <p>对pdf进行权限控制</p>
 * @author Calvin
 * @date 2019/07/17
 * @since v1.0
 */
public class PDFEncryptUtils {

	/**
	 * 加密
	 * @param fileName  文件名
	 * @param fileAuth 文件权限
	 * @throws Exception
	 */
	public static void encrypt(String fileName, FileAuth fileAuth) throws Exception {
		File file = new File(fileName);
		PDDocument document = PDDocument.load(file);
		AccessPermission permissions = new AccessPermission();
		//此处简单进行实现,具体还有很多个权限,此处只实现最常用的,打开,编辑,打印
		//权限中默认都可以操作
		permissions.setCanExtractContent(fileAuth.getOpen() != 1);
		permissions.setCanModify(fileAuth.getEdit() != 1);
		permissions.setCanPrint(fileAuth.getPrint() != 1);
		StandardProtectionPolicy policy = new StandardProtectionPolicy(fileAuth.getOwnerPassword(),
		        fileAuth.getUserPassword(), permissions);
		SecurityHandler handler = new StandardSecurityHandler(policy);
		handler.prepareDocumentForEncryption(document);
		PDEncryption encryption = new PDEncryption();
		encryption.setSecurityHandler(handler);
		document.setEncryptionDictionary(encryption);
		//保存原路径
		document.save(file.getPath());
	}
}
FileAuth.java
/**
 * <p> 用户权限</p>
 *
 * @author Calvin
 * @date 2019/07/15
 * @since v1.0
 */
public class FileAuth {


	/**
	 * 是否可以打开
	 */
	private int open;

	/**
	 * 是否可以编辑
	 */
	private int edit;

	/**
	 * 是否可以打印
	 */
	private int print;

	/**
	 * 所有者权限密码
	 */
	private String ownerPassword;

	/**
	 * 用户权限密码
	 */
	private String userPassword;

	public int getOpen() {
	    return open;
	}

	public void setOpen(int open) {
	    this.open = open;
	}

	public int getEdit() {
	    return edit;
	}

	public void setEdit(int edit) {
	    this.edit = edit;
	}

	public int getPrint() {
	    return print;
	}

	public void setPrint(int print) {
	    this.print = print;
	}

	public String getOwnerPassword() {
	    return ownerPassword;
	}

	public void setOwnerPassword(String ownerPassword) {
	    this.ownerPassword = ownerPassword;
	}

	public String getUserPassword() {
	    return userPassword;
	}

	public void setUserPassword(String userPassword) {
	    this.userPassword = userPassword;
	}
}
--------------------------------------------------------------------------------
利用itext-pdf给pdf加密
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>itextpdf</artifactId>
	<version>5.5.11</version>
	<!-- 最新版本是7.1.7-->
</dependency>
<!-- 这里贴一下
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>barcodes</artifactId>
    <version>7.1.7</version>
    <type>pom</type>
</dependency>

-->
PDFEncryptUtils.java
/**
 * 给pdf设置权限
 * @param pdfStamper pdf文件
 * @param fileAuth 文件权限密码
 * @throws IOException 文件异常
 * @throws DocumentException
 */
public static void encrypt(PdfStamper pdfStamper, FileAuth fileAuth) throws DocumentException, IOException {
        pdfStamper.setEncryption(null, null, PdfWriter.ALLOW_COPY, true);
        pdfStamper.setEncryption(null, null, PdfWriter.ALLOW_DEGRADED_PRINTING, true);
        pdfStamper.setEncryption(null, null, PdfWriter.ALLOW_FILL_IN, true);
        pdfStamper.setEncryption(null, null, PdfWriter.ALLOW_MODIFY_ANNOTATIONS, true);
        pdfStamper.setEncryption(null, null, PdfWriter.ALLOW_MODIFY_CONTENTS, true);
        pdfStamper.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, true);
        byte[] userpassword = fileAuth.getUserPassword().getBytes();
        byte[] owenerpassword = fileAuth.getOwnerPassword().getBytes();
           if(fileAuth.getOpen() == 1){
               pdfStamper.setEncryption(userpassword, userpassword, PdfWriter.ALLOW_SCREENREADERS, false);
           }
           if(fileAuth.getEdit() == 1){
               pdfStamper.setEncryption(userpassword, owenerpassword, PdfWriter.ALLOW_PRINTING, false);
               pdfStamper.setEncryption(userpassword, owenerpassword, PdfWriter.ALLOW_DEGRADED_PRINTING,
                       false);
           }
           if(fileAuth.getPrint() == 1){
               pdfStamper.setEncryption(userpassword, owenerpassword, PdfWriter.ALLOW_MODIFY_ANNOTATIONS,
                       false);
               pdfStamper.setEncryption(userpassword, owenerpassword, PdfWriter.ALLOW_MODIFY_CONTENTS, false);
               pdfStamper.setEncryption(userpassword, owenerpassword, PdfWriter.ALLOW_FILL_IN, false);
           }
           pdfStamper.close();
    }
Client调用
public static void main(String[] args) throws IOException, DocumentException, NoSuchFieldException, IllegalAccessException {
        PdfReader reader = new PdfReader("D:\\4028832b6c4af5e2016c4af694310044.pdf");
        java.lang.reflect.Field f = reader.getClass().getDeclaredField("encrypted");
        f.setAccessible(true);
        f.set(reader, false);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("D:\\test1.pdf"));
        FileAuth fileAuth = new FileAuth();
        fileAuth.setEdit(1);
        fileAuth.setOpen(1);
        fileAuth.setPrint(1);
        fileAuth.setUserPassword("123456");
        fileAuth.setOwnerPassword("654321");
        encrypt(stamper, fileAuth);
    }

标签:pdfStamper,PdfWriter,加密,fileAuth,setEncryption,PDF,null,方法,public
From: https://blog.51cto.com/u_16111396/6975453

相关文章

  • 传奇引擎知识分享传奇GEE引擎设置装备物品绑定的方法
    功能:设置新的装备绑定功能.(专用登录器)SetItemBind,设置物品和人物绑定绑定后物品属性会显示“已绑定”格式:SetItemBind装备位置(-1~13,-1时为OK框中物品)绑定(0-1)说明:参数20=取消1=绑定例子:绑定武器.#IFCheckGold10000#ACTSetItemBind11Take金币10000例子:取消......
  • 安装 配置 正向 解析 DNS方法
    安装配置正向解析DNS方法1,安装dhcp[root@localhost~]#yuminstallbind*-y2,关闭防火墙和selinux[root@localhost~]#systemctlstopfirewalld.service关闭防火墙[root@localhost~]#setenforce0关闭selinux3,查看bind文件列表[root@localhost~]#rpm-qlbind......
  • 最简单的Qt连接MYSQL的方法
    最简单的Qt连接MYSQL的方法⭐当我试图在项目中连接本地的mysql时,反复出现:QMYSQLdrivernotloaded,显示没有成功加载mysql的驱动,在网上查询了很多教程和视频,大多为互相转载且老旧,耗费了大半天还是没有构建成功,通常的解决方法是在本地构建mysql驱动(通过安装qt时勾选的src选项里......
  • 肖健雄(Jianxiong Xiao)的开源SFM代码SFMedu的运行方法
    注意:本文是针对肖健雄(JianxiongXiao)博士的的开源SFM代码SFMedu(https://github.com/jianxiongxiao/SFMedu)的运行方法。本人的运行环境:操作系统:Window1064位;Matalb:MatalbR2013b;C++IDE:MicrosoftVisualStudio2010;编译器:MicrosoftVisualC++2010。SFMedu(https://gith......
  • 基于雷达影像的洪水监测技术方法详解
    洪水发生时候大多数是阴雨天气,光学影像基本上拍不到有效影像。雷达影像这时候就能发挥其不受天气影像的优点。现在星载的雷达卫星非常多,如高分三号、陆探一号、海丝一号(巢湖一号)、哨兵1号等。本文以哨兵1号L1地距(GRD)产品来介绍在洪水监测中的处理技术,其他雷达数据处理类似。处......
  • vue 方法整理
    1、props  传值(可以使用props属性来进行组件之间的数据传递)单向数据流props传值是单向的:父组件的数据可以传给子组件,而子组件的数据不能传给父组件,这是为了防止子组件无意修改了父组件的状态,每次父组件更新时,子组件的所有prop都会更新为最新值。这意味着不应该在子组件内......
  • 传奇架设技术传奇引擎BLUEM2引擎中任意魔法接口设置方法
    功能:任意魔法接口.不再限制为几个简单的魔法了.使用此引擎的朋友也可以Diy魔法了示例:目标触发为[@MagTagFuncXXX].当前人物触发为[@MagSelfFuncXXX].XXX为魔法ID.建议做大点.不要太接近现有的魔法ID.;新增魔法ID为248的魔法.鼠标有目标时则触发QFunction-0.txt中的[@MagTagFunc248]......
  • web前端技能方法总结(css、js、jquery、html)(3)
    HTML(HyperTextMarkupLanguage)就是超文本标记语言。"超文本"就是表示页面内可以包含非文字元素,如:图片、链接、音乐等等。它是一种建立网页文件的语言,通过标记式的指令(Tag),将影像、声音、图片、文字等链接显示出来。这种标记性语言是因特网上网页的主要语言。HTML网页文件可以使用......
  • 传奇引擎知识传奇GOM引擎自定义怪物appr代码计算方法分享
    GOM引擎自定义怪物appr代码计算方法和公式dbc2000打开db数据库里面monster.db(怪物的数据库),找到这个自定义怪物的名字,看他后面的第三行就是Appr的代码,在一些其他辅助工具里面叫形象代码,例如:天道圣主⑨的怪物appr代码是608,他对应的pak补丁就是mon61.pak,他的计算方法就是(61-1)*10=6......
  • 一文弄懂什么是DNS、A记录、CNAME以及使用方法
    域名解析DNS简介域名解析(DomainNameSystem,DNS)是互联网中用于将人类可读的域名(例如www.example.com)转换为计算机可理解的IP地址(例如192.168.1.1)的系统。它充当了互联网上的一个“电话簿”,帮助将用户提供的域名映射到实际的网络地址,使得计算机能够找到并连接到相应的网络服务器。白......