I’m trying out Aspose.PDF for Java as an alternative for PDFBox, as it could not handle some documents. I need to render single pages as images. I need them as a BufferedImage for some other API to process images I generate from PDF files. Basically I need to input a PDF file and get a list of scaled buffered images for further processing.
I can do something like this:
var out = new ByteArrayOutputStream(buff); final PngDevice device = new PngDevice(new Resolution(dpi)); device.process(page, out);
Then I can use ImageIO.read to get a BufferedImage.
But can’t I just render a page directly as a BufferedImage or to a Graphics2D from an image I create?
PDFBox can do it like this:
PDFRenderer renderer = new PDFRenderer(doc); renderer.renderPageToGraphics(index, graphics2d);
This also makes scaling easy as I can use the scale method of Graphics2D.
Am I missing something? Maybe there’s a “Device” for that, but I only see those for PNG, JPG etc.
Another problem with using PngDevice is that when I then load it as an com.aspose.pdf.Image I can’t get the dimensions. getBufferedImage returns null. Why is that? If I could get the dimensions I could use it to do the opposite. Sometimes we need to create a PDF file from images.