iText学习 PDF添加密码 ASN1OctetString
加密PDF可以在生成PDF过程中加密, 代码示例:
PdfWriter writer = PdfWriter.getInstance(pdfdoc, new FileOutputStream(pdfFilename));
/** User password. */
public static byte[] USER = "TEST".getBytes();
/** Owner password. */
public static byte[] OWNER = "1234".getBytes();
writer.setEncryption(USER, OWNER, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
writer.createXmpMetadata();
pdfdoc.open();
。。。。。。
pdfdoc.close();
也可以后加工:
用户需求:通过邮件发送给客户的PDF文件,每个文件都有唯一的ID,通过Encrypt处理后,生成的ID注入到PDF文件中。当客户非法传递给其他非付费客户或发布到Internet上后,我们通过分析文件的ID来,对比本地数据库的USER-ID关系,通过法律部门进行维权处理。
主要功能:对已生成好的源PDF文件加入密码策略。通过Encrypt对PDF文件写入ID关键字,并setEncryption修改文档属性为不可修改即可。
★方法setKeywords主要增加关键字;pdf文件不能直接复制,通过计算当前PDF文件的总页数,然后进行一页页的写入。
1. 计算总页数 int n = reader.getNumberOfPages();
2. 新建PDF文件
PdfWriter writer =
PdfWriter.getInstance(document,new FileOutputStream("e:/newPDF.pdf"));
3. 创建写入直接内容对象
PdfContentByte cb = writer.getDirectContent();
4. 写入内容
PdfImportedPage page;
int rotation;
int i=0;
while(i<n)
{
i++; document.setPageSize(reader.getPageSizeWithRotation(i));
document.newPage();
page = writer.getImportedPage(reader, i);
rotation = reader.getPageRotation(i);
cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
★ 方法getKeywords主要取出pdf的文件信息;主要利用PdfReader的方法getInfo取当前PDF文件信息,返回对象为HashMap,输入对应key的val;
System.out.println(map.get("Author"));
System.out.println(map.get("Keywords"));
文件Encrypt.java如下
package com.txsec.pdf;
import java.io.FileOutputStream;
import java.util.HashMap;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfEncryption;
public class Encrypt {
public Encrypt() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
setPDFKeywords();
getKeywords();
}
public static void setPDFKeywords()
{
try{
PdfReader reader = new PdfReader("E:/1.pdf");
int n = reader.getNumberOfPages();
System.out.println("PDF总页数为:"+n);
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("e:/newPDF.pdf"));
document.addAuthor("uoovi");
document.addKeywords("123456789");
writer.setEncryption(null,null,
PdfWriter.ALLOW_MODIFY_ANNOTATIONS|PdfWriter.ALLOW_COPY|PdfWriter.ALLOW_PRINTING,
PdfWriter.STANDARD_ENCRYPTION_40);
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfImportedPage page;
int rotation;
int i=0;
while(i<n)
{
i++;
document.setPageSize(reader.getPageSizeWithRotation(i));
document.newPage();
page = writer.getImportedPage(reader, i);
rotation = reader.getPageRotation(i);
cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
document.close();
System.out.println("加密成功!");
}
catch(Exception e)
{
System.out.println(e.getStackTrace());
System.out.println(e.getMessage());
}
}
public static String getKeywords()
{
try{
PdfReader reader = new PdfReader("E:/newPDF.pdf");
Document document = new Document();
HashMap map = reader.getInfo();
System.out.println(map.toString());
map.get("Author");
System.out.println(map.get("Author"));
System.out.println(map.get("Keywords"));
return "";
}
catch(Exception e)
{
System.out.println(e.getMessage());
return "";
}
}
}
调用setEncryption会异常
writer.setEncryption(null,null,
PdfWriter.ALLOW_MODIFY_ANNOTATIONS|PdfWriter.ALLOW_COPY|PdfWriter.ALLOW_PRINTING,
PdfWriter.STANDARD_ENCRYPTION_40);
Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1OctetString
at com.lowagie.text.pdf.PdfEncryption.<init>(Unknown Source)
at com.lowagie.text.pdf.PdfWriter.setEncryption(Unknown Source)
at com.txsec.pdf.Encrypt.setPDFKeywords(Encrypt.java:39)
at com.txsec.pdf.Encrypt.main(Encrypt.java:23)
解决办法:下载 bcprov-jdk16-146.jar,然后加入到构建路径中
下载地址:
http://polydistortion.net/bc/download/