Page 1 of 1

Creating new slide layouts

PostPosted: Wed Apr 20, 2011 12:59 am
by alunsford3
I've managed to work with the library pretty well so far, but have become stuck when trying to create a slide with a layout other than slideLayout1.xml, which is the Title and Subtitle slide. I want to create a slide that has a Title and Content box, which I have identified as slideLayout2.xml in the ppt/slideLayouts/ folder of a pptx. My code for creating the layouts is as follows:
Code: Select all
PartName pn = new PartName("/ppt/slideLayouts/slideLayout"+slideNumber+".xml");
System.err.println("PC> tpn: "+pn.toString());
tempLayout = (SlideLayoutPart)presMLPackage.getParts().getParts().get(pn);
if (tempLayout != null){
   String layoutString = this.getLayoutXML(slideName);
   System.err.println("layoutXML: " + layoutString.substring(305, 341));
   SldLayout temp = (SldLayout) XmlUtils.unmarshalString(layoutString, Context.jcPML);
   tempLayout.setJaxbElement(temp);
}else{
   throw new Exception();
}

I can successfully create slideLayout1.xml, but when I try to create slideLayout2.xml tempLayout becomes null and then fails when trying to setJaxbElement (if the null check was removed).

I really hope I am just forgetting something because I have enjoyed working this library and it has saved me a lot of time and headaches up until this point. Thanks for your help.

Re: Creating new slide layouts

PostPosted: Wed Apr 20, 2011 1:38 am
by jason
To start with the obvious question, is presMLPackage a new pptx you are creating, or one you have loaded which you have verified contains /ppt/slideLayouts/slideLayout2.xml?

Re: Creating new slide layouts

PostPosted: Wed Apr 20, 2011 3:42 am
by alunsford3
Yes, sorry. I am creating the entire pptx from scratch, unless you recommend using an empty one as a starter template. I am however using slideLayout1.xml, slideLayout2.xml, and slideLayout4.xml that I took from a generic pptx file in order to create the layouts. As I said, it works when creating slideLayout1.xml but not the other two.

Re: Creating new slide layouts

PostPosted: Thu Apr 21, 2011 1:59 am
by jason
Code: Select all
tempLayout = (SlideLayoutPart)presMLPackage.getParts().getParts().get(pn);


won't create a part which doesn't already exist.

If you haven't added it already, you need to do so.

PresentationMLPackage.createPackage() contains:

Code: Select all
         SlideLayoutPart layoutPart = new SlideLayoutPart();
         layoutPart.setJaxbElement( SlideLayoutPart.createSldLayout() );

You can adapt the second line of that...

You should also add it to a SlideMasterPart and vice versa

Code: Select all
         masterPart.addSlideLayoutIdListEntry(layoutPart);         
         layoutPart.addTargetPart(masterPart);


For ease of reference, here is the complete code of

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        public static PresentationMLPackage createPackage() throws InvalidFormatException {
                               
                // Create a package
                PresentationMLPackage pmlPack = new PresentationMLPackage();

                // Presentation part
                MainPresentationPart pp;
                try {
                       
                        pp = new MainPresentationPart();
                        pp.setJaxbElement(MainPresentationPart.createJaxbPresentationElement() );
                        pmlPack.addTargetPart(pp);             
                                               
                        // Slide layout part
                        SlideLayoutPart layoutPart = new SlideLayoutPart();
                        layoutPart.setJaxbElement( SlideLayoutPart.createSldLayout() );
                       
                        // Slide Master part
                        SlideMasterPart masterPart = new SlideMasterPart();
                        pp.addSlideMasterIdListEntry(masterPart);

                        masterPart.setJaxbElement(masterPart.createSldMaster() );
                        masterPart.addSlideLayoutIdListEntry(layoutPart);
                       
                        layoutPart.addTargetPart(masterPart);
                       
                        // Theme part
                        ThemePart themePart = new ThemePart(new PartName("/ppt/theme/theme1.xml"));
                        java.io.InputStream is = org.docx4j.utils.ResourceUtils.getResource(
                                                "org/docx4j/openpackaging/parts/PresentationML/theme.xml");
                        themePart.unmarshal(is);
                       
                        // .. add it in 2 places ..
                        masterPart.addTargetPart(themePart);
                        pp.addTargetPart(themePart);                   
                       
                } catch (Exception e) {
                        e.printStackTrace();
                        throw new InvalidFormatException("Couldn't create package", e);
                }
               
                // Return the new package
                return pmlPack;        
        }

 
Parsed in 0.018 seconds, using GeSHi 1.0.8.4


see http://207.46.16.248/en-us/library/gg278335.aspx re structure of pptx, or the images in http://www.freshpatents.com/Structuring ... 277452.php

You can also run your sample pptx through org.docx4j.samples.PartsList to get a feel for its structure.

Re: Creating new slide layouts

PostPosted: Thu Apr 21, 2011 3:29 pm
by alunsford3
Thanks! I've gotten it straightened out now. All the layouts I want are being imported. I was never clear on how the first layout was 'just working', I didn't know that it was created by PresentationMLPackage in createPackage(). I do have one follow up question though:

How does the bulleting / numbering system work in powerpoint?

I am using the same slideLayout2.xml that was working in Powerpoint and yet I can't get anything to be bulleted or numbered. When I was using the docx4j to create .docx files I figured out you had to set the NumberingDefinitionsPart, but I don't see anything like that in pptx4j. I have tried setting the a:pPr lvl's, but they only indent and do not bullet or number. I noticed that Powerpoint does a lot of thing on its own and does not store them in the layout files, such as the positions of the Title and Content boxes on the Title & Content slideLayout2.xml. I had to do a lot of research to figure out that it was because they were missing the xfrm tag and to set it to what I wanted.

Re: Creating new slide layouts

PostPosted: Tue Nov 26, 2013 6:07 am
by rickgarcia
I have a question regarding this same topic. I looked at that code, and it relies on having a SlideMasterPart around. How can I lookup this Slide master part after it's been generated? I don't want to have to create new Slide Master Parts every time I want to add new layouts. alunsford3, how did you add the new layotus exactly?