在Java中的PowerPoint中替换文本和图像

Creating a PowerPoint document from scratch can be quite time-consuming. It is getting even harder if you manage to create a well-designed document completely through java code. Therefore, we recommend pre-designing a template using MS PowerPoint and then replacing the text and images in it using Free Spire.Presentation for Java. By doing so, you can save some time and automate the process to some extend. The following sections will demonstrate the same.

Creating a Template

如下所示,我们准备了一个简单的模板,其中包含一个图像和几段文本。 文本已使用字体颜色,字体大小和字体名称进行了格式设置,以便文档中替换的新文本保留所需的样式。

Alt Text

Adding Jar to Project

Step 1. Download Free 小号pire.Presentation for Java package, unzip it. You’ll find Spire.Presentation.jar file under the lib folder.

步骤2.在IED中创建一个Java项目,并将jar文件添加为依赖项。 这是IntelliJ IDEA中的样子。

Alt Text

Using the Code

Part 1. Replace Text

import com.spire.presentation.*;

import java.util.HashMap;
import java.util.Map;

public class ReplaceText {

    public static void main(String[] args) throws Exception {

        //create a Presentation object
        Presentation presentation = new Presentation();

        //load the template file
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\template.pptx");

        //get the first slide
        ISlide slide= presentation.getSlides().get(0);

        //create a Map object
        Map<String, String> map = new HashMap<String, String>();

        //add several pairs of keys and values to the map
        map.put("#cityName#","Chengdu");
        map.put("#location#","Sichuan Province, China");
        String description = "Chengdu, formerly romanized as Chengtu, is a sub-provincial city which serves as the capital "+
                "of China's Sichuan province. It is one of the three most populous cities in Western China . As of 2014, "+
                "the administrative area houses 14,427,500 inhabitants, with an urban population of 10,152,632. At the time "+
                "of the 2010 census, Chengdu was the 5th-most populous agglomeration in China, with 10,484,996 inhabitants "+
                "in the built-up area including Xinjin County and Deyang's Guanghan City.";
        map.put("#description#",description);
        map.put("#popular#","Museum, Landmark, Religious Sites, Architecture, Parks");

        //replace text in the slide
        replaceText(slide,map);

        //save to another file
        presentation.saveToFile("output/ReplaceText.pptx", FileFormat.PPTX_2013);
    }

    /**
     * Replace text within a slide
     * @param slide Specifies the slide where the replacement happens
     * @param map Where keys are existing strings in the document and values are the new strings to replace the old ones
     */
    public static void replaceText(ISlide slide, Map<String, String> map) {

        for (Object shape : slide.getShapes()
        ) {
            if (shape instanceof IAutoShape) {

                for (Object paragraph : ((IAutoShape) shape).getTextFrame().getParagraphs()
                ) {
                    ParagraphEx paragraphEx = (ParagraphEx)paragraph;
                    for (String key : map.keySet()
                    ) {
                        if (paragraphEx.getText().contains(key)) {

                            paragraphEx.setText(paragraphEx.getText().replace(key, map.get(key)));
                         }
                    }
                }
            }
        }
    }
}

Alt Text

Part 2. Replace Image

import com.spire.presentation.*;
import com.spire.presentation.drawing.IImageData;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;

public class ReplaceImage {

    public static void main(String[] args) throws Exception {

        //create a Presentation object 
        Presentation presentation= new Presentation();

        //load the documenet generted by above code
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\ReplaceText.pptx");

        //add an image to the image collection 
        String imagePath = "C:\\Users\\Administrator\\Desktop\\Chengdu.jpeg";
        BufferedImage bufferedImage = ImageIO.read(new FileInputStream(imagePath));
        IImageData image = presentation.getImages().append(bufferedImage);

        //get the shape collection from the first slide 
        ShapeCollection shapes = presentation.getSlides().get(0).getShapes();

        //loop through the shape collection 
        for (int i = 0; i < shapes.getCount(); i++) {

            //determine if a shape is a picture 
            if (shapes.get(i) instanceof SlidePicture) {

                //fill the shape with a new image 
               ((SlidePicture) shapes.get(i)).getPictureFill().getPicture().setEmbedImage(image);
            }
        }

        //save to file 
        presentation.saveToFile("output/ReplaceImage.pptx", FileFormat.PPTX_2013);
    }
}

Alt Text

from: https://dev.to//eiceblue/replace-text-and-images-in-powerpoint-in-java-1ibj

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值