PPTX到PNG的颜色渲染--Apache POI

时间:2014-02-28 15:25:13

标签: java png apache-poi powerpoint

我正在使用POI库将pptx幻灯片转换为png,但它无法正确渲染幻灯片的颜色,例如,它不会渲染绿松石,而是呈现灰色。 我试图将pptx幻灯片转换为svg,但遇到了同样的问题,所以我想知道它是否与XMLSlideShow类或XSLFSlide类有关。 任何帮助将不胜感激!

代码:

String file = "C:\\Users\\ABC\\demo1.pptx";
XMLSlideShow ppt = null;

ppt = new XMLSlideShow(OPCPackage.open(new File(file)));    
Dimension pgsize = ppt.getPageSize();
float scale = 1;
int width = (int) (pgsize.width * scale);
int height = (int) (pgsize.height * scale);

XSLFSlide[] slide = ppt.getSlides();

for (int i = 0; i < slide.length; i++) {
    String title = slide[i].getTitle();

    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = img.createGraphics();

    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    graphics.setColor(Color.white);
    graphics.clearRect(0, 0, width, height);
    graphics.scale(scale, scale);

    slide[i].draw(graphics);

    int sep = file.lastIndexOf(".");
    String fname = file.substring(0, sep == -1 ? file.length() : sep) + "-" + (i + 1) +".png";

    FileOutputStream out = new FileOutputStream(fname);
    ImageIO.write(img, "png", out);
    out.close();
}   

1 个答案:

答案 0 :(得分:1)

这是apache POI库的问题。请使用旧版本,即可能解决您问题的3.7版本。