用poi把ppt或pptx转为图片

代码主要参考官网:http://poi.apache.org/slideshow/how-to-shapes.html

需要jar下载地址:http://download.youkuaiyun.com/detail/emoven/9657787

<pre name="code" class="html">import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;

import org.apache.poi.hslf.usermodel.HSLFShape;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
import org.apache.poi.hslf.usermodel.HSLFTextParagraph;
import org.apache.poi.hslf.usermodel.HSLFTextRun;
import org.apache.poi.hslf.usermodel.HSLFTextShape;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xslf.usermodel.XSLFTextShape;

/*
 * ppt或pptx 转为图片
 */
public class PPTtoImage {
	public static void main(String[] args) throws Exception {
		toImage2007();
		toImage2003();
		
		
	}
	public static void toImage2007() throws Exception{
			FileInputStream is = new FileInputStream("D:/demo/11.pptx");
			XMLSlideShow ppt = new XMLSlideShow(is);
			is.close();
			
			Dimension pgsize = ppt.getPageSize();
			System.out.println(pgsize.width+"--"+pgsize.height);
			
			for (int i = 0; i < ppt.getSlides().size(); i++) {
				try {
					//防止中文乱码
					for(XSLFShape shape : ppt.getSlides().get(i).getShapes()){
						if(shape instanceof XSLFTextShape) {
							XSLFTextShape tsh = (XSLFTextShape)shape;
							for(XSLFTextParagraph p : tsh){
								for(XSLFTextRun r : p){
									r.setFontFamily("宋体");
								}
							}
						}
					}
					BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
					Graphics2D graphics = img.createGraphics();
					// clear the drawing area
					graphics.setPaint(Color.white);
					graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
					
					// render
					ppt.getSlides().get(i).draw(graphics);
					
					// save the output
					String filename = "D:/demo/07-" + (i+1) + ".jpg";
					System.out.println(filename);
					FileOutputStream out = new FileOutputStream(filename);
					javax.imageio.ImageIO.write(img, "png", out);
					out.close();
				} catch (Exception e) {
					System.out.println("第"+i+"张ppt转换出错");
				}
			}
			System.out.println("7success");
	}
	
	public static void toImage2003(){
		try {
			HSLFSlideShow ppt = new HSLFSlideShow(new HSLFSlideShowImpl("D:/demo/22.ppt"));
			
			Dimension pgsize = ppt.getPageSize();
			for (int i = 0; i < ppt.getSlides().size(); i++) {
				//防止中文乱码
				for(HSLFShape shape : ppt.getSlides().get(i).getShapes()){
					if(shape instanceof HSLFTextShape) {
						HSLFTextShape tsh = (HSLFTextShape)shape;
						for(HSLFTextParagraph p : tsh){
							for(HSLFTextRun r : p){
								r.setFontFamily("宋体");
							}
						}
					}
				}
				BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
				Graphics2D graphics = img.createGraphics();
				// clear the drawing area
				graphics.setPaint(Color.white);
				graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
				
				// render
				ppt.getSlides().get(i).draw(graphics);
				
				// save the output
				String filename = "D:/demo/03-760-" + (i+1) + ".jpg";
				System.out.println(filename);
				FileOutputStream out = new FileOutputStream(filename);
				javax.imageio.ImageIO.write(img, "png", out);
				out.close();
//				resizeImage(filename, filename, width, height);
				
			}
			System.out.println("3success");
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	 /*** 
     * 功能 :调整图片大小
     * @param srcImgPath 原图片路径 
     * @param distImgPath  转换大小后图片路径 
     * @param width   转换后图片宽度 
     * @param height  转换后图片高度 
     */  
    public static void resizeImage(String srcImgPath, String distImgPath,  
            int width, int height) throws IOException {  
  
        File srcFile = new File(srcImgPath);  
        Image srcImg = ImageIO.read(srcFile);  
        BufferedImage buffImg = null;  
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
        buffImg.getGraphics().drawImage(  
                srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0,  
                0, null);  
  
        ImageIO.write(buffImg, "JPEG", new File(distImgPath));  
  
    }  
}

 



评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值