1

I have the following method that shrink the page to 50% :

private void shrinkDocuments(List<PdfDocument> documentsToBeMerged){
    for (PdfDocument pdfDoc : documentsToBeMerged) {

        for (int p = 1; p <= pdfDoc.getNumberOfPages(); p++) {
            PdfPage page = pdfDoc.getPage(p);
            Rectangle media = page.getCropBox();
            if (media == null) {
                media = page.getMediaBox();
            }

            // Shrink the page to 50%
            Rectangle crop = new Rectangle(0, 0, media.getWidth() / 2, media.getHeight() / 2);
            page.setMediaBox(crop);
            page.setCropBox(crop);


        }

        pdfDoc.close();
    }

}

Debugging I see that the program throws a com.itextpdf.kernel.PdfException: Document was closed. It is impossible to execute action .

How can I solve this ? How can I open again this documents?

Nexussim Lements
  • 535
  • 1
  • 15
  • 47
  • 1
    I'm afraid you have to make sure that the calling code does not close the PDFs before calling. Furthermore, you should make sure that you correctly handle the case of a document more than once in your `documentsToBeMerged` list because in that case your method itself is the culprit closing the document. – mkl Aug 19 '20 at 12:19

0 Answers0