Beyond that we also did not want to write a single file to the file system so that is where the trick came into play. So based on research, trial and error I give you a converter from PDF to an ArrayList of byte arrays.
private static ArrayList convertImage(byte[] fileBytes) throws Exception {ArrayList images = new ArrayList();ByteBuffer buf = ByteBuffer.wrap(fileBytes);PDFFile pdffile = new PDFFile(buf);// draw the first page to an imageint numberOfPages = pdffile.getNumPages();System.out.println("NUMBER OF PAGES:" + numberOfPages);for (int i = 0; i < numberOfPages; i++) {PDFPage page = pdffile.getPage(i);// get the width and height for the doc at the default zoomint width = (int) page.getBBox().getWidth();int height = (int) page.getBBox().getHeight();Rectangle rect = new Rectangle(0, 0, width, height);int rotation = page.getRotation();Rectangle rect1 = rect;if (rotation == 90 || rotation == 270)rect1 = new Rectangle(0, 0, rect.height, rect.width);// generate the imageBufferedImage img = (BufferedImage) page.getImage(rect.width,rect.height, // width & heightrect1, // clip rectnull, // null for the ImageObservertrue, // fill background with whitetrue // block until drawing is done);ByteArrayOutputStream output = new ByteArrayOutputStream();ImageIO.write(img, "png", output);images.add(output.toByteArray());}return images;}
No comments:
Post a Comment