Thanks for writing pptx4j! It's great to be able to create full-featured powerpoint presentations from Java!
I'm trying to create a presentation with a chart on it. However, I am getting the following exception:
javax.xml.bind.MarshalException
- with linked exception:
[javax.xml.bind.JAXBException: class org.docx4j.dml.chartDrawing.CTGraphicFrame nor any of its super class is known to this context.]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
at org.docx4j.openpackaging.parts.JaxbXmlPart.marshal(JaxbXmlPart.java:177)
at org.docx4j.openpackaging.parts.JaxbXmlPart.marshal(JaxbXmlPart.java:154)
at org.docx4j.openpackaging.io.SaveToZipFile.saveRawXmlPart(SaveToZipFile.java:200)
at org.docx4j.openpackaging.io.SaveToZipFile.saveRawXmlPart(SaveToZipFile.java:188)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:362)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:325)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:377)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:325)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:158)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:99)
at org.docx4j.openpackaging.packages.OpcPackage.save(OpcPackage.java:223)
at ChartTest.make(ChartTest.java:61)
at ChartTest.main(ChartTest.java:28)
Here's the code:
- Code: Select all
import java.util.List;
import javax.xml.bind.JAXBException;
import org.docx4j.dml.CTNonVisualDrawingProps;
import org.docx4j.dml.CTNonVisualGraphicFrameProperties;
import org.docx4j.dml.CTPoint2D;
import org.docx4j.dml.CTPositiveSize2D;
import org.docx4j.dml.CTTransform2D;
import org.docx4j.dml.chartDrawing.CTGraphicFrame;
import org.docx4j.dml.chartDrawing.CTGraphicFrameNonVisual;
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;
import org.pptx4j.pml.CTExtension;
import org.pptx4j.pml.CTExtensionListModify;
public class ChartTest
{
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_test1.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);
// Last add the shapes to the slide
addShapesToSlide( slidePart);
// All done: save it
presentationMLPackage.save(new java.io.File(outputfilepath));
System.out.println("\n\n done .. saved " + outputfilepath);
}
private static void addShapesToSlide(SlidePart slidePart) throws JAXBException
{
List<Object> listOfSpGrpSpEtc = slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame();
CTGraphicFrame chartFrame = getCTGraphicFrame("Chart 1", 1524000, 1397000);
listOfSpGrpSpEtc.add(chartFrame);
slidePart.getJaxbElement().getCSld().getSpTree().setExtLst(new CTExtensionListModify());
CTExtension ctExtension = new CTExtension();
ctExtension.setUri("{D42A27DB-BD31-4B8C-83A1-F6EECF244321}");
slidePart.getJaxbElement().getCSld().getSpTree().getExtLst().getExt().add(ctExtension);
}
private static CTGraphicFrame getCTGraphicFrame(String name, int x, int y)
{
CTGraphicFrame graphicFrame = new CTGraphicFrame();
graphicFrame.setNvGraphicFramePr(new CTGraphicFrameNonVisual());
graphicFrame.getNvGraphicFramePr().setCNvPr(new CTNonVisualDrawingProps());
graphicFrame.getNvGraphicFramePr().getCNvPr().setName(name);
graphicFrame.getNvGraphicFramePr().setCNvGraphicFramePr(new CTNonVisualGraphicFrameProperties());
CTTransform2D ctTransform2D = new CTTransform2D();
CTPoint2D ctPoint2D = new CTPoint2D();
ctPoint2D.setX(x);
ctPoint2D.setY(y);
ctTransform2D.setOff(ctPoint2D);
CTPositiveSize2D ctPositiveSize2D = new CTPositiveSize2D();
ctPositiveSize2D.setCx(6096000);
ctPositiveSize2D.setCy(4064000);
ctTransform2D.setExt(ctPositiveSize2D);
graphicFrame.setXfrm(ctTransform2D);
return graphicFrame;
}
}
Any ideas?