I downloaded POI 3.8 Beta 5 today (in order to get bug fix 52209). I don't
know if this is fully supported or not in the Beta 5 release, but you still
don't appear to be able to instantiate a table correctly into a .pptx document
using the "small" ooxml schemas jar file. The code doesn't throw an exception
any more and allows you to create a table now. But, you can't add any rows to
it. The XML for the rows and everything below does not get persisted. I have
the following sample program:
import java.awt.Color;
import java.awt.Rectangle;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xslf.usermodel.TextAlign;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTable;
import org.apache.poi.xslf.usermodel.XSLFTableCell;
import org.apache.poi.xslf.usermodel.XSLFTableRow;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableCellProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.STTextAnchoringType;
import
org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
public class XSLFTableTest1 {
public static void main(String[] args) {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide1 = ppt.createSlide();
XSLFTable table = slide1.createTable();
table.setAnchor(new Rectangle(30, 120, 100, 100));
XSLFTableRow row = table.addRow();
XSLFTableCell cell = row.addCell();
row.setHeight(130);
cell.setBorderBottom(1.0);
cell.setBorderBottomColor(Color.BLACK);
cell.setBorderTop(1.0);
cell.setBorderTopColor(Color.BLACK);
cell.setBorderRight(1.0);
cell.setBorderRightColor(Color.BLACK);
cell.setBorderLeft(1.0);
cell.setBorderLeftColor(Color.BLACK);
cell.setFillColor(new Color(123, 154, 204));
CTTableCellProperties pr = cell.getXmlObject().getTcPr();
pr.setAnchor(STTextAnchoringType.CTR);
XSLFTextParagraph paragraph = cell.addNewTextParagraph();
XSLFTextRun run = paragraph.addNewTextRun();
run.setText("SampleText");
run.setFontFamily("Arial");
run.setFontSize(16);
run.setFontColor(Color.WHITE);
paragraph.setTextAlign(TextAlign.CENTER);
table.setColumnWidth(0, 85);
CTGraphicalObjectFrame frame = table.getXmlObject();
System.out.println(frame.toString());
try {
FileOutputStream out = new FileOutputStream(args[0]);
ppt.write(out);
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
This should generate a table with a single column, single row, and single
cell. Using the "small" ooxml schemas jar file, I end up with the following
XML code in ppt/slide1.xml:
<?xml version="1.0" encoding="UTF-8"?>
<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"
xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<p:cSld>
<p:spTree>
<p:nvGrpSpPr>
<p:cNvPr id="1" name=""/>
<p:cNvGrpSpPr/>
<p:nvPr/>
</p:nvGrpSpPr>
<p:grpSpPr>
<a:xfrm>
<a:off x="0" y="0"/>
<a:ext cx="0" cy="0"/>
<a:chOff x="0" y="0"/>
<a:chExt cx="0" cy="0"/>
</a:xfrm>
</p:grpSpPr>
<p:graphicFrame>
<p:nvGraphicFramePr>
<p:cNvPr name="Table 1" id="2"/>
<p:cNvGraphicFramePr>
<a:graphicFrameLocks noGrp="true"/>
</p:cNvGraphicFramePr>
<p:nvPr/>
</p:nvGraphicFramePr>
<p:xfrm>
<a:off x="381000" y="1524000"/>
<a:ext cx="1270000" cy="1270000"/>
</p:xfrm>
<a:graphic>
<a:graphicData
uri="http://schemas.openxmlformats.org/drawingml/2006/table">
<a:tbl>
<a:tblPr/>
<a:tblGrid/>
</a:tbl>
</a:graphicData>
</a:graphic>
</p:graphicFrame>
</p:spTree>
</p:cSld>
<p:clrMapOvr>
<a:masterClrMapping/>
</p:clrMapOvr>
</p:sld>
Note that there are no rows under the <a:tbl> element and no column widths
specified under the <a:tblGrid> element.
When I generate the "large" ooxml schema jar file from the schema and replace
the "small" ooxml jar file with it, then the code works correctly. In the
resulting slide1.xml file, you can see the <a:tr> element under the <a:tbl>
element now and a column width under the <a:tblGrid> element.
<a:tbl>
<a:tblPr/>
<a:tblGrid>
<a:gridCol w="1079500"/>
</a:tblGrid>
<a:tr h="1651000">
<a:tc>
...
</a:tc>
</a:tr>
</a:tbl>
If I'm doing something wrong in my use of the API, please let me know. I don't
know how to generate the "small" ooxml schema jar on my own, so for now it
looks like I'm still stuck using the "large" (14 MB) ooxml schema jar.
Thanks,
Tony
----- Original Message -----
From: Yegor Kozlov <[email protected]>
To: POI Users List <[email protected]>; Tony Collins <[email protected]>
Cc:
Sent: Tuesday, December 6, 2011 10:37 AM
Subject: Re: Trouble creating a table using XSLF
Support for tables is brand new and was committed after we released 3.8 beta4.
Yegor
On Tue, Dec 6, 2011 at 7:31 PM, Tony Collins <[email protected]> wrote:
> Thanks for the reply. Downloading the ooxml-schemas-1.1.jar seems to have
> done the trick. I'm a little surprised that the table support wasn't already
> there in the poi-ooxml-schemas-3.8-beta4-20110826.jar that came with the POI
> 3.8 Beta 4 distribution, but maybe including a table into a PowerPoint slide
> is not considered to be a common operation.
>
> Thanks again,
> Tony
>
>
>
> ----- Original Message -----
> From: Nick Burch <[email protected]>
> To: POI Users List <[email protected]>; Tony Collins <[email protected]>
> Cc:
> Sent: Monday, December 5, 2011 8:00 PM
> Subject: Re: Trouble creating a table using XSLF
>
> On Mon, 5 Dec 2011, Tony Collins wrote:
>> and getting the following error:
>>
>> Exception in thread "main" java.lang.IllegalStateException: CTTable element
>> was not found
>> at org.apache.poi.xslf.usermodel.XSLFTable.<init>(XSLFTable.java:63)
>
> See the FAQ for more details on what has happened, why, how to fix it in the
> short term, and long term:
> http://poi.apache.org/faq.html#faq-N10025
>
> Nick
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]