gpt4 book ai didi

apache-poi - 如何使用 apache poi 从 pptx 幻灯片获取背景数据

转载 作者:行者123 更新时间:2023-12-04 02:48:31 26 4
gpt4 key购买 nike

我正在尝试构建一个相当粗糙的工具,将 ppt/pptx 文件转换为 HTML 格式。
不幸的是,我发现 apache poi 没有提供用于处理 powerpoint 文件的统一编程模型,必须编写代码来解析每种格式。
我觉得 pptx 文件支持比 ppt 支持要有限得多。我面临的一个问题是获取有关 pptx 幻灯片的背景(颜色、图案、背景图像)的信息。

我发现 XSLFBackground (pptx api) 类比其相应的背景类 (ppt api) 受到更多限制。
有没有人设法使用 apache poi 获取有关 pptx 幻灯片背景的信息?

也有人可以指点我一些关于这个主题的好资源。我发现 apache poi javadoc 几乎无法使用,并且 poi 网站上的示例仅涵盖基本功能。

最好的问候,塞尔吉乌

最佳答案

背景元素的内容在Office Open Schema中描述。 - 检查 zip-link at the bottom和里面的 pml-slide.xsd。

掌握了模式后,您将了解用户模型接口(interface)下的 XML bean。

作为初学者,这里有一个读取背景图像以及将幻灯片导出为 png 的示例(可能对您的 html 导出有用?):

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import org.apache.poi.xslf.usermodel.*;
import org.openxmlformats.schemas.presentationml.x2006.main.CTBackground;

public class PptxBackground {
public static void main(String[] args) throws Exception {
// sorry for the content, but it was one of the first non-commercial google matches ...
URL url = new URL("http://newkilpatrickblog.typepad.com/files/sunday_june_03_2012_trinity_and_majesty_communion.pptx");
InputStream is = url.openStream();
XMLSlideShow ss = new XMLSlideShow(is);
is.close();

XSLFSlide sld = ss.getSlides()[0];
XSLFBackground bg = sld.getBackground();
CTBackground xmlBg = (CTBackground)bg.getXmlObject();
String relId = xmlBg.getBgPr().getBlipFill().getBlip().getEmbed();

XSLFPictureData pic = (XSLFPictureData)sld.getRelationById(relId);
String filename = pic.getFileName();
byte fileBytes[] = pic.getData();


/***** or convert the slides to images ****/

double zoom = 2; // magnify it by 2
AffineTransform at = new AffineTransform();
at.setToScale(zoom, zoom);

Dimension pgsize = ss.getPageSize();
XSLFSlide slides[] = ss.getSlides();
for (int i = 0; i < slides.length; i++) {
BufferedImage img = new BufferedImage((int)Math.ceil(pgsize.width*zoom), (int)Math.ceil(pgsize.height*zoom), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setTransform(at);

graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
slides[i].draw(graphics);
FileOutputStream out = new FileOutputStream("slide-" + (i+1) + ".png");
javax.imageio.ImageIO.write(img, "png", out);
out.close();
}
}
}

关于apache-poi - 如何使用 apache poi 从 pptx 幻灯片获取背景数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18292287/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com