完成只读操作的代码为:
writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
附:PdfWriter setEncryption()方法的文档说明
setEncryption
public void setEncryption(byte[] userPassword,
byte[] ownerPassword,
int permissions,
int encryptionType)
throws DocumentExceptionDescription copied from interface: PdfEncryptionSettings
Sets the encryption options for this document. The userPassword and the ownerPassword can be null or have zero length. In this case the ownerPassword is replaced by a random string. The open permissions for the document can be AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations, AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting. The permissions can be combined by ORing them.
Specified by:
setEncryption in interface PdfEncryptionSettings
Parameters:
userPassword - the user password. Can be null or empty
ownerPassword - the owner password. Can be null or empty
permissions - the user permissions
encryptionType - the type of encryption. It can be one of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128. Optionally DO_NOT_ENCRYPT_METADATA can be ored to output the metadata in cleartext
Throws:
DocumentException - if the document is already open
See Also:
PdfEncryptionSettings.setEncryption(byte[], byte[], int, int)
当userPassword,ownerPassword设置为null时,文档只读。也可以传入相应的byte[]对文档进行加密。
在制作加密文档的过程中产生如下异常:
严重: Servlet.service() for servlet ExcelServlet threw exception
java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1Primitive
at com.itextpdf.text.pdf.PdfEncryption.<init>(PdfEncryption.java:147)
at com.itextpdf.text.pdf.PdfWriter.setEncryption(PdfWriter.java:2049)
at com.ha.system.util.pdf.PdfUtil.writePdfFile(PdfUtil.java:57)
at com.ha.common.PdfServlet.doPost(PdfServlet.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.ydyd.common.util.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:57)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
在google搜到下面一个帖子:
http://itext-general.2136553.n4.nabble.com/iText-error-in-signing-big-PDF-files-SigDict-Contents-illegal-data-td4655309.html#a4655354其中:Paulo Soares-4 相关回答Paulo "You need BC 1.47. "
于是将bcprov-jdk15on-146.jar换成了bcprov-jdk15on-147.jar
jar包下载地址
结果又出现了下面的异常:
class org.bouncycastle.asn1.ASN1Primitive overrides final method equals.(Ljava/lang/Object;)Z
java.lang.VerifyError: class org.bouncycastle.asn1.ASN1Primitive overrides final method equals.(Ljava/lang/Object;)Z
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
刚开始以为是包没导进去,后来发现错误和原来的不一样,在网上找了半天没找到答案.也有人问
同样的问题,但是没人回答,最后想是不是还是jar包不对,于是将bcprov-jdk15on-147.jar换成了bcprov-ext-jdk15on-147.jar,结果就好了。
分享到:
相关推荐
在Java代码中使用itext生成PDF文档,实现PDF文档的导出。
itext 生成pdf 目录,itext 生成pdf 目录,itext 生成pdf 目录
itext生成PDF图片文档
使用IText生成PDF和WORD文档 包括源码和IText所需jar包
itext 生成pdf的中文帮助文档,具体有:图片、块(下划线/删除线、上标/下标)、短句、段落等。
Android使用iText生成pdf并读取pdf内容,iText是著名的开放源码的站点sourceforge的一个项目,可用来生成Pdf文件和读取Pdf文件内容.
此文档主要讲述了使用Java组件iText来生成PDF,操作PDF
用itext生成pdf文档,生成的内容有表格形式,文本形式,设置字体样式,文档页边距。
Android端用itext库生成的PDF文档无法输出中文,现修改itext库jar包,未修改源码,只是加入了字体部分。调用示例亲测可用,生成中文PDF文档
自己整理的 itext 生成的 pdf 的技术!
itext5.5.10生成PDF。itext5.5.10生成PDF。itext5.5.10生成PDF。itext5.5.10生成PDF。
使用itextpdf将html模板生成pdf文档,poi导出excel,freemarker导出excel,pdf添加文字水印。
iText生成pdf解决中文不显示字库,pdf凉字不显示,由于生成iText插件生成pdf的时候中文会显示不出来,遇到过的是"凉"字,查到是字体库的原因,网上下载字体库msyh.ttc,生成的时候指定字体库,就可以解决了,小bug一...
iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。 iText的安装非常方便,下载iText.jar文件后,...
java解决Itext生成PDF中文不换行的问题
Java iText 生成pdf文档源代码(包括插入文本、表格、图像)
iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。
itext5生成PDF(含水印文字及图片)的源代码,操作说明详细,代码完整可用。
iText 实现根据pdf模板生成pdf并导入数据.....iText-5.0.6.jar....附字体jar包 很久很久之前写的了 代码见: https://github.com/thisisthis/noh-doc
Text中文教程.pdf .........可生成pdf文件和rtf文件。h5转pdf 在示例代码0104中你可以看到该文档有一个0.5英寸的左边距和1英寸的右 边距,上边距为1.5英寸,下边距为2.5英寸 说明