Hi jason. Thx to your support, now i've managed to add the table.
I conclude all the experiences I got here.
1. with the unmarshal method, the reason that why I could not get result is:
(1) I ignored defining the namespace.
2. with the create object method, i found some element cannot be missing to fulfill the whole structure.
I past the basic structure to create table below, and I create 20 cell here:
- Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<p:sld xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<p:cSld>
<p:spTree>
<p:nvGrpSpPr>
<p:cNvPr name="" id="1"/>
<p:cNvGrpSpPr/>
<p:nvPr/>
</p:nvGrpSpPr>
<p:grpSpPr>
<a:xfrm>
<a:off y="0" x="0"/>
<a:ext cy="0" cx="0"/>
<a:chOff y="0" x="0"/>
<a:chExt cy="0" cx="0"/>
</a:xfrm>
</p:grpSpPr>
<p:graphicFrame>
<p:nvGraphicFramePr>
<p:cNvPr name="1" id="0"/>
<p:cNvGraphicFramePr>
<a:graphicFrameLocks noGrp="true"/>
</p:cNvGraphicFramePr>
<p:nvPr/>
</p:nvGraphicFramePr>
<p:xfrm>
<a:off y="0" x="0"/>
<a:ext cy="0" cx="0"/>
</p:xfrm>
<a:graphic>
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/table">
<a:tbl>
<a:tblGrid>
<a:gridCol w="300000"/>
<a:gridCol w="300000"/>
</a:tblGrid>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
<a:tr h="0">
<a:tc/>
<a:tc/>
</a:tr>
</a:tbl>
</a:graphicData>
</a:graphic>
</p:graphicFrame>
</p:spTree>
</p:cSld>
</p:sld>
the corresponding code is:
- Code: Select all
protected static Logger log = Logger.getLogger(pptHelloWorld.class);
@SuppressWarnings( { "static-access", "unchecked" })
public static void main(String[] args) throws Exception {
String outputfilepath = System.getProperty("user.dir")
+ "/pptx-out.pptx";
PresentationMLPackage presentationMLPackage = PresentationMLPackage
.createPackage();
// Need references to these parts to create a slide
// Please note that these parts *already exist* - they are
// created by createPackage() above. See that method
// for instruction on how to create and add a part.
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 = presentationMLPackage.createSlidePart(pp,
layoutPart, new PartName("/ppt/slides/slide1.xml"));
SlidePart slidePart2 = presentationMLPackage.createSlidePart(pp,
layoutPart, new PartName("/ppt/slides/slide2.xml"));
//instatiation the factory for later object creation.
ObjectFactory objectFactory = new ObjectFactory();
org.pptx4j.pml.ObjectFactory graphicObjectFactory = new org.pptx4j.pml.ObjectFactory();
//Node Creation
CTGraphicalObjectFrame graphicFrame = graphicObjectFactory
.createCTGraphicalObjectFrame();
org.pptx4j.pml.CTGraphicalObjectFrameNonVisual nvGraphicFramePr=graphicObjectFactory.createCTGraphicalObjectFrameNonVisual();
org.docx4j.dml.CTNonVisualDrawingProps cNvPr=objectFactory.createCTNonVisualDrawingProps();
org.docx4j.dml.CTNonVisualGraphicFrameProperties cNvGraphicFramePr=objectFactory.createCTNonVisualGraphicFrameProperties();
org.docx4j.dml.CTGraphicalObjectFrameLocking graphicFrameLocks=new org.docx4j.dml.CTGraphicalObjectFrameLocking();
org.docx4j.dml.CTTransform2D xfrm=objectFactory.createCTTransform2D();
Graphic graphic = objectFactory.createGraphic();
GraphicData graphicData = objectFactory.createGraphicData();
CTTable ctTable = objectFactory.createCTTable();
JAXBElement<CTTable> tbl = objectFactory.createTbl(ctTable);
CTTableGrid ctTableGrid = objectFactory.createCTTableGrid();
CTTableCol gridCol = objectFactory.createCTTableCol();
CTTableRow ctTableRow = objectFactory.createCTTableRow();
CTTableCell ctTableCell = objectFactory.createCTTableCell();
//Build the parent-child relationship of this slides.xml
graphicFrame.setNvGraphicFramePr(nvGraphicFramePr);
nvGraphicFramePr.setCNvPr(cNvPr);
cNvPr.setName("1");
nvGraphicFramePr.setCNvGraphicFramePr(cNvGraphicFramePr);
cNvGraphicFramePr.setGraphicFrameLocks(graphicFrameLocks);
graphicFrameLocks.setNoGrp(true);
nvGraphicFramePr.setNvPr(graphicObjectFactory.createNvPr());
graphicFrame.setXfrm(xfrm);
xfrm.setExt(objectFactory.createCTPositiveSize2D());
xfrm.setOff(objectFactory.createCTPoint2D());
graphicFrame.setGraphic(graphic);
graphic.setGraphicData(graphicData);
graphicData.setUri("http://schemas.openxmlformats.org/drawingml/2006/table");
graphicData.getAny().add(tbl);
ctTable.setTblGrid(ctTableGrid);
ctTableGrid.getGridCol().add(gridCol);
ctTableGrid.getGridCol().add(gridCol);
gridCol.setW(300000);
ctTableRow.getTc().add(ctTableCell);
ctTableRow.getTc().add(ctTableCell);
for(int i=0;i<20;i++){ctTable.getTr().add(ctTableRow);}
note: the for node <p:cNvPr>, the name attri will not added with default value when create the node. However, this attribute could not be missed.
In the same time, the width attri of <a:gridCol> should not be set as default value "0" or small value. It will also cause error when open document.
Basically, these are my experiences. Hope it will make help to pptx4j:)
One last question, jason, do u know the method to get the width of one slide?