【发布时间】:2019-09-05 08:55:52
【问题描述】:
我想使用 Java 和 Aspose 库更新 PowerPoint 模板:
例如:
我在ppt中有key和value作为
firstname:${firstname}
lastname:${lastname}
我有一个包含以下数据的 XML 文件:
<firstname> Arjun </firstname>
<lastname> Rathore</lastname>
我想用Arjun动态更新ppt名字,用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
2)input_xml_data
3)expected_output_ppt
【问题讨论】:
标签: java aspose.words aspose-slides