JAVA怎样将PPTX文件转换成图片

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA怎样将PPTX文件转换成图片相关的知识,希望对你有一定的参考价值。

关键代码就是: rtruns[l].setFontName("宋体");   

import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.record.Slide;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;

public class PPTtoImage
public static void main(String[] args)
// 读入PPT文件
File file = new File("F:/20110618.ppt");
doPPTtoImage(file);

public static boolean doPPTtoImage(File file)
boolean isppt = checkFile(file);
if (!isppt)
System.out.println("The image you specify don\'t exit!");
return false;

try

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++)
System.out.print("第" + 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++)
int index = rtruns[l].getFontIndex();
String name = rtruns[l].getFontName();
rtruns[l].setFontIndex(1);
rtruns[l].setFontName("宋体");
System.out.println(rtruns[l].getText());


BufferedImage 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[i].draw(graphics);

// 这里设置图片的存放路径和图片的格式(jpeg,png,bmp等等),注意生成文件路径
FileOutputStream out = new FileOutputStream("F:/test/pict_"
+ (i + 1) + ".jpeg");
javax.imageio.ImageIO.write(img, "jpeg", out);
out.close();


System.out.println("success!!");
return true;
catch (FileNotFoundException e)
System.out.println(e);
// System.out.println("Can\'t find the image!");
catch (IOException e)

return false;

// function 检查文件是否为PPT
public static boolean checkFile(File file)

boolean isppt = false;
String filename = file.getName();
String suffixname = null;

if (filename != null && filename.indexOf(".") != -1)
suffixname = filename.substring(filename.indexOf("."));
if (suffixname.equals(".ppt"))
isppt = true;

return isppt;
else
return isppt;

参考技术A

添加依赖spire.presentation.jar,使用下面的代码

    //创建Presentation对象

    Presentation ppt = new Presentation();

    //加载示例文档

    ppt.loadFromFile("C:/Users/Administrator/Desktop/example.pptx");

    //遍历幻灯片

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

    //将幻灯片保存为BufferedImage对象

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

    //将BufferedImage保存为PNG格式文件

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

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

    ppt.dispose();

以上是关于JAVA怎样将PPTX文件转换成图片的主要内容,如果未能解决你的问题,请参考以下文章

microsoft office 2007 PPTX文件导出为PDF文件后,PDF文件中图片质量下降,如何解决?

怎样将pdf文件转换成图片,pdf转图片的小技巧

怎样将pdf文件转换成图片,pdf转图片的小技巧

pdf在线怎样转图片

怎样将jpg.转换成png.格式?

怎样将图片格式转换成PDF格式?