Page 1 of 1

Microsoft Charts with docx4j

PostPosted: Tue Mar 20, 2012 7:52 pm
by SmithaRSanthosh
Is it possible for me to create Microsoft chart objects with docx4j. As per my requirement I need to create different charts in word and ppt.

Re: Microsoft Charts with docx4j

PostPosted: Wed Mar 21, 2012 10:16 pm
by sureshbabubv
Hi Santosh,

Please check the docx4jexamples or docx4jsamples in the docx4j bundle, there are some examples available on the same.

Thanks & Regards,
B.V.Suresh Babu.

Re: Microsoft Charts with docx4j

PostPosted: Wed Mar 21, 2012 11:37 pm
by SmithaRSanthosh
Thank You. I will check them out.

Re: Microsoft Charts with docx4j

PostPosted: Thu Mar 22, 2012 1:19 pm
by jason
There are also some recent posts in the pptx4j forum relating to charts, which may be of interest.

Re: Microsoft Charts with docx4j

PostPosted: Fri Mar 23, 2012 11:39 pm
by SmithaRSanthosh
Hi , I tried creating a chart with the following code :
org.docx4j.openpackaging.parts.DrawingML.Chart chartPart
= new org.docx4j.openpackaging.parts.DrawingML.Chart(new PartName("/word/charts/chart1.xml"));
wordMLPackage.getMainDocumentPart().addTargetPart(chartPart);
chartPart.setContentType(new org.docx4j.openpackaging.contenttype.ContentType(
org.docx4j.openpackaging.contenttype.ContentTypes.DRAWINGML_CHART));

wordMLPackage.getMainDocumentPart().addTargetPart(chartPart);

and it was throwing the below exception. Can you help me in this regard?

4500 [main] INFO org.docx4j.openpackaging.parts.JaxbXmlPart - marshalling com.Chart
java.lang.IllegalArgumentException: obj parameter must not be null
at javax.xml.bind.helpers.AbstractMarshallerImpl.checkNotNull(Unknown Source)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
at org.docx4j.openpackaging.parts.JaxbXmlPart.marshal(JaxbXmlPart.java:197)
at org.docx4j.openpackaging.parts.JaxbXmlPart.marshal(JaxbXmlPart.java:175)
at org.docx4j.openpackaging.io.SaveToZipFile.saveRawXmlPart(SaveToZipFile.java:245)
at org.docx4j.openpackaging.io.SaveToZipFile.saveRawXmlPart(SaveToZipFile.java:194)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:410)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:373)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:428)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:373)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:164)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:105)
at org.docx4j.openpackaging.packages.WordprocessingMLPackage.save(WordprocessingMLPackage.java:219)
at com.CreateWordprocessingMLDocument.main(CreateWordprocessingMLDocument.java:176)
4500 [main] ERROR org.docx4j.openpackaging.io.SaveToZipFile - java.lang.IllegalArgumentException: obj parameter must not be null
org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:378)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:164)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:105)
at org.docx4j.openpackaging.packages.WordprocessingMLPackage.save(WordprocessingMLPackage.java:219)
at com.CreateWordprocessingMLDocument.main(CreateWordprocessingMLDocument.java:176)

Re: Microsoft Charts with docx4j

