gpt4 book ai didi

java - 如何使用 Apache POI API 将图像添加到 pptx 中添加的图像占位符?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:23:16 25 4
gpt4 key购买 nike

我已经预定义了带有文本和图像占位符的 pptx 模板,我如何从模板访问和修改这些占位符。我可以使用 POI pptx API 将图像和文本直接添加到幻灯片,但是如何将它们添加到模板的占位符。

最佳答案

引用链接了解如何添加占位符以创建固定模板--
https://support.office.com/en-us/article/Add-a-text-placeholder-with-custom-prompt-text-85048846-c08c-4b7b-8f1a-fb25469903f3

这里是解决方案---

//read all shapes i.e place holder in array.
XMLSlideShow ppt = new XMLSlideShow(fin);
XSLFSlide[] slides = ppt.getSlides();
XSLFSlide slide1 =slides[0];
XSLFShape shapes[]= slide1.getShapes();
for(int i=0;i<shapes.length;i++){
System.out.println(shapes[i].getShapeName());
}

// add text to text place holder like this. assuming at index 0

XSLFShape title = shapes[0];
XSLFTextShape textShape = (XSLFTextShape) title;
textShape.clearText();
XSLFTextParagraph p = textShape.addNewTextParagraph();
XSLFTextRun r1 = p.addNewTextRun();
r1.setText("The");
r1.setFontColor(Color.blue);
r1.setFontSize(24.);

// replace picture text holder assuming at index2 2 and type autoshape

XSLFShape pic = shapes[2];
java.awt.geom.Rectangle2D anchor = pic.getAnchor();

byte[] pictureData = IOUtils.toByteArray(
new FileInputStream("C:\\Users\\gm807394\\Desktop\\Koala.jpg"));
int idx = ppt.addPicture(pictureData,
XSLFPictureData.PICTURE_TYPE_PNG);
XSLFPictureShape picture = slide1.createPicture(idx);
slide1.removeShape(pic);

picture.setAnchor(anchor);


FileOutputStream fos = new FileOutputStream(path);
ppt.write(fos);
fos.close();

关于java - 如何使用 Apache POI API 将图像添加到 pptx 中添加的图像占位符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35721547/

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