如何使用Apache POI API将图像添加到pptx中添加的图像占位符? [英] How to Add image to image placeholder added in pptx using Apache POI API?

查看:32
本文介绍了如何使用Apache POI API将图像添加到pptx中添加的图像占位符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

这是解决方案---

//读取所有形状,即数组中的占位符.XMLSlideShow ppt = new XMLSlideShow(fin);XSLFSlide[] 幻灯片 = ppt.getSlides();XSLFSlide slide1 =slides[0];XSLFShape 形状[]= slide1.getShapes();for(int i=0;i

I have predefined pptx template with text and Image place holder, How i can access and modify these placeholder from template. I can add image and text directly to slide using POI pptx API, but how to add same to placeholder of template.

解决方案

Refer link to see how to add place holder to create fixed template--
https://support.office.com/en-us/article/Add-a-text-placeholder-with-custom-prompt-text-85048846-c08c-4b7b-8f1a-fb25469903f3

Here is solution---

//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();

这篇关于如何使用Apache POI API将图像添加到pptx中添加的图像占位符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