pptx转图片

package com;

import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.xslf.usermodel.*;

import java.awt.*;
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.InputStream;

public class ImageConvert {

    public static void converter(String fileName) {
        // BackRS rs = new BackRS();
        File file = new File(fileName);
        String name = file.getName();
        String filePath = file.getParent();
        try {
            FileInputStream in = new FileInputStream(file);
            XMLSlideShow ppt = new XMLSlideShow(in);
            Dimension pgsize = ppt.getPageSize();
            String saveImagePathName = filePath + File.separator + getFileNameNoEx(name) + "_JPG";
            File path = new File(saveImagePathName);
            if (!path.exists()) {
                path.mkdir();
            }
            Integer i = 0;
            BufferedImage img = null;
            String mp4Name = "";
            for (XSLFSlide slide : ppt.getSlides()) {

                //解决乱码问题
                for (XSLFShape shape : slide.getShapes()) {
                    if (shape instanceof XSLFTextShape) {
                        XSLFTextShape tsh = (XSLFTextShape) shape;
                        for (XSLFTextParagraph p : tsh) {
                            for (XSLFTextRun r : p) {
                                r.setFontFamily("宋体");
                            }
                        }
                    }
                }
                //将pptx的每一页保存成图片
                img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics = img.createGraphics();
                graphics.setPaint(Color.white);
                graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
                slide.draw(graphics);
                FileOutputStream out = new FileOutputStream(path + File.separator + (i + 1) + ".JPG");
                i++;
                javax.imageio.ImageIO.write(img, "JPG", out);
                out.close();

                for (POIXMLDocumentPart part : slide.getRelations()) {
                    String contentType = part.getPackagePart().getContentType();
                    //取pptx中的图片
//                    if (contentType.equals("image/jpeg")||contentType.equals("image/png")) {
//                        InputStream is = part.getPackagePart().getInputStream();
//                        FileOutputStream fos = new FileOutputStream(path + File.separator + (i + 1) + ".jpg");
//                        i++;
//                        byte[] b = new byte[1024];
//                        int length;
//                        while ((length = is.read(b)) > 0) {
//                            fos.write(b, 0, length);
//                        }
//                        is.close();
//                        fos.close();
//                    }else
                    if (contentType.equals("video/mp4")) {//判断类型是否为mp4
                        if (name.indexOf(String.valueOf(part.getPackagePart().getPartName())) == -1) {//因为相同的MP4会出现两次,所以做此判断
                            name += "," + part.getPackagePart().getPartName();
                        } else {
                            continue;
                        }
                        InputStream is = part.getPackagePart().getInputStream();
                        FileOutputStream fos = new FileOutputStream(path + File.separator + (i + 1) + ".mp4");
                        i++;
                        byte[] b = new byte[1024];
                        int length;
                        while ((length = is.read(b)) > 0) {
                            fos.write(b, 0, length);
                        }
                        is.close();
                        fos.close();
                    }
                }
            }
            System.out.println("Image successfully converted.");
        } catch (Exception e) {
            System.out.println("error:" + e.getMessage());
        }
    }

    /**
     * 获取文件名,去除扩展名的
     */
    public static String getFileNameNoEx(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length()))) {
                return filename.substring(0, dot);
            }
        }
        return filename;
    }

    public static void main(String[] args) {
        System.out.println("Hello World!");
        // ImageConvert imageConvert=new ImageConvert();
        converter("E:\\spring-boot\\SpireDemo\\1.pptx");
        System.out.println("successfully!");
    }
}

 

pom.xml

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>4.1.2</version>
</dependency>

 

https://www.cnblogs.com/lgx5/p/5662571.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
namespace PPT2Img { class Program { [STAThread] static void Main(string[] args) { TaskTimerRecorder taskTimerRecorder = new TaskTimerRecorder(); //确认需要执行的操作 //getinfo:获取总页数 //convert:将指定起始页码换到指定文件夹 string strActionMode = "getinfo"; //拿到输入文件名 string strPPTFileName = "PPT路径"; //拿到输出图片路径 string strOutputFilePath = "图片输出路径"; //拿到输出图片的名词前缀(后缀就是从0计数的页码) string strPrefix = "输出图片前缀"; //需要抠掉的颜色 uint nColor = 0xffffffff; //需要输出的起始页码 int nBeginPage = 0; //需要输出的结束页码(-1为全部输出) int nEndPage = -1; //宽高 int nWidth = 1920; int nHeight = 1080; if (args.Length > 0) { strActionMode = args[0]; if (args.Length > 1) { strPPTFileName = args[1]; } if (args.Length > 2) { strOutputFilePath = args[2]; if (strActionMode == "getinfo") { Console.WriteLine("工作模式是:" + strActionMode); Console.WriteLine("PPT路径是:" + strPPTFileName); Console.WriteLine("结果输出路径是:" + strOutputFilePath); Console.WriteLine("开始执行"); ESBasic.Office.OfficeScanner scaner = new ESBasic.Office.OfficeScanner(); int nPage = scaner.GetInfo(strPPTFileName); FileStream fs = File.Create(strOutputFilePath + "pagecount.txt"); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(nPage.ToString()); sw.Close(); fs.Close(); Console.WriteLine("执行结束"); return; } else if (strActionMode == "convert") { if (args.Length > 3) { strPrefix = args[3]; if (args.Length > 4) { string strColor = args[4]; nColor = uint.Parse(strColor, System.Globalization.NumberStyles.HexNumber); if (args.Length > 5) { string strBeginPage = args[5]; nBeginPage = int.Parse(strBeginPage); if (args.Length > 6) { string strEndPage = args[6]; nEndPage = int.Parse(strEndPage); if (args.Length > 7) { string strWidth = args[7]; nWidth = int.Parse(strWidth); if (args.Length > 8) { string strHeight = args[8]; nHeight = int.Parse(strHeight); //Console.WriteLine("1:工作模式是:" + strActionMode); //Console.WriteLine("2:PPT路径是:" + strPPTFileName); //Console.WriteLine("3:图片输出路径是:" + strOutputFilePath); //Console.WriteLine("4:图片输出前缀是:" + strPrefix); //Console.WriteLine("5:需要抠掉的颜色是:" + nColor.ToString()); //Console.WriteLine("6:输出起始页码是:" + nBeginPage.ToString()); //Console.WriteLine("7:输出结束页码是:" + nEndPage.ToString()); //Console.WriteLine("8:输出宽度是" + nWidth.ToString()); //Console.WriteLine("9:输出高度是" + nHeight.ToString()); //Console.WriteLine("1:" + strActionMode); //Console.WriteLine("2:" + strPPTFileName); //Console.WriteLine("3:" + strOutputFilePath); //Console.WriteLine("4:" + strPrefix); //Console.WriteLine("5:" + nColor.ToString()); //Console.WriteLine("6:" + nBeginPage.ToString()); //Console.WriteLine("7:" + nEndPage.ToString()); //Console.WriteLine("8:" + nWidth.ToString()); //Console.WriteLine("9:" + nHeight.ToString()); ESBasic.Office.OfficeScanner scaner = new ESBasic.Office.OfficeScanner(); int nPage = scaner.Convert(strPPTFileName, strOutputFilePath, strPrefix, nColor, nBeginPage, nEndPage, nWidth, nHeight); FileStream fs = File.Create(strOutputFilePath + "pagecount.txt"); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(nPage.ToString()); sw.Close(); fs.Close(); return; } } } } } } Console.WriteLine("1:" + strActionMode); Console.WriteLine("2:" + strPPTFileName); Console.WriteLine("3:" + strOutputFilePath); Console.WriteLine("4:" + strPrefix); Console.WriteLine("5:" + nColor.ToString()); Console.WriteLine("6:" + nBeginPage.ToString()); Console.WriteLine("7:" + nEndPage.ToString()); Console.WriteLine("8:" + nEndPage.ToString()); Console.WriteLine("9:" + nEndPage.ToString()); Console.WriteLine("10"); } } } else { #if false //无参数模式,给一些参数,调试使用 strActionMode = "convert"; strPPTFileName = "C:\\Users\\dujiangtao\\Desktop\\测试2.pptx"; strOutputFilePath = "E:\\output\\"; strPrefix = "ppt"; nColor = 0x000000ff; nWidth = 1920; nHeight = 1080; nColor = uint.Parse("000000ff", System.Globalization.NumberStyles.HexNumber); nBeginPage = 0; nEndPage = -1; ESBasic.Office.OfficeScanner scaner = new ESBasic.Office.OfficeScanner(); int nPage = scaner.Convert(strPPTFileName, strOutputFilePath, strPrefix, nColor, nBeginPage, nEndPage, nWidth, nHeight); FileStream fs = File.Create(strOutputFilePath + "pagecount.txt"); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(nPage.ToString()); sw.Close(); fs.Close(); taskTimerRecorder.Stop(); Console.ReadLine(); #endif } } } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值