PostPosted: Mon Mar 26, 2012 6:34 pm
by SmithaRSanthosh
THis is the code I used to generate the docx with a chart object.
public static void main(String[] args) throws Exception {


try {
getInputFilePath(args);
} catch (IllegalArgumentException e) {
inputfilepath = System.getProperty("user.dir") + "/CreateWordprocessingMLDocument_out1.docx";
}
System.out.println("Input File path is "+inputfilepath);
boolean save =
(inputfilepath == null ? false : true);

System.out.println( "Creating package..");
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();

wordMLPackage.getMainDocumentPart()
.addStyledParagraphOfText("Title", "Hello world");

wordMLPackage.getMainDocumentPart().addParagraphOfText("Adding a Paragraph - using docx4j!");

// To get bold text, you must set the run's rPr@w:b,
// so you can't use the createParagraphOfText convenience method

//org.docx4j.wml.P p = wordMLPackage.getMainDocumentPart().createParagraphOfText("text");

org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
org.docx4j.wml.P p = factory.createP();

org.docx4j.wml.Text t = factory.createText();
t.setValue("SMITHA R");

org.docx4j.wml.R run = factory.createR();
run.getContent().add(t);

p.getContent().add(run);


org.docx4j.wml.RPr rpr = factory.createRPr();
org.docx4j.wml.BooleanDefaultTrue b = new org.docx4j.wml.BooleanDefaultTrue();
b.setVal(true);
rpr.setB(b);

run.setRPr(rpr);
wordMLPackage.getMainDocumentPart().addObject(p);
// Optionally, set pPr/rPr@w:b
org.docx4j.wml.PPr ppr = factory.createPPr();
p.setPPr( ppr );
org.docx4j.wml.ParaRPr paraRpr = factory.createParaRPr();
ppr.setRPr(paraRpr);
rpr.setB(b);
org.docx4j.openpackaging.parts.DrawingML.Chart chartPart
= new org.docx4j.openpackaging.parts.DrawingML.Chart(
new PartName("/word/charts/chart1.xml"));
//wordMLPackage.getMainDocumentPart().addTargetPart(chartPart, RelationshipsPart.AddPartBehaviour.OVERWRITE_IF_NAME_EXISTS);
wordMLPackage.getMainDocumentPart().addTargetPart(chartPart);
// Now save it
if (save) {
System.out.println(""+inputfilepath);
try{
wordMLPackage.save(new java.io.File(inputfilepath) );
}catch (Exception e) {
System.out.println("Ex is "+e.getMessage()+" "+e.getCause()+" "+e.getLocalizedMessage());
e.printStackTrace();
}
System.out.println("Saved " + inputfilepath);
} else {
// Create a org.docx4j.wml.Package object
FlatOpcXmlCreator worker = new FlatOpcXmlCreator(wordMLPackage);
org.docx4j.xmlPackage.Package pkg = worker.get();

// Now marshall it
JAXBContext jc = Context.jcXmlPackage;
Marshaller marshaller=jc.createMarshaller();

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
NamespacePrefixMapperUtils.setProperty(marshaller,
NamespacePrefixMapperUtils.getPrefixMapper());
System.out.println( "\n\n OUTPUT " );
System.out.println( "====== \n\n " );
marshaller.marshal(pkg, System.out);

}

System.out.println("Done.");

}
I can generate the docx when I remove the lines that add the chart object. But when I add the chart it gives me the exception:
java.lang.IllegalArgumentException: obj parameter must not be null
at javax.xml.bind.helpers.AbstractMarshallerImpl.checkNotNull(Unknown Source)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
at org.docx4j.openpackaging.parts.JaxbXmlPart.marshal(JaxbXmlPart.java:197)
at org.docx4j.openpackaging.parts.JaxbXmlPart.marshal(JaxbXmlPart.java:175)
at org.docx4j.openpackaging.io.SaveToZipFile.saveRawXmlPart(SaveToZipFile.java:245)
at org.docx4j.openpackaging.io.SaveToZipFile.saveRawXmlPart(SaveToZipFile.java:194)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:410)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:373)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:428)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:373)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:164)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:105)
at org.docx4j.openpackaging.packages.WordprocessingMLPackage.save(WordprocessingMLPackage.java:219)
at com.CreateWordprocessingMLDocument.main(CreateWordprocessingMLDocument.java:116)
org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:378)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:164)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:105)
at org.docx4j.openpackaging.packages.WordprocessingMLPackage.save(WordprocessingMLPackage.java:219)
at com.CreateWordprocessingMLDocument.main(CreateWordprocessingMLDocument.java:116)
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:378)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:428)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:373)
... 4 more
6531 [main] ERROR org.docx4j.openpackaging.io.SaveToZipFile - java.lang.IllegalArgumentException: obj parameter must not be null
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Problem saving part word/charts/chart1.xml
at org.docx4j.openpackaging.io.SaveToZipFile.saveRawXmlPart(SaveToZipFile.java:303)
at org.docx4j.openpackaging.io.SaveToZipFile.saveRawXmlPart(SaveToZipFile.java:194)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:410)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:373)
... 6 more
Caused by: java.lang.IllegalArgumentException: obj parameter must not be null
at javax.xml.bind.helpers.AbstractMarshallerImpl.checkNotNull(Unknown Source)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
at org.docx4j.openpackaging.parts.JaxbXmlPart.marshal(JaxbXmlPart.java:197)
at org.docx4j.openpackaging.parts.JaxbXmlPart.marshal(JaxbXmlPart.java:175)
at org.docx4j.openpackaging.io.SaveToZipFile.saveRawXmlPart(SaveToZipFile.java:245)
... 9 more
org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:378)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:164)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:105)
at org.docx4j.openpackaging.packages.WordprocessingMLPackage.save(WordprocessingMLPackage.java:219)
at com.CreateWordprocessingMLDocument.main(CreateWordprocessingMLDocument.java:116)
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:378)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:428)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:373)
... 4 more
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Problem saving part word/charts/chart1.xml

