Page 1 of 1

Read PPTX file using java and generate Powerpoint

PostPosted: Thu May 24, 2018 1:11 am
by Saranya
Hi All,

I have an requirement where I need to generate Powerpoint from my webpage
using JAVA . I have an sample PPT template with me.
Is it possible to read the input PPTX file and generate out PPTX file with all the titles and Table values and images populated inside?

Could anyone help me out providing an sample code for it. I would greatly appreciate for this help!

Thanks in advance!


Regards,
Saranya

Re: Read PPTX file using java and generate Powerpoint

PostPosted: Fri May 25, 2018 12:00 am
by Saranya
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

Re: Read PPTX file using java and generate Powerpoint

PostPosted: Fri May 25, 2018 7:59 pm
by jason
You're almost there.

If you take a look at the VariableReplace sample, you'll see that your variables on the slide should look like so: ${SlideTitleName}

And then your hashmap: h.put("SlideTitleName", "SARANYA");

Obviously you need to invoke variableReplace on each slide.

If it doesn't work, its probably because Powerpoint has "split your run". You'll need to ensure the variable eg ${SlideTitleName} is in a single run.

Re: Read PPTX file using java and generate Powerpoint

PostPosted: Fri May 25, 2018 10:48 pm
by Saranya
Hi Jason,

Thanks for your reply.I have done the changes suggested ,


public static void main(String[] args) throws IOException, Docx4JException, Pptx4jException, JAXBException
{
String inputfilepath = "C:\\Work\\24Jan2018_CheckOut\\dhl\\PPT-TRAILS\\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);


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);

}

But still text not replacing. I tried with a new PPTX file with having only text "${SlideTitleName}" inside PPT. But still its not working. I could see in the forum you are the life saver of many. Please help me to come out of this problem.

Thanks.

Re: Read PPTX file using java and generate Powerpoint

PostPosted: Sun May 27, 2018 2:31 pm
by jason
I see you've elected to move this post to https://stackoverflow.com/questions/505 ... using-java

So I'm closing this thread; further discussion can take place there.