itext読み取り専用pdfドキュメントの生成

5037 ワード

読み取り専用操作を完了するコードは次のとおりです.

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.(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-jdk 15 on-14.6をJArをbcprov-jdk 15 on-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-jdk 15 on-147をJArをbcprov-ext-jdk 15 on-147に変更しました.jar、結果はよかった.