Can you help me resolve the issue?

Re: Microsoft Charts with docx4j

PostPosted: Mon Mar 26, 2012 9:25 pm
by jason
I haven't looked at your code in any detail, but it looks like you aren't adding any content to your chart part. So you get an error from JAXB complaining that you are marshalling null content. You'll need to create the chart content .. any content ought to be enough to satisfy JAXB, but populating it with something Word can render you may find to be a non-trivial exercise! (Create a chart in Word, save the file, unzip it and inspect the contents to see what you need. This will also show you the structure you need in your main document part to properly include the chart).

Re: Microsoft Charts with docx4j

PostPosted: Tue Mar 27, 2012 8:07 pm
by SmithaRSanthosh
Thank You. Can you provide me an example of how I can add a content to the chartpart. My open office / word installations do not work as expected and I am not able to generate xml files.

Re: Microsoft Charts with docx4j

PostPosted: Tue Mar 27, 2012 11:42 pm
by SmithaRSanthosh
HI , I thank you for the reply and the answers.

I still cannot understand the concepts involved while creating charts. I was able to save it , unzip and see the contents. While doing that , I can see an embedded xlsx which corresponds to the chart. I am not sure how that can be converted to code. Again , can you give me the method for adding a content to the chart part.

I also saw other examples using the graphics object. But I am not sure how I can add it back to the file.
Graphic g = new Graphic();
GraphicData gd = new GraphicData();
gd.setUri("http://schemas.openxmlformats.org/drawingml/2006/chart");
org.docx4j.dml.chart.ObjectFactory factory1 = new org.docx4j.dml.chart.ObjectFactory();
CTRelId ctrelid = new CTRelId();
ctrelid.setId("rId2");
JAXBElement<CTRelId> e = factory1.createChart(ctrelid);
gd.getAny().add(e);
g.setGraphicData(gd);

How can I add this graphic element to the wordMLPackage , i tried some options and get different errors and I do not think I am on the right track. Can you provide me help in this regard.

Re: Microsoft Charts with docx4j

PostPosted: Wed Mar 28, 2012 8:55 pm
by jason
The easiest way to create a chart part would be to use Excel/Word to create one of the type you are interested in, and modify that. I don't think you need the embedded xlsx (but you should check this in your target consuming application).

A more general solution would be to modify jfreechart to output DrawingML.

The easiest way out may be to use jfreechart to create your chart as an image, and include that. See http://stackoverflow.com/questions/3354 ... apache-poi for further discussion.

Re: Microsoft Charts with docx4j

PostPosted: Wed Mar 28, 2012 9:09 pm
by SmithaRSanthosh
Thank You. I tried creating the document as I wanted but there was an embedded xlsx in the file . I cannot use images , my requirement wants me to create Chart objects instead.
How can I add content to a chart , for now I need to generate a simple chart for POC purposes and have to get this working.

Can you help me with it.

Re: Microsoft Charts with docx4j

PostPosted: Wed Mar 28, 2012 10:38 pm
by jason
SmithaRSanthosh wrote: my requirement wants me to create Chart objects instead.
How can I add content to a chart , for now I need to generate a simple chart for POC purposes and have to get this working.


If that's the case, then, as per my earlier post, use Excel/Word to create one of the type you are interested in, and modify that. It should be obvious what you need to do, for example, to make a bar in a bar chart twice the height.

Re: Microsoft Charts with docx4j

PostPosted: Wed Mar 28, 2012 11:19 pm
by SmithaRSanthosh
I did try to create the document manually and get it working using the xml , and I am facing problems with the file being corrupt. I have posted a question on this .

If I need to get it done using the Chart object, how do I add content to it. I could not get a method addContent to do this. What should be added to the chart part. I am missing that concept.

I am creating a chart object. And I am adding it to the document part. Can you give me some code snippet of how I can add a content to this chart object and the type of the content.

Code: Select all
org.docx4j.openpackaging.parts.DrawingML.Chart chartPart
                    = new org.docx4j.openpackaging.parts.DrawingML.Chart(
                          new PartName("/word/charts/chart1.xml"));

Re: Microsoft Charts with docx4j

PostPosted: Thu Apr 19, 2012 8:55 am
by conphident4
Hi,
this may be the problem causing statement.
new PartName("/word/charts/chart1.xml")

docx4j adds the rel ID itself - proposedRelID field. try removing that & see if it works