[英]How to Add image to image placeholder added in pptx using Apache POI API?
我已經預定義了帶文本和圖像占位符的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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.