赞
踩
1、文档打开口令:打开文件时的密码
2、许可口令:操作文件时的密码
工具千奇百怪,此处只举例本人用到的两个
Adobe Reader、WPS
Adobe Reader的用途是查看pdf文件属性的
WPS是用来验证许可口令的
(如果你不是加密许可口令则忽略它)
bcprov-jdk15on-1.46.jar
itext-2.0.6.jar
注意这俩jar包的版本是否互相配套
package com.company; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.PageSize; import com.lowagie.text.pdf.PdfWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class Main { public static final String path="D:/Download/333.pdf"; public static final byte[] pwd = "123".getBytes(); public static void main(String[] args) throws FileNotFoundException, DocumentException { //实现A4纸页面 并且横向显示(不设置则为纵向) Document document = new Document(PageSize.A4.rotate()); PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(path)); /* *setEncryption(col1,col2,col3,clo4) * col1=文档打开口令 * col2=许可口令 * col3=被限制的操作(不懂含义就翻译单词) * col4=false就完事 **/ pdfWriter.setEncryption(null, pwd, PdfWriter.AllowAssembly, false); pdfWriter.setEncryption(null, pwd, PdfWriter.AllowCopy, false); pdfWriter.setEncryption(null, pwd, PdfWriter.AllowDegradedPrinting, false); pdfWriter.setEncryption(null, pwd, PdfWriter.AllowFillIn, false); pdfWriter.setEncryption(null, pwd, PdfWriter.AllowModifyAnnotations, false); pdfWriter.setEncryption(null, pwd, PdfWriter.AllowModifyContents, false); pdfWriter.setEncryption(null, pwd, PdfWriter.AllowPrinting, false); pdfWriter.setEncryption(null, pwd, PdfWriter.AllowScreenReaders, false); pdfWriter.setEncryption(null, pwd, PdfWriter.DO_NOT_ENCRYPT_METADATA, false); pdfWriter.close(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。