Object theChart = presentationMLPackage
.getParts().getParts()
.get(new PartName("/ppt/charts/chart1.xml"));
However, after this line, theChart is null.
Here is the full code:
- Code: Select all
package com.ama.pptgen;
import org.docx4j.openpackaging.packages.PresentationMLPackage;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.PresentationML.MainPresentationPart;
import org.docx4j.openpackaging.parts.PresentationML.SlideLayoutPart;
import org.docx4j.openpackaging.parts.PresentationML.SlidePart;
public class PptGenerator
{
public static void main(String[] args)
{
try
{
make();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void make() throws Exception
{
// Where will we save our new .ppxt?
String outputfilepath = System.getProperty("user.dir")
+ "/ppt_chart_test3.pptx";
// Create skeletal package
PresentationMLPackage presentationMLPackage = PresentationMLPackage
.createPackage();
// Create and add a new slidepart to the presentation package
// First get Main Presentation Part and add new slide to it
MainPresentationPart mpPart = (MainPresentationPart) presentationMLPackage
.getParts().getParts()
.get(new PartName("/ppt/presentation.xml"));
SlidePart slidePart = new SlidePart(new PartName(
"/ppt/slides/slide1.xml"));
mpPart.addSlideIdListEntry(slidePart);
slidePart.setJaxbElement(SlidePart.createSld());
// Get and set slide layout part target
SlideLayoutPart layoutPart = (SlideLayoutPart) presentationMLPackage
.getParts().getParts()
.get(new PartName("/ppt/slideLayouts/slideLayout1.xml"));
slidePart.addTargetPart(layoutPart);
//add chart
Object theChart = presentationMLPackage
.getParts().getParts()
.get(new PartName("/ppt/charts/chart1.xml"));
System.out.println("theChart is of type " + theChart.getClass().toString());
// All done: save it
presentationMLPackage.save(new java.io.File(outputfilepath));
System.out.println("\n\n done .. saved " + outputfilepath);
}
}
If you run it, you should get a null pointer exception. Is there a work-around?