Open baszero opened 4 years ago
With trial and error I was able to use ImageElement
in combination with RenderContext
. From the renderContext
you can also derive the current page by using the method renderContext.getCurrentPage()
.
PDDocument pdDocument = new PDDocument();
RenderContext renderContext = new RenderContext(new Document(new PageFormat(), pdDocument));
renderContext.renderElement(renderContext, new ImageElement(inputStream), new VerticalLayoutHint());
However, with this approach I am getting an OutOfMemory in some cases. Here is the relevant stacktrace:
java.lang.OutOfMemoryError: Java heap space
at org.apache.pdfbox.io.IOUtils.copy(IOUtils.java:65)
at org.apache.pdfbox.filter.FlateFilter.encode(FlateFilter.java:131)
at org.apache.pdfbox.filter.Filter.encode(Filter.java:101)
at org.apache.pdfbox.cos.COSOutputStream.close(COSOutputStream.java:139)
at java.base/java.io.FilterOutputStream.close(FilterOutputStream.java:188)
at org.apache.pdfbox.cos.COSStream$1.close(COSStream.java:237)
at org.apache.pdfbox.pdmodel.PDPageContentStream.close(PDPageContentStream.java:2548)
at rst.pdfbox.layout.elements.render.RenderContext.closePage(RenderContext.java:423)
at rst.pdfbox.layout.elements.render.RenderContext.newPage(RenderContext.java:371)
at rst.pdfbox.layout.elements.render.VerticalLayout.turnPage(VerticalLayout.java:62)
at rst.pdfbox.layout.elements.render.VerticalLayout.layoutAndDrawReleative(VerticalLayout.java:213)
at rst.pdfbox.layout.elements.render.VerticalLayout.renderReleative(VerticalLayout.java:148)
at rst.pdfbox.layout.elements.render.VerticalLayout.render(VerticalLayout.java:96)
at rst.pdfbox.layout.elements.render.VerticalLayout.render(VerticalLayout.java:79)
at rst.pdfbox.layout.elements.render.RenderContext.render(RenderContext.java:309)
I didn't find anything related to inserting images in the wiki.
As of PDFBox, inserting an image would work like this:
however I don't find any way how to get the current page from pdfbox-layout.
Any advise?