2

PresentationMLPackage PresentationMLPackage = PresentationMLPackage.createPackage();

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

    PresentationPropertiesPart presProp = (PresentationPropertiesPart)presentationMLPackage.getParts().getParts().get(
            new PartName("/ppt/preseProps.xml"));

    // OK, now we can create a slide
    SlidePart slidePart = presentationMLPackage.createSlidePart(pp, layoutPart, 
            new PartName("/ppt/slides/slide1.xml"));

    // Create and add shape
    Shape sample = ((Shape)XmlUtils.unmarshalString(SAMPLE_SHAPE, Context.jcPML) );
    slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(sample);

    // All done: save it
    presentationMLPackage.save(new java.io.File(outputfilepath));

    System.out.println("\n\n done .. saved " + outputfilepath);

我使用上面的 CreateHelloworld.java 示例来创建一个 pptx 文档。我已经对其进行了修改,以便创建的 pptx 包具有 presProps.xml。但是当我执行上面的代码时它不会生成....任何帮助将不胜感激

4

1 回答 1

0

根据您复制/粘贴的代码顶部的注释,您的代码假定 PresentationPropertiesPart 已经存在。

它没有,所以你必须先创建它。以下应该可以解决问题:

PresentationPropertiesPart ppPart = new PresentationPropertiesPart();       
pp.addTargetPart(ppPart);
PresentationPr presProp = Context.getpmlObjectFactory().createPresentationPr();
ppPart.setJaxbElement(presProp);
于 2012-12-11T01:26:50.907 回答