I need to generate large size PDF by adding multiple images to it. In itext 5 if I add a image to the document it is immediately flushed to disk. But in itext 7 it stays in memory and is written to disk only after closing the document.
Itext 7 docs says about using large tables concept https://kb.itextpdf.com/home/it7kb/examples/large-tables , which I tried but it also doesnt flush the images to disk.
Anyone know why ? Thanks in advance for help.
Document document = new Document(PageSize.A4, 36, 36, 36, 72);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
document.open();
Image image1 = null;
for (int i = 0; i < 1000; i++) {
File f = new File("big_image.png");
InputStream is = new FileInputStream(f);
image1 = Image.getInstance(IOUtils.toByteArray(is));
document.add(image1);
}
document.close();
writer.close();
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("test.pdf"));
Document doc = new Document(pdfDoc);
for (int i = 0; i < 1000; i++) {
File f = new File("big_image.png");
doc.add(new Image(ImageDataFactory.create(f.getPath())));
}
doc.close();
It seems this is not working as intended in iText 7. We'll need to look into it further (disclosure: I'm an iText Software employee).
In the meantime, as a simple workaround, you can flush the images explicitly:
for (int i = 0; i < 1000; i++) {
File f = new File("big_image.png");
Image image = new Image(ImageDataFactory.create(f.getPath()));
image.getXObject().makeIndirect(pdfDoc).flush();
doc.add(image);
}