Java PDF Conversion jpg

Source: Internet
Author: User

/**
* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.