Hi,
I am creating a pptx using dox4j and trying to create chart from it refering to below link
http://bridgei2i.com/blog/programmatica ... le-charts/However a blank pptx is getting generated.
Could someone please assist? I have gone through this post and tried the snippets here but no luck.
Here is my code. May be I am doing some minor mistake. Kindly help.
I am using the chart.xml from below link
https://github.com/sekhar-rangam/docx4j ... t_data.xmlpackage default;
import org.docx4j.XmlUtils;
import org.docx4j.dml.chart.CTChartSpace;
import org.docx4j.openpackaging.exceptions.Docx4JException;
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.Pptx4jException;
import org.pptx4j.jaxb.Context;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import java.io.*;
public class CreateChartDox4j {
public static void main(String args[]) throws IOException, JAXBException, Docx4JException, Pptx4jException {
String outputfilepath = System.getProperty("user.dir") + "/pptx-test1.pptx";
InputStream in = null;
BufferedReader br = null;
Reader fr =null;
String line = "";
PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage();
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);
int slideIndex =1;
org.docx4j.openpackaging.parts.DrawingML.Chart chartPart = new org.docx4j.openpackaging.parts.DrawingML.Chart(new PartName("/ppt/charts/chart" + slideIndex + ".xml"));
StringBuffer chartXMLBuffer = new StringBuffer();
String chartDataXmlFile = "/temp/chart.xml";
in = Docx4jChart.class.getResourceAsStream(chartDataXmlFile);
fr = new InputStreamReader(in, "utf-8");
br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
chartXMLBuffer.append(line);
chartXMLBuffer.append("");
}
//CTChartSpace chartSpace = (CTChartSpace) XmlUtils.unmarshalString(chartXMLBuffer.toString(), Context.jcPML,CTChartSpace.class);
CTChartSpace chartSpace = ((JAXBElement<CTChartSpace>) XmlUtils.unmarshalString(chartXMLBuffer.toString())).getValue();
chartPart.setJaxbElement(chartSpace );
slidePart.addTargetPart(chartPart);
presentationMLPackage.save(new java.io.File(outputfilepath));
System.out.println("Powerpoint Office document is created in the following path: "+outputfilepath);
System.out.println("\n\n done .. saved " + outputfilepath);
}
}