Java implementation pdf cut into pictures by page

Source: Internet
Author: User

 Packagecom.ces.component.pictrueCut.entity;ImportJava.awt.Image; ImportJava.awt.Rectangle; ImportJava.awt.image.BufferedImage; ImportJava.io.File; Importjava.io.FileNotFoundException; ImportJava.io.FileOutputStream; Importjava.io.IOException; ImportJava.io.RandomAccessFile; ImportJava.lang.reflect.Method; ImportJava.nio.MappedByteBuffer; ImportJava.nio.channels.FileChannel; ImportJava.security.AccessController; Importjava.security.PrivilegedAction; ImportCom.sun.image.codec.jpeg.JPEGCodec; ImportCom.sun.image.codec.jpeg.JPEGEncodeParam; ImportCom.sun.image.codec.jpeg.JPEGImageEncoder; ImportCom.sun.pdfview.PDFFile; ImportCom.sun.pdfview.PDFPage; Public classPdftoimage { Public Static intchangepdftoimg (String instructiopath,string picturepath) {intCountpage =0; Try {              //Instructiopath = "D:/instructio/2015-05-16/android 4 Programming Primer Classic. pdf"//PicturePath = "d:/instructio/picture/2015-05-16/"; File File=NewFile (Instructiopath); Randomaccessfile RAF=NewRandomaccessfile (file, "R"); FileChannel Channel=Raf.getchannel (); Mappedbytebuffer buf=Channel.map (FileChannel.MapMode.READ_ONLY,0, Channel.size ()); Pdffile Pdffile=NewPdffile (BUF); //Create a picture folderFile Dirfile =NewFile (PicturePath); if(!dirfile.exists ())                  {Dirfile.mkdirs (); }              //Get Picture PagesCountpage =pdffile.getnumpages ();  for(inti = 1; I <= pdffile.getnumpages (); i++) {pdfpage page=pdffile.getpage (i); Rectangle rect=NewRectangle (0, 0, ((int) Page.getbbox (). GetWidth ()), ((int) Page.getbbox (). GetHeight ()); intn = 2; /**image sharpness (n>0 and n<7) "pdf magnification parameter"*/Image img= Page.getimage (Rect.width * N, Rect.height *N, Rect,/**enlarge the PDF to n times to create the picture. */                          NULL,/**null for the ImageObserver*/                          true,/**fill background with white*/                          true /**block until drawing is done*/                  ); BufferedImage Tag=NewBufferedImage (Rect.width *N, Rect.height*N, Bufferedimage.type_int_rgb); Tag.getgraphics (). DrawImage (IMG,0, 0, Rect.width *N, Rect.heightNNULL); /*** File Imgfile = new file ("d:\\work\\mybook\\filesnew\\img\\" + * i + ". jpg"); if (i Mgfile.exists ()) {* if (Imgfile.createnewfile ()) {System.out.println ("Create Picture:" + * "D:\\wor K\\mybook\\filesnew\\img\\ "+ i +". jpg "); } else {* SYSTEM.OUT.PRINTLN ("Create Picture Failed! "); } }                  */FileOutputStream out=NewFileOutputStream (picturepath+ "/" +I+ ". png"); /**output to file stream*/JPEGImageEncoder Encoder=Jpegcodec.createjpegencoder (out); JPEGEncodeParam param2=Encoder.getdefaultjpegencodeparam (tag); Param2.setquality (1f,true); /**1f~0.01f is to improve the resulting image quality*/Encoder.setjpegencodeparam (param2);                  Encoder.encode (tag); /**JPEG encoding*/Out.close ();              } channel.close ();              Raf.close ();              Unmap (BUF); /**If you want to delete the PDF after you turn the picture, you have to close the stream and empty the buffer.*/          } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }          returnCountpage; } @SuppressWarnings ("Unchecked")       Public Static voidUnmapFinalObject Buffer) {accesscontroller.doprivileged (Newprivilegedaction () { PublicObject Run () {Try{Method Getcleanermethod=Buffer.getclass (). GetMethod ("Cleaner",NewClass[0]); Getcleanermethod.setaccessible (true); Sun.misc.Cleaner Cleaner=(Sun.misc.Cleaner) Getcleanermethod. Invoke (buffer,NewObject[0]);                  Cleaner.clean (); } Catch(Exception e) {e.printstacktrace (); }                  return NULL;      }          }); }  }  

Java implementation pdf cut into pictures by page

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.