Page 1 of 1

To add an chart to document

PostPosted: Wed Apr 04, 2012 11:32 pm
by SmithaRSanthosh
I tried to add a chart to a docx but I cannot see the chart object when I check the document.xml file. The chart object is not displayed on the docx also. this is my code

Syntax: [ Download ] [ Hide ]
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);


        }
 
Parsed in 0.026 seconds, using GeSHi 1.0.8.4



My document.xml has only :
Syntax: [ Download ] [ Hide ]
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>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


after the xmlns decl

Re: To add an object to a file

PostPosted: Wed Apr 11, 2012 11:43 am
by jason
You aren't adding your CTChart chart to your document.

In your document.xml, you need something like:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
    <w:p w:rsidR="003B5C14" w:rsidRDefault="00ED0C36">
      <w:bookmarkStart w:id="0" w:name="_GoBack"/>
      <w:r>
        <w:rPr>
          <w:noProof/>
        </w:rPr>
        <w:drawing>
          <wp:inline distT="0" distB="0" distL="0" distR="0">
            <wp:extent cx="5486400" cy="3200400"/>
            <wp:effectExtent l="0" t="0" r="19050" b="19050"/>
            <wp:docPr id="1" name="Chart 1"/>
            <wp:cNvGraphicFramePr/>
            <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
              <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/chart">
                <c:chart xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId5"/>
              </a:graphicData>
            </a:graphic>
          </wp:inline>
        </w:drawing>
      </w:r>
      <w:bookmarkEnd w:id="0"/>
    </w:p>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Note the c:chart goes in a:graphicData, which is in turn nested in other elements. You need to create that structure, then add it using run.getContent().add.

Also, when you add the xlsx, add it using addTargetPart, not getParts().getParts().put. Ordinarily, you'd expect to add the relId somewhere in the source part (in this case, the chart, but if memory serves, that isn't done in this case).

Re: To add an chart to document

PostPosted: Tue Feb 24, 2015 12:29 am
by sekhar-rangam
Programmatically creating MS Office-compatible charts

If you have a web application that has chart visualizations and customer requests a downloadable copy of the report that can be viewed in PowerPoint, How do you go about it?

see more at: http://bridgei2i.com/blog/programmatica ... le-charts/