簡體   English   中英

如何使用 Aspose 和 java 替換 ppt/word 模板數據

[英]How to replace ppt/word template data using Aspose and java

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

例如:

我在 ppt 中有 key 和 value 作為

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

我有一個包含以下數據的 XML 文件:

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

我想用Arjun動態更新 ppt firstname,用Rathore動態更新 lastname。

我嘗試使用以下代碼使用 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 截圖

我觀察了您共享的圖像,並基於該圖像創建了一個示例應用程序,該應用程序將根據從 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 擔任支持開發人員/傳播者。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM