Java Itext PdfSmartCopy获取空指针异常

Java Itext PdfSmartCopy获取空指针异常,java,pdf,itext,Java,Pdf,Itext,我现在正在使用Itext PdfSmartCopy。我正在使用XMLworker向文档对象添加一些业务内容。然后我声明了一个阅读器(用于将pdf文件连接到此文档对象)。然后我使用与参数相同的文档对象和输出文件流调用PdfSmartCopy。然后我使用常规步骤将页面复制到文档中 addHTML(document, htmlStringToBeAdded); document.newPage(); com.itextpdf.text.pdf.PdfCopy copy = new com.itextp

我现在正在使用Itext PdfSmartCopy。我正在使用XMLworker向文档对象添加一些业务内容。然后我声明了一个阅读器(用于将pdf文件连接到此文档对象)。然后我使用与参数相同的文档对象和输出文件流调用PdfSmartCopy。然后我使用常规步骤将页面复制到文档中

addHTML(document, htmlStringToBeAdded);
document.newPage();
com.itextpdf.text.pdf.PdfCopy copy = new com.itextpdf.text.pdf.PdfSmartCopy(document, new FileOutputStream("c:\\pdf_issue\\bad_itext3.pdf"));
com.itextpdf.text.pdf.PdfReader reader=new com.itextpdf.text.pdf.PdfReader("c:\\pdf_issue\\bad1.pdf");
 // loop over the pages in that document
 n = reader.getNumberOfPages();
  for (int page = 0; page < n; ) {
                copy.addPage(copy.getImportedPage(reader, ++page));
            }
copy.freeReader(reader);
    reader.close();

但是如果我使用一个新的文档对象ie而不添加业务内容,这篇文章会很好地工作。

我们的已结问题跟踪程序中也有类似的问题。在该票据中,似乎需要在创建
PdfCopy
实例之后立即打开
文档

在您的例子中,我们看到了一个类似的问题:您使用
文档
对象从头开始创建PDF,然后使用相同的
文档
创建现有PDF的副本。这是行不通的:您需要先关闭从头创建的文档,然后为复制过程创建一个新的
文档

// first create the document
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.open();
addHTML(document, htmlStringToBeAdded);
document.close();
// Now you can use the document you've just created
PdfReader reader = new PdfReader(baos.toArray());
PdfReader existing = new PdfReader("c:\\pdf_issue\\bad1.pdf");
document = new Document();
PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream("c:\\pdf_issue\\bad_itext3.pdf"));
document.open();
copy.addDocument(reader);
copy.addDocument(existing);
document.close();
reader.close();
existing.close();

在我们的封闭问题跟踪程序中,我们也遇到了类似的问题。在该票据中,似乎需要在创建
PdfCopy
实例之后立即打开
文档

在您的例子中,我们看到了一个类似的问题:您使用
文档
对象从头开始创建PDF,然后使用相同的
文档
创建现有PDF的副本。这是行不通的:您需要先关闭从头创建的文档,然后为复制过程创建一个新的
文档

// first create the document
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.open();
addHTML(document, htmlStringToBeAdded);
document.close();
// Now you can use the document you've just created
PdfReader reader = new PdfReader(baos.toArray());
PdfReader existing = new PdfReader("c:\\pdf_issue\\bad1.pdf");
document = new Document();
PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream("c:\\pdf_issue\\bad_itext3.pdf"));
document.open();
copy.addDocument(reader);
copy.addDocument(existing);
document.close();
reader.close();
existing.close();

查看pdf文件会有帮助,否则任何文档都会失败?嗨@Paulosares,如果你想要一个重现问题的示例,请看一看。嗨@Bruno,我无法访问上面的链接。你能帮我解决这个问题吗?上面的链接只针对客户。您是客户吗?Document.open()方法应在创建PdfCopy/PdfSmartCopy实例后立即调用。否则,文档未正确初始化。查看pdf会有帮助,否则任何文档都会失败?Hi@Paulosaares,如果您想要重现此问题的示例,请查看。Hi@Bruno,我无法访问上述链接。你能帮我解决这个问题吗?上面的链接只针对客户。您是客户吗?Document.open()方法应在创建PdfCopy/PdfSmartCopy实例后立即调用。否则文档未正确初始化。谢谢Bruno。这很有道理,给了我一个我正在寻找的解决方案。让我试试。谢谢布鲁诺。这很有道理,给了我一个我正在寻找的解决方案。让我试试。