'How to replace ppt/word template data using Aspose and java
I want to update a PowerPoint template using Java and Aspose library:
For Example:
I have key and value in ppt as
firstname:${firstname}
lastname:${lastname}
I have an XML file which contains below data:
<firstname> Arjun </firstname>
<lastname> Rathore</lastname>
I want to dynamically update the ppt firstname with Arjun
and lastname with Rathore
.
i have tried below code to replace text in ppt templae using Aspose but replacement is not happening as per expectation.
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);
Please find below attachment for reference:
1)input_ppt_template
2)input_xml_data
3)expected_output_ppt
Solution 1:[1]
I have observed the images shared by you and based on that created a sample application that shall do the replacement of the text based on data read from XML file. Please try using following sample example for your kind reference.
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;
}
I have created a Data class to load data from 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;}
}
Attached here, please source presentation, source XML and generated output presentation for your reference. I hope the shared example will be helpful to you now.
I am working as Support developer/ Evangelist at Aspose.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Wai Ha Lee |