Export PDF pages to a series of images in Java -
i need export pages of arbitrary pdf document series of individual images in jpeg/png/etc format. need in in java.
although know itext, pdfbox , various other java pdf libraries, hoping pointer working example, or how-to.
thanks.
here 1 way it, combining code fragments around web.
how draw pdf image?
https://pdf-renderer.dev.java.net/examples.html
creating buffered image image
original: http://www.exampledepot.com/egs/java.awt.image/image2buf.html
updated: how convert buffered image image , vice-versa?
saving generated graphic png or jpeg file
original: http://www.exampledepot.com/egs/javax.imageio/graphic2file.html
updated: http://docs.oracle.com/javase/tutorial/2d/images/saveimage.html
combined works turn pages images:
import com.sun.pdfview.pdffile; import com.sun.pdfview.pdfpage; import java.awt.graphics; import java.awt.graphicsconfiguration; import java.awt.graphicsdevice; import java.awt.graphicsenvironment; import java.awt.headlessexception; import java.awt.image; import java.awt.rectangle; import java.awt.transparency; import java.io.*; import java.nio.bytebuffer; import java.nio.channels.filechannel; import javax.swing.*; import javax.imageio.*; import java.awt.image.*; public class imagemain { public static void setup() throws ioexception { // load pdf byte buffer file file = new file("test.pdf"); randomaccessfile raf = new randomaccessfile(file, "r"); filechannel channel = raf.getchannel(); bytebuffer buf = channel.map(filechannel.mapmode.read_only, 0, channel.size()); pdffile pdffile = new pdffile(buf); int numpgs = pdffile.getnumpages(); (int = 0; < numpgs; i++) { // draw first page image pdfpage page = pdffile.getpage(i); // width , height doc @ default zoom rectangle rect = new rectangle(0, 0, (int) page.getbbox().getwidth(), (int) page.getbbox().getheight()); // generate image image img = page.getimage(rect.width, rect.height, // width & height rect, // clip rect null, // null imageobserver true, // fill background white true // block until drawing done ); // save file bufferedimage bimg = tobufferedimage(img); file yourimagefile = new file("page_" + + ".png"); imageio.write(bimg, "png", yourimagefile); } } // method returns buffered image contents of image public static bufferedimage tobufferedimage(image image) { if (image instanceof bufferedimage) { return (bufferedimage) image; } // code ensures pixels in image loaded image = new imageicon(image).getimage(); // determine if image has transparent pixels; method's // implementation, see e661 determining if image has transparent // pixels boolean hasalpha = hasalpha(image); // create buffered image format that's compatible // screen bufferedimage bimage = null; graphicsenvironment ge = graphicsenvironment.getlocalgraphicsenvironment(); try { // determine type of transparency of new buffered image int transparency = transparency.opaque; if (hasalpha) { transparency = transparency.bitmask; } // create buffered image graphicsdevice gs = ge.getdefaultscreendevice(); graphicsconfiguration gc = gs.getdefaultconfiguration(); bimage = gc.createcompatibleimage(image.getwidth(null), image.getheight(null), transparency); } catch (headlessexception e) { // system not have screen } if (bimage == null) { // create buffered image using default color model int type = bufferedimage.type_int_rgb; if (hasalpha) { type = bufferedimage.type_int_argb; } bimage = new bufferedimage(image.getwidth(null), image.getheight(null), type); } // copy image buffered image graphics g = bimage.creategraphics(); // paint image onto buffered image g.drawimage(image, 0, 0, null); g.dispose(); return bimage; } public static boolean hasalpha(image image) { // if buffered image, color model readily available if (image instanceof bufferedimage) { bufferedimage bimage = (bufferedimage) image; return bimage.getcolormodel().hasalpha(); } // use pixel grabber retrieve image's color model; // grabbing single pixel sufficient pixelgrabber pg = new pixelgrabber(image, 0, 0, 1, 1, false); try { pg.grabpixels(); } catch (interruptedexception e) { } // image's color model colormodel cm = pg.getcolormodel(); return cm.hasalpha(); } public static void main(final string[] args) { swingutilities.invokelater(new runnable() { public void run() { try { imagemain.setup(); } catch (ioexception ex) { ex.printstacktrace(); } } }); } }
Comments
Post a Comment