首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用Aspose和java替换ppt/word模板数据

如何用Aspose和java替换ppt/word模板数据
EN

Stack Overflow用户
提问于 2019-09-05 16:55:53
回答 1查看 198关注 0票数 0

我想使用Java和Aspose库更新一个PowerPoint模板:

例如:

我在ppt中有键和值作为

代码语言:javascript
运行
复制
firstname:${firstname} 
lastname:${lastname}

我有一个XML文件,其中包含以下数据:

代码语言:javascript
运行
复制
<firstname> Arjun </firstname>
<lastname>  Rathore</lastname>

我想用Arjun动态更新ppt的名字,用Rathore动态更新姓氏。

我已经尝试使用Aspose替换ppt模板中的文本,但替换没有按照预期发生。

代码语言:javascript
运行
复制
    String path="C:\\workspace\\src\\main\\resources\\testdata\\";
    Presentation presentation = new Presentation(path+"sample.pptx");

    presentation.joinPortionsWithSameFormatting();
    String strToReplace = "Done";
    ITextFrame[] tb = SlideUtil.getAllTextFrames(presentation, true);
    String strToFind = "sample";
    System.out.println("Before for");
    for (int i = 0; i < tb.length; i++)
    {
        for (IParagraph ipParagraph : tb[i].getParagraphs())
        {
            ipParagraph.getText().replace(strToFind,strToReplace);
            System.out.println("replaced");
            for (IPortion iPortion : ipParagraph.getPortions())
            {
                if (iPortion.getText().toLowerCase().contains(strToFind.toLowerCase()))
                {
                    iPortion.setText(iPortion.getText().toLowerCase().replace(strToFind,strToReplace));
                    System.out.println("replaced");
                }
            }
        }
    }
    presentation.save(path+"Output.pptx",SaveFormat.Pptx);

请参阅以下附件以供参考:

1)input_ppt_template

input_ppt_template screenshot

2)input_xml_data

input_xml_data screenshot

3)expected_output_ppt

expected_output_ppt screenshot

EN

回答 1

Stack Overflow用户

发布于 2019-09-12 01:05:21

@Karan Rathore

我已经观察到您共享的图像,并在此基础上创建了一个示例应用程序,该应用程序将根据从XML文件读取的数据替换文本。请尝试使用下面的示例作为您的友好参考。

代码语言:javascript
运行
复制
public static void TestTextReplacment()
{
    String path="C:\\Aspose Data\\XMLdata\\";
    Presentation presentation = new Presentation(path+"TextToReplace.pptx");
    List<Data>ListData=LoadXML();

    String[] strToFindArray= {"{{clientName}}","{{contactNumber}}","{{contactAddress}}"};

    presentation.joinPortionsWithSameFormatting();
    String strToReplace = "Done";
    ITextFrame[] tb = SlideUtil.getAllTextFrames(presentation, true);

    System.out.println("Before for");

    for(int x=0;x<strToFindArray.length;x++)
    {
        String strToFind=strToFindArray[x];

        for (int i = 0; i < tb.length; i++)
        {
            for (IParagraph ipParagraph : tb[i].getParagraphs())
            {
                //ipParagraph.getText().replace(strToFind,strToReplace);
                //System.out.println("replaced");
                for (IPortion iPortion : ipParagraph.getPortions())
                {
                    if (iPortion.getText().toLowerCase().contains(strToFind.toLowerCase()))
                    {
                        System.out.println(iPortion.getText());
                        //iPortion.setText(iPortion.getText().toLowerCase().replace(strToFind,strToReplace));
                        if(x==0)
                        {
                            iPortion.setText(iPortion.getText().toLowerCase().replace(strToFind.toLowerCase(),ListData.get(0).clientName)); 
                        }
                        else if(x==1)
                        {
                            iPortion.setText(iPortion.getText().toLowerCase().replace(strToFind.toLowerCase(),ListData.get(0).clientNumber)); 
                        }
                        else 
                        {
                            iPortion.setText(iPortion.getText().toLowerCase().replace(strToFind.toLowerCase(),ListData.get(0).clientAddress)); 
                        }
                        System.out.println("After: "+ iPortion.getText());

                        System.out.println("replaced");
                    }


                }

            }

        }

    }

    presentation.save(path+"Output.pptx",SaveFormat.Pptx);
}

public static List<Data> LoadXML()
{
    File file = new File("C:\\Aspose Data\\XMLdata\\TestData.xml");
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
                .newInstance();
    DocumentBuilder documentBuilder;
    Document document=null;
    try
    {
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
        document = documentBuilder.parse(file);
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }

    NodeList nList = document.getElementsByTagName("data");

    List<Data> dataList=new ArrayList<Data>();

      System.out.println("----------------------------");

   for (int temp = 0; temp < nList.getLength(); temp++) 
    {
       Node nNode = nList.item(temp);

       if (nNode.getNodeType() == Node.ELEMENT_NODE)
        {
            Data data=new Data();

            Element eElement = (Element) nNode;
             System.out.println(eElement.getNodeName());

            data.clientName=eElement.getElementsByTagName("clientName").item(0).getChildNodes().item(0).getNodeValue();
            data.clientNumber=eElement.getElementsByTagName("clientNumber").item(0).getChildNodes().item(0).getNodeValue();
            data.clientAddress=eElement.getElementsByTagName("clientAddress").item(0).getChildNodes().item(0).getNodeValue();
            dataList.add(data);
        }
    }

    return dataList;

}

我已经创建了一个数据类来从XML加载数据。

代码语言:javascript
运行
复制
public class Data {

    public String clientName;
public String clientNumber;
public String clientAddress;

Data()
{
        clientName="";
        clientNumber="";
        clientAddress="";
}
public String getclientName(){return clientName;}
public String getclientNumber(){return clientNumber;}   
public String getclientAddress(){return clientAddress;}

public void setclientName(String ClientName){clientName=ClientName;}
public void setclientNumber(String ClientNumber){clientNumber=ClientNumber;}    
public void setclientAddress(String ClientAddress ){clientAddress=ClientAddress;}

}

请提供演示文稿、源Attached here和生成的输出演示文稿供您参考。我希望现在分享的例子能对你有所帮助。

我在Aspose担任支持开发人员/布道者。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57801852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档