合并pdf与数字签名与文本5.4.0或最大给出错误



我有一个大问题,试图合并一些pdf,创建与文本,与数字签名。

这是代码:

             ByteArrayOutputStream  bos = new ByteArrayOutputStream();
             Document document = new Document();
             PdfCopy writer = new PdfCopy(document, bos);
             writer.setMergeFields();                
             document.addTitle("");
             document.open();
             PdfReader reader = null;
             for (int i = 0; i < iss.length; i++) {
                 reader = new PdfReader(iss[i]);                     
                 writer.addDocument(reader);
                 reader.close();
             }
             document.close();
             bos.close();

执行时收到如下错误:

java.lang.IllegalStateException: Already closed
at com.itextpdf.text.io.ArrayRandomAccessSource.get(ArrayRandomAccessSource.java:65)
at com.itextpdf.text.io.IndependentRandomAccessSource.get(IndependentRandomAccessSource.java:78)
at com.itextpdf.text.pdf.RandomAccessFileOrArray.read(RandomAccessFileOrArray.java:226)
at com.itextpdf.text.pdf.RandomAccessFileOrArray.readFully(RandomAccessFileOrArray.java:248)
at com.itextpdf.text.pdf.RandomAccessFileOrArray.readFully(RandomAccessFileOrArray.java:242)
at com.itextpdf.text.pdf.PdfReader.getStreamBytesRaw(PdfReader.java:2522)
at com.itextpdf.text.pdf.PdfReader.getStreamBytesRaw(PdfReader.java:2559)
at com.itextpdf.text.pdf.PRStream.toPdf(PRStream.java:230)
at com.itextpdf.text.pdf.PdfIndirectObject.writeTo(PdfIndirectObject.java:158)
at com.itextpdf.text.pdf.PdfWriter$PdfBody.write(PdfWriter.java:420)
at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:398)
at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:377)
at com.itextpdf.text.pdf.PdfWriter.addToBody(PdfWriter.java:868)
at com.itextpdf.text.pdf.PdfCopyFieldsImp.closeIt(PdfCopyFieldsImp.java:439)
at com.itextpdf.text.pdf.PdfCopyFieldsImp.close(PdfCopyFieldsImp.java:397)
at com.itextpdf.text.pdf.PdfCopyFields.close(PdfCopyFields.java:159)
at it.almavivaitalia.commons.crystal.utils.ReportClientDocumentManager.mergePdfs(ReportClientDocumentManager.java:310)
at it.almavivaitalia.commons.crystal.utils.ReportClientDocumentManager.exportReportSchedulationPDFMerged(ReportClientDocumentManager.java:632)
at it.consip.pat.common.web.controller.AbstractReportCtrl.exportPDFMerged(AbstractReportCtrl.java:388)
at it.consip.pat.common.web.controller.AbstractReportCtrl.exportReportSchedulationPDFMerged(AbstractReportCtrl.java:334)
at it.consip.fgen.web.interrogazioni.controllers.RicEseConsStampeCtrl.stampaAll(RicEseConsStampeCtrl.java:530)
at it.consip.fgen.web.interrogazioni.controllers.RicEseConsStampeCtrl$$FastClassByCGLIB$$d94d9dbb.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:67)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
at it.consip.fgen.web.interrogazioni.controllers.RicEseConsStampeCtrl$$EnhancerByCGLIB$$b8912b3f_2.stampaAll(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1093)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter.doFilter(AbstractPreAuthenticatedProcessingFilter.java:89)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:206)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:842)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:648)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:450)

如果我注释代码writer.setMergeFields();一切工作正常,但字段不复制。

如果我使用文本5.3.6版本一切工作正常(使用PdfCopyField)。

为什么?

你的推理有两个错误。一个可以修复的错误,一个不是技术错误,而是无法修复的推理错误。

技术错误:在关闭document之前先关闭reader。那太早了。当document.close()被调用时,PdfCopyDocument需要访问读取器。您已经关闭了循环中的reader实例,然后java.lang.IllegalStateException通知您资源是已经关闭

你的假设的问题:数字签名是保证文档完整性的一种方法。当某人签署一份有5页纸的文件时,那个人想要确保他签署了这5页,不多也不少。现在要添加页面。那将永远破坏签名!

如果您想合并数字签名的PDF文档:- 禁止组装这些文档:如果你这样做,签名将消失或破坏。-你唯一的选择是将这些文件合并成一个便携的集合。在这种情况下,主PDF充当一个包或投资组合来存储单独的PDF。pdf本身保持完整(如果不完整,签名断开)。

相关内容

最新更新