0

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

例如:

我在 ppt 中有 key 和 value 作为

firstname:${firstname} 
lastname:${lastname}

我有一个包含以下数据的 XML 文件:

<firstname> Arjun </firstname>
<lastname>  Rathore</lastname>

我想动态更新 ppt firstname withArjun和 lastname with Rathore

我尝试使用以下代码使用 Aspose 替换 ppt 模板中的文本,但替换没有按预期进行。

    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 截图

2)input_xml_data

input_xml_data 截图

3)expected_output_ppt

expected_output_ppt 截图

4

1 回答 1

0

@Karan Rathore,

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

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;

}

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

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;}

}

附在此处,请提供源演示文稿、源 XML 和生成的输出演示文稿供您参考。我希望共享的示例现在对您有所帮助。

我在 Aspose 担任支持开发人员/传播者。

于 2019-09-11T17:05:21.187 回答