ExceptionConverter:java.io.IOException:尝试使用 iText 创建 PDF 时流关闭

ExceptionConverter: java.io.IOException: Stream Closed when trying to create a PDF using iText

我正在从事与 PDF 相关的项目,我想从现有的 PDF 创建一个 PDF。

所有事情都已完成,但是当我当时创建最终 PDF 时,此异常在 document.close(); 行的 savePDF 方法中抛出,如下所述。

这是我的代码

仅当我们添加新页面时应用程序崩溃

private void createAndAddPage(Bitmap bitmap) {

    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        Document document = new Document();
        File file = getPdfFile();
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        PdfWriter pdfWriter = PdfWriter.getInstance(document, fileOutputStream); //  Change pdf's name.
        document.open();
        Image image = Image.getInstance(byteArray); // Change image's name and extension.
        float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
                - document.rightMargin() - 0) / image.getWidth()) * 100; // 0 means you have no indentation. If you have any, change it.
        image.scalePercent(scaler);
        image.setAlignment(Image.ALIGN_CENTER);
        document.add(image);
        document.close();

        PdfReader pdfReader = new PdfReader(file.getPath());
        PdfImportedPage pdfImportedPage = pdfWriter.getImportedPage(pdfReader, 1);
        pageAdjustmentAdapter.AddPage(new PageAjdustAdapter.PdfPage(pdfImportedPage, bitmap));

    } catch (Exception e) {
        e.printStackTrace();
    }
}

保存最终PDF时的方法(此方法会崩溃)

    private void savePDF(PageAjdustAdapter pageAdjustment) {
       
                mPDFpages = pageAdjustment.getUpdatedList();
             
                try {

                    pdfWriter.setPageEvent(new RotateEvent());
                    document.open();

                    PdfContentByte pdfContentByte = pdfWriter.getDirectContent();

                    for (int i = 0; i < mPDFpages.size(); i++) {
                        pdfContentByte.addTemplate(mPDFpages.get(i).page, 0, 0);
                        document.newPage();
                    }

                } catch (Exception e) {
                    Log.d(TAG, "run: -> " + e.getMessage());
                    e.printStackTrace();
                } finally {
                    if (document.isOpen()) document.close();
                    actionListener.onEnd("Success");
                
                }

    }

logcat

ExceptionConverter: java.io.IOException: Stream Closed
    at java.io.FileOutputStream.write(FileOutputStream.java:391)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
    at java.io.BufferedOutputStream.write(BufferedOutputStream.java:121)
    at java.io.FilterOutputStream.write(FilterOutputStream.java:103)
    at com.itextpdf.text.pdf.OutputStreamCounter.write(OutputStreamCounter.java:104)
    at com.itextpdf.text.pdf.PRStream.toPdf(PRStream.java:244)
    at com.itextpdf.text.pdf.PdfIndirectObject.writeTo(PdfIndirectObject.java:157)
    at com.itextpdf.text.pdf.PdfWriter$PdfBody.write(PdfWriter.java:402)
    at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:380)
    at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:359)
    at com.itextpdf.text.pdf.PdfWriter.addToBody(PdfWriter.java:854)
    at com.itextpdf.text.pdf.PdfReaderInstance.writeAllVisited(PdfReaderInstance.java:160)
    at com.itextpdf.text.pdf.PdfReaderInstance.writeAllPages(PdfReaderInstance.java:176)
    at com.itextpdf.text.pdf.PdfWriter.addSharedObjectsToBody(PdfWriter.java:1368)
    at com.itextpdf.text.pdf.PdfWriter.close(PdfWriter.java:1251)
    at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:901)
    at com.itextpdf.text.Document.close(Document.java:415)
    at com.mobilix.docscanner.PageAdjustment.run(PageAdjustment.java:233)
    at java.lang.Thread.run(Thread.java:923)

createAndAddPage你把页面导入错了PdfWriter:

Document document = new Document();
File file = getPdfFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
PdfWriter pdfWriter = PdfWriter.getInstance(document, fileOutputStream); //  Change pdf's name.
document.open();
[...]
document.close();

PdfReader pdfReader = new PdfReader(file.getPath());
PdfImportedPage pdfImportedPage = pdfWriter.getImportedPage(pdfReader, 1);

在这里将新页面导入到用于创建相同新页面的PdfWriter。您必须将它导入到 PdfWriter 中,您最终要在其中使用 pdfImportedPage.