当前位置:   article > 正文

itext生成只读pdf文档_itextpdf实现对pdf设置为只读

itextpdf实现对pdf设置为只读

完成只读操作的代码为:

  1. writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);

附:PdfWriter  setEncryption()方法的文档说明

  1. setEncryption
  2. public void setEncryption(byte[] userPassword,
  3. byte[] ownerPassword,
  4. int permissions,
  5. int encryptionType)
  6. throws DocumentExceptionDescription copied from interface: PdfEncryptionSettings
  7. 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.
  8. Specified by:
  9. setEncryption in interface PdfEncryptionSettings
  10. Parameters:
  11. userPassword - the user password. Can be null or empty
  12. ownerPassword - the owner password. Can be null or empty
  13. permissions - the user permissions
  14. 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
  15. Throws:
  16. DocumentException - if the document is already open
  17. See Also:
  18. PdfEncryptionSettings.setEncryption(byte[], byte[], int, int)

当userPassword,ownerPassword设置为null时,文档只读。也可以传入相应的byte[]对文档进行加密。 
在制作加密文档的过程中产生如下异常: 

  1. 严重: Servlet.service() for servlet ExcelServlet threw exception
  2. java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1Primitive
  3. at com.itextpdf.text.pdf.PdfEncryption.<init>(PdfEncryption.java:147)
  4. at com.itextpdf.text.pdf.PdfWriter.setEncryption(PdfWriter.java:2049)
  5. at com.ha.system.util.pdf.PdfUtil.writePdfFile(PdfUtil.java:57)
  6. at com.ha.common.PdfServlet.doPost(PdfServlet.java:97)
  7. at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
  8. at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
  9. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
  10. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  11. at com.ydyd.common.util.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:57)
  12. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
  13. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  14. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
  15. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
  16. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
  17. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
  18. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
  19. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
  20. at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
  21. at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
  22. at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
  23. 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包下载地址 
结果又出现了下面的异常:

  1. class org.bouncycastle.asn1.ASN1Primitive overrides final method equals.(Ljava/lang/Object;)Z
  2. java.lang.VerifyError: class org.bouncycastle.asn1.ASN1Primitive overrides final method equals.(Ljava/lang/Object;)Z
  3. at java.lang.ClassLoader.defineClass1(Native Method)
  4. at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
  5. at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
  6. at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
  7. at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
  8. at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
  9. at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
  10. at java.security.AccessController.doPrivileged(Native Method)
  11. at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
  12. at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
  13. at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

刚开始以为是包没导进去,后来发现错误和原来的不一样,在网上找了半天没找到答案.也有人问同样的问题,但是没人回答,最后想是不是还是jar包不对,于是将bcprov-jdk15on-147.jar换成了bcprov-ext-jdk15on-147.jar,结果就好了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/202814
推荐阅读
相关标签
  

闽ICP备14008679号