by SmithaRSanthosh » Mon Mar 26, 2012 6:34 pm
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?