@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 担任支持开发人员/传播者。