Java 将PPT转换为图片格式

2020-02-28  本文已影响0人  Tina_Tang

现如今,PPT已然成为了很多领域常用的办公软件之一。为了方便后期浏览,通常会将PPT进行转换处理,较为常用的是转换为图片格式。除了PPT幻灯片的整体转换外,PPT中的形状也可进行单独转换。本文就将通过使用Java程序来演示如何将完整的PPT幻灯片转换为图片格式(主要为BMP和SVG格式)及将PPT形状转换为图片格式(PNG格式)。

使用工具: Free Spire.Presentation for Java(免费版)

Jar文件获取及导入:

方法1通过官方网站下载获取jar包。解压后将lib文件夹下的Spire.Presentation.jar文件导入Java程序。(如下图)

方法2通过maven仓库安装导入。具体安装教程详见此网页

原文档截图:


【示例1】PowerPoint转BMP格式

import com.spire.presentation.Presentation;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import java.io.File;

public class ConvertBMP {

public static void main(String[] args) throws Exception {

Presentation ppt =new Presentation();

ppt.loadFromFile("D:\\Desktop\\Sample.pptx");

//将PPT保存为PNG格式

 for (int i = 0; i < ppt.getSlides().getCount(); i++) {

      BufferedImage image =ppt.getSlides().get(i).saveAsImage();

      String fileName = String.format("output/ToImage-%d.png", i);

            ImageIO.write(image,"PNG",new File(fileName));

        }

        ppt.dispose();

    }

}

转换效果:


【示例2】PowerPoint转SVG格式

import com.spire.presentation.Presentation;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.util.ArrayList;

public class ConvertSVG {

public static void main(String[] args) throws Exception {

Presentation ppt =new Presentation();

 ppt.loadFromFile("D:\\Desktop\\Sample.pptx");

//将PPT保存为SVG格式

 ArrayList<byte[]> svgBytes =(ArrayList<byte[]>) ppt.saveToSVG();

int count = svgBytes.size();

int len = svgBytes.size();

for (int i = 0; i < len; i++)

        {

byte[] bytes = svgBytes.get(i);

FileOutputStream stream =new FileOutputStream(String.format("output/ToSVG-%d.svg", i));

   stream.write(bytes);

        }

   ppt.dispose();

    }

}

转换效果:


【示例3】PPT形状(表格、文本框、三角形、图表)转图片格式

原文档截图:

代码示例:

import com.spire.presentation.Presentation;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import java.io.File;

public class ConvertPNG {

public static void main(String[] args) throws Exception {

 String inputFile ="D:\\Desktop\\Sample2.pptx";

 String outputPath ="output/";

  //创建实例

  Presentation ppt = new Presentation();

  //加载文件

ppt.loadFromFile(inputFile);

for (int i = 0; i < ppt.getSlides().get(0).getShapes().getCount(); i++)

        {

 String fileName = outputPath + String.format("shapeToImage-%1$s.png", i);

//将shape保存为image对象

 BufferedImage image = ppt.getSlides().get(0).getShapes().saveAsImage(i);

//写出图片

 ImageIO.write(image,"PNG"new File(fileName));

        }

    }

}

转换效果:

(本文完)

上一篇 下一篇

猜你喜欢

热点阅读