文件在线预览ppt转换图片(二)
程序员文章站
2022-06-12 09:55:25
...
package com.lx.file.convert.master.ppt; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.List; import java.util.Locale; import javax.imageio.ImageIO; import org.apache.poi.sl.draw.DrawFactory; import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XSLFShape; import org.apache.poi.xslf.usermodel.XSLFSlide; import org.apache.poi.xslf.usermodel.XSLFTextParagraph; import org.apache.poi.xslf.usermodel.XSLFTextRun; import org.apache.poi.xslf.usermodel.XSLFTextShape; public class PPT2ImageMaster { public String PptxToImage(String filePath) throws IOException{ //创建文件输入对象 File outdir = new File("F:\\21.文档转换\\测试文档\\浪潮交流"); File file=new File(filePath); FileInputStream fileinputstream =new FileInputStream(file); //读取幻灯片 XMLSlideShow ppt = new XMLSlideShow(fileinputstream); List<XSLFSlide> slides = ppt.getSlides(); Dimension pgsize = ppt.getPageSize(); int width = (int) (pgsize.width); int height = (int) (pgsize.height); for (int i=0 ;i< slides.size();i++) { XSLFSlide slide = slides.get(i); for (XSLFShape shape : ppt.getSlides().get(i).getShapes()) { if (shape instanceof XSLFTextShape) { XSLFTextShape tsh = (XSLFTextShape) shape; for (XSLFTextParagraph p : tsh) { for (XSLFTextRun r : p) { System.out.println(r.getFontFamily()); r.setFontFamily("微软雅黑"); } } } } BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = img.createGraphics(); DrawFactory.getInstance(graphics).fixFonts(graphics); // default rendering options graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); graphics.scale(1, 1); // draw stuff slide.draw(graphics); // save the result String outname = file.getName().replaceFirst(".pptx?", ""); outname = String.format(Locale.ROOT, "%1$s-%2$04d.%3$s", outname, i, "png"); File outfile = new File(outdir, outname); ImageIO.write(img, "png", outfile); graphics.dispose(); img.flush(); } return null; } public static void main(String[] args) { try { new PPT2ImageMaster().PptxToImage("F:\\21.文档转换\\测试文档\\ppttest.pptx"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
上一篇: Velocity学习(二)绝对路径 ContextPath
下一篇: 只有两行的表如何设置约束?