码迷,mamicode.com
首页 > Web开发 > 详细

以poi实现PPT转html

时间:2018-09-25 23:28:41      阅读:397      评论:0      收藏:0      [点我收藏+]

标签:richtext   dwr   css   doc   div   NPU   文档   tput   add   

哈哈,终于两个月了(笔者不玩网游,虽然脾气越来越不好,但是生活越来越充实)。不扯了,今晚还要再写一篇呢,直接上代码。

            package com.css.java.learning.massbag;

            import java.awt.Color;
            import java.awt.Dimension;
            import java.awt.Graphics2D;
            import java.awt.geom.Rectangle2D;
            import java.awt.image.BufferedImage;
            import java.io.BufferedWriter;
            import java.io.File;
            import java.io.FileInputStream;
            import java.io.FileNotFoundException;
            import java.io.FileOutputStream;
            import java.io.IOException;
            import java.io.OutputStreamWriter;
            import java.util.ArrayList;
            import java.util.List;
            import org.apache.poi.hslf.model.TextRun;
            import org.apache.poi.hslf.usermodel.RichTextRun;
            import org.apache.poi.hslf.usermodel.SlideShow;

            /**
             * ppt转html
             * @author Red_Ant
             * 20180925
             */
            public class PptToHtml {

                private static void pptToPicture(String tempContextUrl, String outPath){
                    //文件夹名
                    List<String> imgList = new ArrayList<String>();
                    File file = new File(tempContextUrl);
                    File folder = new File(outPath + File.separator + "20180925");
                    try {   
                        folder.mkdirs();
                                    FileInputStream is = new FileInputStream(file);   
                                    SlideShow ppt = new SlideShow(is);   
                                    is.close();   
                                    Dimension pgsize = ppt.getPageSize();   
                                    org.apache.poi.hslf.model.Slide[] slide = ppt.getSlides();   
                                    for (int i = 0; i < slide.length; i++) {
                                            TextRun[] truns = slide[i].getTextRuns();      
                                            for ( int k=0;k<truns.length;k++){      
                                                 RichTextRun[] rtruns = truns[k].getRichTextRuns();      
                                                for(int l=0;l<rtruns.length;l++){      
                                                            rtruns[l].setFontIndex(1);      
                                                            rtruns[l].setFontName("宋体");  
                                                 }      
                                            }      
                                            BufferedImage img = new BufferedImage(pgsize.width,pgsize.height, BufferedImage.TYPE_INT_RGB);   
                                            Graphics2D graphics = img.createGraphics();   
                                            graphics.setPaint(Color.BLUE);   
                                            graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));   
                                            slide[i].draw(graphics);   
                                            String imgName = File.separator + "20180925" + File.separator +"pict_"+ (i + 1) + ".jpeg";
                                            FileOutputStream out = new FileOutputStream(outPath + imgName);   
                                            javax.imageio.ImageIO.write(img, "jpeg", out);
                                            out.close();   
                                            imgList.add("20180925" + File.separator +"pict_"+ (i + 1) + ".jpeg");
                                    }
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                    createPPTHtml(outPath,imgList, tempContextUrl);
                }

                /**自己创建的html代码
                 * 原理上就是,把上一步ppt转的图片
                 * 以html的方式呈现出来
                 */
                public static void createPPTHtml(String wordPath,List<String> imgList,String tempContextUrl){
                    StringBuilder sb = new StringBuilder("<!doctype html><html><head><meta charset=‘utf-8‘><title>无标题文档</title></head><body><div align=\"center\">");
                    if (imgList != null && !imgList.isEmpty()) {
                        for (String img : imgList) {
                            sb.append("<img src=‘" + img + "‘ /><br>");
                        }
                    }
                    sb.append("</div></body></html>");
                    try {
                        File file = new File(wordPath + File.separator + "20180925.html");
                        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"UTF-8"));
                        bufferedWriter.write(sb.toString());
                        bufferedWriter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                public static void main(String[] args) {
                    pptToPicture("D:\\red_ant_file\\20180925\\阿里巴巴网店的各大模块.ppt",
                            "D:\\red_ant_file\\20180925");
                }

            }

【后话】
awt包下的代码基本用不到了,现在出现仅仅是因为笔者比较无聊,纯属娱乐。
运行后。

技术分享图片

技术分享图片

技术分享图片

以poi实现PPT转html

标签:richtext   dwr   css   doc   div   NPU   文档   tput   add   

原文地址:http://blog.51cto.com/13479739/2285594

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!