Learn to use software to read Office class files for the first time.
Today Baidu a bit, found that Apache also has such open source projects, POI provides the following support:
Test the HSLF of the PPT read operation, and the PPT into a picture saved to the specified directory:
The package must be introduced before use:
Association package:
Test the main function as follows:
Set the input ppt file, output path, and generate picture size.
1 PackageEdu.office;2 3 Importjava.util.List;4 5 Public classTesttransfor {6 7 /**8 * Test ppt conversion9 * Ten * **/ One Public Static voidMain (string[] args) { APptreader reader =NewPptreader (); -Reader.setppt_file_path ("D:/1.ppt"); -Reader.setoutput_path ("d:/ppt/"); the //control picture size 1 time times twice times 3 times Times -Reader.setscale (1); - -list<string> images =reader.ppt2png (); + for(String image:images) { - System.out.println (image); + } A } at -}
The PPT conversion PNG class is as follows:
1 PackageEdu.office;2 3 ImportJava.awt.Color;4 Importjava.awt.Dimension;5 ImportJava.awt.Graphics2D;6 Importjava.awt.RenderingHints;7 ImportJava.awt.geom.Rectangle2D;8 ImportJava.awt.image.BufferedImage;9 ImportJava.io.File;Ten ImportJava.io.FileInputStream; One Importjava.io.FileNotFoundException; A ImportJava.io.FileOutputStream; - Importjava.io.IOException; - Importjava.util.ArrayList; the Importjava.util.List; - - ImportJavax.imageio.ImageIO; - + Importorg.apache.poi.hslf.model.Slide; - Importorg.apache.poi.hslf.usermodel.SlideShow; + A Public classpptreader{ at - //ppt input File path - PrivateString Ppt_file_path; - //ppt2png path to save PNG images - PrivateString Output_path; - //Scale is a picture of the original size several times in Private intScale = 1; - to PublicString Getppt_file_path () { + returnPpt_file_path; - } the Public voidSetppt_file_path (String ppt_file_path) { * This. Ppt_file_path =Ppt_file_path; $ }Panax Notoginseng Public intGetscale () { - returnScale ; the } + Public voidSetscale (intScale ) { A This. Scale =Scale ; the } + PublicString Getoutput_path () { - returnOutput_path; $ } $ Public voidSetoutput_path (String output_path) { - This. Output_path =Output_path; - } the - /**ppt to PNGWuyi * the * @returnnull failed list<string> picture Name list - * **/ Wu PublicList<string>ppt2png () { - //int slidenum = 2; Aboutlist<string> images =NewArraylist<string>(); $ Try { - FileInputStream is; -is =NewFileInputStream (ppt_file_path); -Slideshow ppt =Newslideshow (is); A is.close (); + theDimension pgsize =ppt.getpagesize (); - $ intwidth = (int) (pgsize.width*Scale ); the intHeight = (int) (pgsize.height*Scale ); the theslide[] Slide =ppt.getslides (); the for(inti = 0; i < slide.length; i++) { - in //if (slidenum! =-1 && slidenum! = (i+1)) continue; the //get PPT title per page theString title =slide[i].gettitle (); About //System.out.println ("Rendering Slide" +slide[i].getslidenumber () + (title = = null? "": ":" + title)); the theBufferedImage img =Newbufferedimage (width, height, bufferedimage.type_int_rgb); theGraphics2D graphics =img.creategraphics (); + Graphics.setrenderinghint (renderinghints.key_antialiasing, renderinghints.value_antialias_on); - Graphics.setrenderinghint (renderinghints.key_rendering, renderinghints.value_render_quality); the Graphics.setrenderinghint (renderinghints.key_interpolation, Renderinghints.value_interpolation_bicubic );Bayi Graphics.setrenderinghint (Renderinghints.key_fractionalmetrics, Renderinghints.value_fractionalmetrics _ON); the the Graphics.setpaint (color.white); -Graphics.fill (NewRectangle2d.float (0, 0, width, height)); - theGraphics.scale ((Double) Width/pgsize.width, (Double) height/pgsize.height); the the Slide[i].draw (graphics); the //name each picture -String fname = Output_path + "ppt-" + (i+1) + ". png"; theImages.add ("ppt-" + (i+1) + ". png"); theFile File =NewFile (output_path); the if(!file.exists ()) {94 file.mkdirs (); the } theFileOutputStream out =NewFileOutputStream (fname); theImageio.write (IMG, "PNG", out);98 out.close (); About } - returnimages;101}Catch(FileNotFoundException e) {102 e.printstacktrace ();103}Catch(IOException e) {104 e.printstacktrace (); the }106 return NULL;107 }108}
POI Open Source project-ppt2png conversion test