0
点赞
收藏
分享

微信扫一扫

PDF加密的实现方法

盖码范 2023-08-05 阅读 67


通过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);
    }

举报

相关推荐

0 条评论