by Saranya » Fri May 25, 2018 12:00 am
Hi,
Could you please help me out in this. I have an PPT with values {VALUES} format and an xml file to map it. I would like to replace the text of the PPT file containing ,
{SAMPLE} to SARAN and so on using Java. Earlier i tried with Apache poi and i had no solution but i could find that in Docx4j there is an method names variableReplace() which will replace the text. I tried one sample but i dont see any replace activity.
Please please help me out how to read the pptx file and replace the text inside ppt using java,
My sample trail,
public static void main(String[] args) throws IOException, Docx4JException, Pptx4jException, JAXBException
{
String inputfilepath = "C:\\Work\\24Jan2018_CheckOut\\dhl\\SampleTemplate.pptx";
PresentationMLPackage pptPackage = PresentationMLPackage.load(new File(inputfilepath));
ThemePart themeSlidePart = (ThemePart)pptPackage.getParts().getParts().get(new PartName("/ppt/theme/theme1.xml"));
Theme themeOfSlides = (Theme)themeSlidePart.getJaxbElement();
System.out.println("Theme of original PPT : " + XmlUtils.marshaltoString(themeOfSlides, true));
SlidePart slide = pptPackage.getMainPresentationPart().getSlide(0);
HashMap h = new HashMap<String, String>();
h.put("{SlideTitleName}", "SARANYA");
h.put("HIII", "{SlideTitleName}");
slide.variableReplace(h);
Set set = h.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();
System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
System.out.println("mapEntry:::::"+mentry.getValue());
System.out.println("mapEntry 222:::::"+h.get(mentry.getKey()));
}
String outputfilepath = "C:\\Work\\24Jan2018_CheckOut\\dhl\\PPT-TRAILS\\SampleTemplate.pptx";
PresentationMLPackage pptPackagetoApply = PresentationMLPackage.load(new File(outputfilepath));
ThemePart themeSlidePartToApply = (ThemePart)pptPackagetoApply.getParts().getParts().get(new PartName("/ppt/theme/theme1.xml"));
themeSlidePartToApply.setJaxbElement(themeOfSlides);
SaveToZipFile saver = new SaveToZipFile(pptPackagetoApply);
saver.save(outputfilepath);
}
Appreciate greatly for you help. Please do help me and let me know what i need to do to get the requirement done. Thanks in advance.
With Regards,
Saranya C
- Attachments
-
- slide1.zip
- (6.68 KiB) Downloaded 411 times
-
- SampleTemplate.pptx
- (226.93 KiB) Downloaded 432 times