/**
* Convert all pages of PDF to JPG, and return all images of the path to the collection
* @param Inputfilepath
* Picture path, specific to file name
* @param Outputfilepath
* Output directory, no file name required
* @return
* @throws IOException
*/
public static list<string> pdf2jpg (String Inputfilepath,
String Outputfilepath) throws IOException {
list<string> outputfilepathlist = new arraylist<string> ();
Load a PDF from a byte buffer
File File = new file (Inputfilepath);
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);
String outputFilePath2 = Outputfilepath + system.currenttimemillis () + ". jpg";
FileOutputStream out = new FileOutputStream (outputFilePath2); Output to file stream
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (tag); JPEG encoding
Out.close ();
Outputfilepathlist.add (OUTPUTFILEPATH2);
}
return outputfilepathlist;
}
Java PDF Conversion jpg