Page 1 of 1

Noob trying to generate simple text pptx

PostPosted: Sat May 31, 2014 6:13 am
by asantas93
I'm really new to this library--can some tell me how I can generate a pptx file and populate it with slides containing text that my program specifies.

Specifically, I have a PresentationMLPackage that was created using an empty .pptx file. I want to make a method that looks like:

Code: Select all
private void addSlide(PresentationMLPackage, String title, String body)


or something similar. Any help is appreciated!

Re: Noob trying to generate simple text pptx

PostPosted: Sat May 31, 2014 7:39 am
by jason
Hello, and welcome.

Did you see the CreateHelloWorld sample?

It contains:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                // Need references to these parts to create a slide
                // Please note that these parts *already exist* - they are
                // created by createPackage() above.  See that method
                // for instruction on how to create and add a part.
                MainPresentationPart pp = (MainPresentationPart)presentationMLPackage.getParts().getParts().get(
                                new PartName("/ppt/presentation.xml"));        
                SlideLayoutPart layoutPart = (SlideLayoutPart)presentationMLPackage.getParts().getParts().get(
                                new PartName("/ppt/slideLayouts/slideLayout1.xml"));
               
                // OK, now we can create a slide
                SlidePart slidePart = new SlidePart(new PartName("/ppt/slides/slide1.xml"));
                slidePart.setContents( SlidePart.createSld() );        
                pp.addSlide(0, slidePart);
               
                // Slide layout part
                slidePart.addTargetPart(layoutPart);
               
                               
                // Create and add shape
                Shape sample = ((Shape)XmlUtils.unmarshalString(SAMPLE_SHAPE, Context.jcPML) );
                slidePart.getContents().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(sample);
 
Parsed in 0.018 seconds, using GeSHi 1.0.8.4


For help writing the code for the slide content, try the docx4j webapp (linked in menu above). You can upload a sample pptx, then generate code for a slide by clicking through to it.

Re: Noob trying to generate simple text pptx

PostPosted: Tue Jun 03, 2014 6:06 am
by asantas93
Thanks for responding! I've managed to add parts to my empty presentation, but I don't see how to edit them or how to just add an xml file to the presentation. Can you help with that?

Re: Noob trying to generate simple text pptx

PostPosted: Tue Jun 03, 2014 7:08 am
by jason
For how the pptx file format works, please read http://openxmldeveloper.org/blog/b/open ... /1970.aspx

For how docx4j/pptx4j works, please read our Getting Started guide.

After consulting the above, you'll understand what you are doing when you use the docx4j webapp (linked in menu above) to help you create slide content.

If you need help after working through the above, you are welcome to post your code, and a simple pptx showing what you are trying to achieve.