Using java Syntax Highlighting
public static void main(String[] args) throws InvalidFormatException, FileNotFoundException {
System.out.println( "Creating package..");
inputfilepath = System.getProperty("user.dir") + "\\CreateWord3.docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
wordMLPackage.getMainDocumentPart()
.addStyledParagraphOfText("Title", "Hello world");
wordMLPackage.getMainDocumentPart().addParagraphOfText("Adding a Chart - using docx4j!");
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 Rajasenan");
org.docx4j.wml.R run = factory.createR();
run.getContent().add(t);
p.getContent().add(run);
Chart chartPart = new Chart(new PartName("/word/charts/chart1.xml"));
//Chart chartPart = (Chart)pMLPackage.getParts().get(new PartName("/word/charts/chart1.xml"));
System.out.println("The chart Part is "+chartPart);
CTChartSpace ch = new CTChartSpace();
CTBoolean bool = new CTBoolean();
bool.setVal(true);
ch.setDate1904(bool);
CTTextLanguageID lang = new CTTextLanguageID();
lang.setVal("en-US");
ch.setLang(lang);
System.out.println(ch +" CH IS ");
CTChart chart = new CTChart();
CTPlotArea ctp = new CTPlotArea();
//List<Object> objs = new Vector<Object>();
CTBarChart objBar = new CTBarChart();
CTBarDir barDir = new CTBarDir();
barDir.setVal(STBarDir.COL);
objBar.setBarDir(barDir);
CTBarGrouping ctbarGr = new CTBarGrouping();
ctbarGr.setVal(STBarGrouping.CLUSTERED);
objBar.setGrouping(ctbarGr);
CTBarSer barSer = new CTBarSer();
CTUnsignedInt unsign = new CTUnsignedInt();
unsign.setVal(0);
barSer.setOrder(unsign);
CTSerTx ctSerTx = new CTSerTx();
CTStrRef ctSerRef = new CTStrRef();
ctSerRef.setF("Sheet1!$B$1");
CTStrData ctStrData = new CTStrData();
CTUnsignedInt unsign1 = new CTUnsignedInt();
unsign1.setVal(1);
ctStrData.setPtCount(unsign1);
CTStrVal ctStrVal = new CTStrVal();
ctStrVal.setIdx(0);
ctStrVal.setV("Series 1");
ctStrData.getPt().add(ctStrVal);
ctSerRef.setStrCache(ctStrData);
ctSerTx.setStrRef(ctSerRef);
barSer.setTx(ctSerTx);
CTAxDataSource cat = new CTAxDataSource();
CTStrRef strRef = new CTStrRef();
strRef .setF("Sheet1!$A$2:$A$5");
CTStrData strCache = new CTStrData();
CTUnsignedInt unsign4 = new CTUnsignedInt();
unsign4.setVal(4);
strCache.setPtCount(unsign4);
CTStrVal pt1 = new CTStrVal();
pt1.setIdx(0);
pt1.setV("Category 1");
CTStrVal pt2 = new CTStrVal();
pt2.setIdx(1);
pt2.setV("Category 2");
CTStrVal pt3 = new CTStrVal();
pt3.setIdx(2);
pt3.setV("Category 3");
CTStrVal pt4 = new CTStrVal();
pt4.setIdx(4);
pt4.setV("Category 4");
strCache.setPtCount(unsign4);
strCache.getPt().add(pt1);
strCache.getPt().add(pt2);
strCache.getPt().add(pt3);
strCache.getPt().add(pt4);
strRef.setStrCache(strCache);
cat.setStrRef(strRef);
barSer.setCat(cat);
CTNumVal val = new CTNumVal();
val.setIdx(0);
val.setV("12");
CTNumVal val1 = new CTNumVal();
val1.setIdx(1);
val1.setV("14");
CTNumData numData = new CTNumData();
numData.getPt().add(val);
numData.getPt().add(val1);
numData.setFormatCode("General");
CTNumRef numRef = new CTNumRef();
numRef.setNumCache(numData);
numRef.setF("Sheet1!$B$2:$B$5");
CTNumDataSource vals = new CTNumDataSource();
vals.setNumRef(numRef);
barSer.setVal(vals);
objBar.getSer().add(barSer);
System.out.println("The value od the string f is "+numRef.getF());
//objs.add(objBar);
ctp.getAreaChartOrArea3DChartOrLineChart().add(objBar);
chart.setPlotArea(ctp);
ch.setChart(chart);
chartPart.setJaxbElement(ch);
wordMLPackage.getMainDocumentPart().addTargetPart(chartPart);
EmbeddedPackagePart embeded = new EmbeddedPackagePart(new PartName("/word/embeddings/Microsoft_Office_Excel_Worksheet1.xlsx"));
//embeded.setBinaryData(getBytesFromFile(new File("C:/Sprint1-Templates/embeded.xlsx")));
embeded.setBinaryData(new FileInputStream(System.getProperty("user.dir") + "/Files/embeddings/Microsoft_Office_Excel_Worksheet1.xlsx"));
wordMLPackage.getParts().getParts().put(new PartName("/word/embeddings/Microsoft_Office_Excel_Worksheet1.xlsx"), embeded);
try{
wordMLPackage.save(new java.io.File(inputfilepath) );
}catch (Exception e1) {
System.out.println("Ex is "+e1.getMessage()+" "+e1.getCause()+" "+e1.getLocalizedMessage());
e1.printStackTrace();
}
System.out.println("Saved " + inputfilepath);
}
System.out.println( "Creating package..");
inputfilepath = System.getProperty("user.dir") + "\\CreateWord3.docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
wordMLPackage.getMainDocumentPart()
.addStyledParagraphOfText("Title", "Hello world");
wordMLPackage.getMainDocumentPart().addParagraphOfText("Adding a Chart - using docx4j!");
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 Rajasenan");
org.docx4j.wml.R run = factory.createR();
run.getContent().add(t);
p.getContent().add(run);
Chart chartPart = new Chart(new PartName("/word/charts/chart1.xml"));
//Chart chartPart = (Chart)pMLPackage.getParts().get(new PartName("/word/charts/chart1.xml"));
System.out.println("The chart Part is "+chartPart);
CTChartSpace ch = new CTChartSpace();
CTBoolean bool = new CTBoolean();
bool.setVal(true);
ch.setDate1904(bool);
CTTextLanguageID lang = new CTTextLanguageID();
lang.setVal("en-US");
ch.setLang(lang);
System.out.println(ch +" CH IS ");
CTChart chart = new CTChart();
CTPlotArea ctp = new CTPlotArea();
//List<Object> objs = new Vector<Object>();
CTBarChart objBar = new CTBarChart();
CTBarDir barDir = new CTBarDir();
barDir.setVal(STBarDir.COL);
objBar.setBarDir(barDir);
CTBarGrouping ctbarGr = new CTBarGrouping();
ctbarGr.setVal(STBarGrouping.CLUSTERED);
objBar.setGrouping(ctbarGr);
CTBarSer barSer = new CTBarSer();
CTUnsignedInt unsign = new CTUnsignedInt();
unsign.setVal(0);
barSer.setOrder(unsign);
CTSerTx ctSerTx = new CTSerTx();
CTStrRef ctSerRef = new CTStrRef();
ctSerRef.setF("Sheet1!$B$1");
CTStrData ctStrData = new CTStrData();
CTUnsignedInt unsign1 = new CTUnsignedInt();
unsign1.setVal(1);
ctStrData.setPtCount(unsign1);
CTStrVal ctStrVal = new CTStrVal();
ctStrVal.setIdx(0);
ctStrVal.setV("Series 1");
ctStrData.getPt().add(ctStrVal);
ctSerRef.setStrCache(ctStrData);
ctSerTx.setStrRef(ctSerRef);
barSer.setTx(ctSerTx);
CTAxDataSource cat = new CTAxDataSource();
CTStrRef strRef = new CTStrRef();
strRef .setF("Sheet1!$A$2:$A$5");
CTStrData strCache = new CTStrData();
CTUnsignedInt unsign4 = new CTUnsignedInt();
unsign4.setVal(4);
strCache.setPtCount(unsign4);
CTStrVal pt1 = new CTStrVal();
pt1.setIdx(0);
pt1.setV("Category 1");
CTStrVal pt2 = new CTStrVal();
pt2.setIdx(1);
pt2.setV("Category 2");
CTStrVal pt3 = new CTStrVal();
pt3.setIdx(2);
pt3.setV("Category 3");
CTStrVal pt4 = new CTStrVal();
pt4.setIdx(4);
pt4.setV("Category 4");
strCache.setPtCount(unsign4);
strCache.getPt().add(pt1);
strCache.getPt().add(pt2);
strCache.getPt().add(pt3);
strCache.getPt().add(pt4);
strRef.setStrCache(strCache);
cat.setStrRef(strRef);
barSer.setCat(cat);
CTNumVal val = new CTNumVal();
val.setIdx(0);
val.setV("12");
CTNumVal val1 = new CTNumVal();
val1.setIdx(1);
val1.setV("14");
CTNumData numData = new CTNumData();
numData.getPt().add(val);
numData.getPt().add(val1);
numData.setFormatCode("General");
CTNumRef numRef = new CTNumRef();
numRef.setNumCache(numData);
numRef.setF("Sheet1!$B$2:$B$5");
CTNumDataSource vals = new CTNumDataSource();
vals.setNumRef(numRef);
barSer.setVal(vals);
objBar.getSer().add(barSer);
System.out.println("The value od the string f is "+numRef.getF());
//objs.add(objBar);
ctp.getAreaChartOrArea3DChartOrLineChart().add(objBar);
chart.setPlotArea(ctp);
ch.setChart(chart);
chartPart.setJaxbElement(ch);
wordMLPackage.getMainDocumentPart().addTargetPart(chartPart);
EmbeddedPackagePart embeded = new EmbeddedPackagePart(new PartName("/word/embeddings/Microsoft_Office_Excel_Worksheet1.xlsx"));
//embeded.setBinaryData(getBytesFromFile(new File("C:/Sprint1-Templates/embeded.xlsx")));
embeded.setBinaryData(new FileInputStream(System.getProperty("user.dir") + "/Files/embeddings/Microsoft_Office_Excel_Worksheet1.xlsx"));
wordMLPackage.getParts().getParts().put(new PartName("/word/embeddings/Microsoft_Office_Excel_Worksheet1.xlsx"), embeded);
try{
wordMLPackage.save(new java.io.File(inputfilepath) );
}catch (Exception e1) {
System.out.println("Ex is "+e1.getMessage()+" "+e1.getCause()+" "+e1.getLocalizedMessage());
e1.printStackTrace();
}
System.out.println("Saved " + inputfilepath);
}
Parsed in 0.024 seconds, using GeSHi 1.0.8.4
My document.xml has only :
Using xml Syntax Highlighting
<w:body>
- <w:p>
- <w:pPr>
<w:pStyle w:val="Title" />
</w:pPr>
- <w:r>
<w:t>Hello world</w:t>
</w:r>
</w:p>
- <w:p>
- <w:r>
<w:t>Adding a Chart - using docx4j!</w:t>
</w:r>
</w:p>
- <w:sectPr>
<w:pgSz w:code="9" w:h="16839" w:w="11907" />
<w:pgMar w:left="1440" w:bottom="1440" w:right="1440" w:top="1440" />
</w:sectPr>
</w:body>
</w:document>
- <w:p>
- <w:pPr>
<w:pStyle w:val="Title" />
</w:pPr>
- <w:r>
<w:t>Hello world</w:t>
</w:r>
</w:p>
- <w:p>
- <w:r>
<w:t>Adding a Chart - using docx4j!</w:t>
</w:r>
</w:p>
- <w:sectPr>
<w:pgSz w:code="9" w:h="16839" w:w="11907" />
<w:pgMar w:left="1440" w:bottom="1440" w:right="1440" w:top="1440" />
</w:sectPr>
</w:body>
</w:document>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
after the xmlns decl