当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

itext生成只读pdf文档

发表于: 2012-10-25   作者:ctrain   来源:转载   浏览次数:
摘要: 完成只读操作的代码为: writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128); 附:PdfWriter  setEncryption()方法的文档说明 setEncryption public void setEncryptio
完成只读操作的代码为:
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,结果就好了。

itext生成只读pdf文档

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
itextpdf-5.2.1.jar iText-2.1.7.jar iText-rtf-2.1.7.jar iTextAsian.ja 生成 pdf //Step 1—Creat
itextpdf-5.2.1.jar iText-2.1.7.jar iText-rtf-2.1.7.jar iTextAsian.ja 生成 pdf //Step 1—Creat
itextpdf-5.2.1.jar iText-2.1.7.jar iText-rtf-2.1.7.jar iTextAsian.ja 生成 pdf //Step 1—Creat
一:工程截图: 二:项目运行截图: 三:源代码: Book.java package com.iText.bean; public class
这个问题我在网上搜了很久,没有找到什么解决方案,需求其实很简单,就是添加背景图片。在解决这个
itext实现HTML转pdf网上有很多实现方案,可惜大部分没有提供完整的源码下载,而有的方案却随着itext
itextpdf-5.4.3.jar下载地址: http://www.kuaipan.cn/file/id_58980483773788178.htm 导入itextpdf
itextpdf-5.4.3.jar下载地址: http://www.kuaipan.cn/file/id_58980483773788178.htm 导入itextpdf
最近的项目中遇到了需求,用户在页面点击下载,将页面以PDF格式下载完成供用户浏览,所以上网找了下
第18章. iText PDF 生成 18.1. 使用PDF支持 18.1.1. 创建一个文档 18.1.2. 基本的文本元素 18.1.3.
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号