使用APACHE POI将PPT转换为Image时,汉字转换为正方形

时间:2010-04-22 00:44:59

标签: java apache-poi powerpoint image

当我尝试使用Apache POI项目将PPT转换为图像时出现问题。我的代码如下:

FileInputStream is = new FileInputStream("test.ppt");

SlideShow ppt = new SlideShow(is);


is.close();

Dimension pgsize = ppt.getPageSize();

Slide[] slide = ppt.getSlides();

for (int i = 0; i < slide.length; i++) {

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

//save the output
FileOutputStream out = new FileOutputStream("slide-" + (i+1) + ".png");
javax.imageio.ImageIO.write(img, "png", out);
out.close();

除了将所有中文单词转换为某些正方形外,它的工作正常。那怎么解决这个问题呢?

3 个答案:

答案 0 :(得分:3)

这似乎是apache POI的一个错误。我已经在bugzilla中添加了它

https://issues.apache.org/bugzilla/show_bug.cgi?id=54880

答案 1 :(得分:2)

问题不在POI方面,而是在JVM字体设置中。

您需要在JVM字体列表(/usr/lib/jvm/jdk1.8.0_20/jre/lib/fonts或类似字体)中将字体设置为1,例如simsun.ttc。

XSLFTextShape[] phs = slide[i].getPlaceholders();
for (XSLFTextShape ts : phs) {
  java.util.List<XSLFTextParagraph> tpl = ts.getTextParagraphs();
  for(XSLFTextParagraph tp: tpl) {
    java.util.List<XSLFTextRun> trs = tp.getTextRuns();
    for(XSLFTextRun tr: trs) {
      logger.info(tr.getFontFamily());
      tr.setFontFamily("SimSun");
    }
  }
}

答案 2 :(得分:1)

问题在于FileOuputStream的使用,它总是以默认系统编码将数据写入文件,该编码很可能是针对Windows的ISO-8859_1。此编码不支持中文字符。您需要创建一个流,您可以使用需要创建阅读器的UTF-8编码进行编写。我正在查看API,但没有发现任何以读者为参数的方法。但请检查ImageOutputStream是否可以帮助您。

相关问题