Java upload file-generate file homepage thumbnail generate PDF Grab picture

Source: Internet
Author: User

Method: 1. Convert files to PDF (using OpenOffice or Jacob)

2, grab the PDF home page image

The first step: using Jacob:

A, download Jacob attention to distinguish 32-bit, 64-bit, otherwise can not be used
Place the DLL file in the Java Bin directory
Jar Package Introduction Project

B. Convert pdf

Package Cn.bnsr.edu_yun.util;import Java.io.File;  Import Java.io.ioexception;import com.jacob.activeX.ActiveXComponent;  Import Com.jacob.com.ComThread;  Import Com.jacob.com.Dispatch;    Import com.jacob.com.Variant; public class Testutil {static final int wdformatpdf = 17;//PDF format public void wordtopdf (String sfilename,strin              G tofilename) {System.out.println ("Start Word ...");              Long start = System.currenttimemillis ();          Activexcomponent app = null;          Dispatch doc = null;                  try {app = new activexcomponent ("Word.Application");              App.setproperty ("Visible", new Variant (false));   Dispatch docs = app.getproperty ("Documents"). Todispatch ();               doc = Dispatch.call (docs, "Open", SourceFile). Todispatch (); doc = Dispatch.invoke (docs, "Open", Dispatch.method,new object[] {sfilename, new Variant ( False), new Variant (True)}, NEW int[1]). Todispatch ();              SYSTEM.OUT.PRINTLN ("Open Document ..." + sfilename);                  SYSTEM.OUT.PRINTLN ("Convert Document to PDF ..." + tofilename);                  File ToFile = new file (tofilename);                  if (tofile.exists ()) {tofile.delete ();                                }//Dispatch.call (Doc, "SaveAs", DestFile, 17); Dispatch.invoke (Doc, "SaveAs", Dispatch.method, new object[] {tofilename, new Variant (17)                }, new int[1]);                  Long end = System.currenttimemillis (); SYSTEM.OUT.PRINTLN ("Conversion Complete:                        Spents: "+ (End-start) +" Ms. ");              } catch (Exception e) {e.printstacktrace ();              System.out.println ("========error: Document conversion failed:" + e.getmessage ());              } finally {Dispatch.call (doc, "Close", false);              SYSTEM.OUT.PRINTLN ("close document"); if (app! = null) App.invoke ("QuIt ", new variant[] {});         }//If there is no such sentence, the Winword.exe process will not close comthread.release ();          } public static void Main (string[] args) {testutil d = new Testutil ();      D.wordtopdf ("D:\\work\\1111.docx", "d:\\work\\1111.pdf"); }    }

The second step: Crawl home page diagram:

  

Package Cn.bnsr.edu_yun.util;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.lang.reflect.method;import Java.nio.bytebuffer;import Java.nio.channels.filechannel;import Java.security.accesscontroller;import Java.security.privilegedaction;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 Pdfimageutil {//convert PDF file to JPG format public static void Pdftojpg (String inputfile) throws IOException {//LOA D a pdf from a byte bufferfile file = new file (inputfile); Randomaccessfile RAF = new Randomaccessfile (file, "R"); FileChannel channel = Raf.getchannel ();//This line of code establishes a map map, if you want to delete file so contact mapping bytebuffer buf = Channel.map ( FileChannel.MapMode.READ_ONLY, 0,channel.size ()); Pdffile pdffile = new Pdffile (BUF); int Totalpage =pdffile.getnumpagES (); for (int i = 1; I <= totalpage; i++) {if (i = = 1) {//Draw the first page to an image//in the form of a picture page pdfpage page = Pdffile.getpage (i); Rectangle rect = new Rectangle (0, 0, (int) Page.getbbox (). GetWidth (), (int) Page.getbbox (). GetHeight ());//Generate The IM age//Generate image Image img = page.getimage (rect.width, Rect.height,//Width &//heightrect,//Clip rectnull,///null for TH e imageobservertrue,//fill background with whitetrue//block until drawing are done); BufferedImage tag = new BufferedImage (rect.width, Rect.height,bufferedimage.type_int_rgb); Tag.getgraphics (). DrawImage (Img.getscaledinstance (Rect.width, Rect.height, Image.scale_smooth), 0, 0, rect.width, rect.height,null); FileOutputStream out = new FileOutputStream ("d:\\work\\s111" + ". jpg"); Output to file stream JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out); Encoder.encode (tag); JPEG encode//OFF output stream out.close (); break;}} Buf.clear (); Channel.close (); Raf.close (); Unmap (BUF); File.delete ();} Release map map public static <T> void UNMAP (Final Object buffer) {accesscontroller.doprivileged (new privilegedaction<t> () {@Overridepublic T run () {try { Method Getcleanermethod = Buffer.getclass (). GetMethod ("cleaner", new Class[0]); Getcleanermethod.setaccessible (True) ; Sun.misc.Cleaner Cleaner = (sun.misc.Cleaner) getcleanermethod.invoke (buffer, new object[0]); Cleaner.clean ();} catch (Exception e) {e.printstacktrace ();} return null;} });}}

  

Step three, fix it, join your business logic, and ask questions.

  

Java upload file-generate file homepage thumbnail generate PDF Grab picture

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.