I am using iText 7.1.6 to generate the PDF. When I try to run , I get this error document was closed . It is impossible to execute the action.
I checked solutions on Stack Overflow for this, but they are saying it is related to fonts. I modified the fonts by creating instance every time I use it, but even then I get same issue.
How can I resolve this issue?
Please find the below code and exception:
Document doc = null;
PdfPage sourcePage = null;
try (InputStream resource = new FileInputStream(new File(Paths.get("Output").toAbsolutePath()+"/source.pdf"));
PdfReader pdfReader = new PdfReader(resource);
PdfDocument pdfDocument = new PdfDocument(pdfReader)) {
PdfDocumentContentParser contentParser = new PdfDocumentContentParser(pdfDocument);
MarginFinder strategy = contentParser.processContent(1, new MarginFinder());
sourcePage = pdfDocument.getPage(1);
sourcePage.setCropBox(strategy.getBoundingBox());
sourcePage.setMediaBox(strategy.getBoundingBox());
}
@SuppressWarnings("resource")
PdfWriter writer = new PdfWriter(new FileOutputStream(new File(Paths.get("Output").toAbsolutePath()+"/final.pdf"))).setSmartMode(true);
PdfDocument pdfDoc = new PdfDocument(writer);
pdfDoc.setDefaultPageSize(PageSize.A3.rotate());
String fonts[] = {Paths.get("fonts").toAbsolutePath() + "/TREBUC.TTF", Paths.get("fonts").toAbsolutePath() + "/TREBUCBD.TTF", Paths.get("fonts").toAbsolutePath() + "/TREBUCBI.TTF",Paths.get("fonts").toAbsolutePath() + "/TREBUCIT.TTF"};
FontProvider fontProvider = new FontProvider();
Map<String, PdfFont> pdfFontMap = new HashMap<String, PdfFont>();
for (String font : fonts) {
FontProgram fontProgram = FontProgramFactory.createFont(font);
if (font.endsWith("TREBUC.TTF")) {
pdfFontMap.put("NORMAL", PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, true));
} else if (font.endsWith("TREBUCBD.TTF")) {
pdfFontMap.put("BOLD", PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, true));
} else if (font.endsWith("TREBUCBI.TTF")) {
pdfFontMap.put("BOLD_ITALIC", PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, true));
} else if (font.endsWith("TREBUCIT.TTF")) {
pdfFontMap.put("ITALIC", PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, true));
}
fontProvider.addFont(fontProgram);
}
TestVisualSummaryNew testVisualSummaryNew = new TestVisualSummaryNew();
NormalPageHeader headerHandler = testVisualSummaryNew.new NormalPageHeader(Paths.get("images").toAbsolutePath() + "\\logo.png", pdfFontMap);
pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, headerHandler);
PageEndEvent pageEndEvent = testVisualSummaryNew.new PageEndEvent(Paths.get("images").toAbsolutePath() + "\\FooterLineExternal.png" ,pdfFontMap);
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, pageEndEvent);
doc = new Document(pdfDoc);
doc.setTopMargin(55);
PdfFormXObject xobject = sourcePage.copyAsFormXObject(pdfDoc);
Rectangle xobjectBoundaryBox = xobject.getBBox().toRectangle();
xobject.getPdfObject().put(PdfName.Matrix, new PdfArray(new float[] {1, 0, 0, 1, -xobjectBoundaryBox.getLeft(), -xobjectBoundaryBox.getBottom()}));
Image image = new Image(xobject);
image.setAutoScale(true);
doc.add(image);
System.out.println("Converted to PDF Succesfully >>> source.pdf");
Exception
com.itextpdf.kernel.PdfException: Document was closed. It is
impossible to execute action. at
com.itextpdf.kernel.pdf.PdfDocument.checkClosingStatus(PdfDocument.java:2041)
at
com.itextpdf.kernel.pdf.PdfDocument.getWriter(PdfDocument.java:706)
at
com.itextpdf.kernel.pdf.PdfIndirectReference.getWriter(PdfIndirectReference.java:270)
at com.itextpdf.kernel.pdf.PdfObject.copyTo(PdfObject.java:318) at
com.itextpdf.kernel.pdf.PdfPage.copyAsFormXObject(PdfPage.java:439)
sourcePage.copyAsFormXObject
), after this original document is closed. After document closing you cannot perform any operations with it. – FinchpdfDocument.close()
line, you also have to move thepdfDocument = new PdfDocument(pdfReader)
out of thetry-with-resources
resources as those resources also are closed at the end of thetry
block. – Siracusa