Home > Back-end >  ExceptionConverter: java.io.IOException: Stream Closed when trying to create a PDF using iText
ExceptionConverter: java.io.IOException: Stream Closed when trying to create a PDF using iText

Time:02-17

I'm working on PDF related project and I want to create a PDF from the existing PDF.

all things are done but when I created a final PDF at that time this exception was thrown at the line of document.close(); at the method of savePDF which describe below.

  • Create new PDF from existing PDF adding PdfImportedPage.

here is my code

The app crashes only when we add a new page

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();
    }
}

method when saves a final PDF(crash happens in this method)

    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$8.run(PageAdjustment.java:233)
    at java.lang.Thread.run(Thread.java:923)

CodePudding user response:

In createAndAddPage you import the page into the wrong 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);

Here you import the new page into the PdfWriter used for creating that same new page. You instead have to import it into the PdfWriter in which you eventually want to use the pdfImportedPage.

  • Related