Package PDF;
Import Java.awt.Image;
Import Java.awt.Rectangle;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.RandomAccessFile;
Import Java.nio.ByteBuffer;
Import Java.nio.channels.FileChannel;
Import Com.sun.image.codec.jpeg.JPEGCodec;
Import Com.sun.image.codec.jpeg.JPEGImageEncoder;
Import Com.sun.pdfview.PDFFile;
Import Com.sun.pdfview.PDFPage;
public class Pdftojpgtest {
public static void Setup (file file) throws IOException {
Load a PDF from a byte buffer
/*file file = new file (
"C://xxxxx.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);
System.out.println ("pages:" + pdffile.getnumpages ());
for (int i = 1; I <= pdffile.getnumpages (); i++) {
Draw the first page to an image
Pdfpage page = Pdffile.getpage (i);
Get the width and height for the doc at the default zoom
Rectangle rect = new Rectangle (0, 0, (int) Page.getbbox ()
. GetWidth (), (int) Page.getbbox (). GetHeight ());
Generate the image
Image img = page.getimage (rect.width, Rect.height,//Width &
Height
Rect,//clip rect
NULL,//null for the ImageObserver
True,//fill background with white
True//block until drawing is done
);
BufferedImage tag = new BufferedImage (Rect.width, Rect.height,
BUFFEREDIMAGE.TYPE_INT_RGB);
Tag.getgraphics (). DrawImage (img, 0, 0, rect.width, Rect.height,
NULL);
FileOutputStream out = new FileOutputStream (
"c:/users/bonnie/desktop/content Release/img/" +file.getname () + i + ". jpg"); Output to file stream
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (tag); JPEG encoding
Out.close ();
}
Show the image in a frame
JFrame frame = new JFrame ("PDF Test");
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
Frame.add (New JLabel (New ImageIcon (IMG)));
Frame.pack ();
Frame.setvisible (TRUE);
}
public static void Showallfiles (File dir) throws exception{
int pdfcount=0;
file[] fs = Dir.listfiles ();
for (int i=0; i<fs.length; i++) {
if (Fs[i].isdirectory ()) {
try{
Showallfiles (Fs[i]);
}catch (Exception e) {}
}else{
String ffhouzhui=fs[i].getname (). substring (Fs[i].getname (). LastIndexOf (".") +1,fs[i].getname (). Length ());//suffix name
if (Ffhouzhui.equals ("PDF")) {//pdf file
pdfcount++;
System.out.println (pdfcount+ ">>>>>>>>>" +fs[i].getname ());
Setup (Fs[i]);
}
}
}
}
public static void Main (final string[] args) {
Recursively displays all folders and files in the C drive
File root = new file ("c:/users/bonnie/desktop/content publishing");
try {
Showallfiles (root);
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Note: Com.sun.pdfview,jar package needs to be downloaded
Java Loop folder reads PDF and converts to JPG