Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从itext7中的现有PDF文档打开PDF文档?_Java_Itext_Itext7 - Fatal编程技术网

Java 如何从itext7中的现有PDF文档打开PDF文档?

Java 如何从itext7中的现有PDF文档打开PDF文档?,java,itext,itext7,Java,Itext,Itext7,为了检查上传的PDF文件的基本PDF/A一致性,我需要将它们作为PdfADocuments读入。 但是从版本7.1.6开始,这不再有效,而是抛出一个PdfException(PdfException.PdfReaderHasBeenAlreadyUtilized) 类分析器服务 ... 公共布尔分析(PdfDocument pdf){ PdfADocument pdfa=新的PdfADocument( pdf.getReader(),pdf.getWriter()您的方法使用相同的PdfRea

为了检查上传的PDF文件的基本PDF/A一致性,我需要将它们作为PdfADocuments读入。 但是从版本7.1.6开始,这不再有效,而是抛出一个PdfException(PdfException.PdfReaderHasBeenAlreadyUtilized)


类分析器服务
...
公共布尔分析(PdfDocument pdf){
PdfADocument pdfa=新的PdfADocument(

pdf.getReader(),pdf.getWriter()您的方法使用相同的
PdfReader
和(甚至更糟)对于
PdfDocument
PdfADocument
实例,相同的
PdfWriter
。由于两者都可以操作
PdfReader
并写入
PdfWriter
,这种情况很可能会导致写入程序中出现垃圾,因此您不应这样做

简单地把读者和作者都看作是一个正在进行中的工作,不能把它当作一个完整的文档文件,例如中间检查的摘录。

当您想检查上传的PDF文件时,为什么不直接将
buffer.toByteArray()
中的
字节[]
转发到
analyze
方法,从中创建一个单独的阅读器(如果需要,还可以是文档)?这确实会检查上传的文件


此外,如果您的输入文档可能是PDF/A conform,并且在这种情况下会被特别处理,那么您是否也应该将其作为
PdfADocument
进行操作呢?即,您是否应该首先检查分析仪的一致性,并且在肯定的情况下,在控制器类中也使用
PdfADocument
PdfWriter及其输出从未被使用过,只是因为PDFadDocument坚持要有一个;-)我改变了整个应用程序,将PdfDocuments视为临时的,只在整个工作流中传递字节[]中的数据。不过,这相当繁琐,并且重新创建PdfDocument(新的PDF阅读器(新的ByteArrayInputStream(byteArray)))每次都会导致明显的性能影响。是否有其他对象类型更适合通过工作流传递?“PdfWriter及其输出从不使用”-那你为什么要把它放在那里呢?有一个
PdfDocument
构造函数,只有一个读卡器,没有写卡器…可能在你的情况下使用该构造函数就足够了…正确。唯一的原因是-我将PdfDocument作为一种持久对象通过工作流传递(这似乎是个坏主意;-)-工作流中调用的PdfDocument构造函数需要它,我认为让PdfDocument和PdfDocument都从同一个PdfReader读取是不好的,但是使用不同的PdfWriter实例。但是根据您的帖子,整个概念似乎与这些对象的设计目的不一致,所以我将我还在想,是否有一种方法可以在整个工作流中使用单个对象,更符合iText的一般设计原则。
class Controller
...
// get uploaded data into PdfDocument, which is passed
// on to different services.

InputStream filecontent = fileupload.getInputStream();
int read = 0;
byte[] bytes = new byte[1024];
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
while ((read = filecontent.read(bytes,0,bytes.length)) != -1) {
    filesize += read;
    buffer.write(bytes, 0, read);
}

ByteArrayInputStream input = new ByteArrayInputStream(buffer.toByteArray());
PdfReader reader = new PdfReader(input); 
PdfWriter writer = new PdfWriter(new ByteArrayOutputStream());

PdfDocument pdf = new PdfDocument(reader, writer);

AnalyzerService analyzer = new AnalyzerService();
if(analyzer.analyze(pdf)) {
    otherService.doSomethingWith(pdf);
}
...
class AnalyzerService
...
public boolean analyze(PdfDocument pdf) {
    PdfADocument pdfa = new PdfADocument(
        pdf.getReader(), pdf.getWriter() <-- PdfException here
    );
...
}