API/Samples/Java/Writer/TextTable
Controlling Writer's TextTable is not so easy, and it's quite complicated. This page should help you to solve some of your problems. Exceptions are not handled in these samples. Just to make samples as short as possible.
This page is not Developer's Guide Tables chapter replacement. The goal is to provide more samples with some comments.
TextTable
How to create new table
If you want to create a new table, you need access to XMultiServiceFactory. I assume you have an idea how to get it. Next sample creates new <idl>com.sun.star.text.TextTable</idl> with 10 rows and 5 columns.
XTextTable xTextTable = ( XTextTable ) UnoRuntime.queryInterface(
XTextTable.class, xMSF.createInstance( "com.sun.star.text.TextTable" ) );
xTextTable.initialize( 10, 5 );
You can use xTextTable object as <idl>com.sun.star.text.XTextContent</idl> in <idlm>com.sun.star.text.XText:insertTextContent</idlm> function to insert text table in to your Writer document.
XTextCursor xTC = xTextDocument.getText().createTextCursor();
xTextDocument.getText().insertTextContent( xTC, xTextTable, false );
Horizontal alignment
The easiest way how to align table is to set its horizontal orientation to NONE and set left and right margins.
XPropertySet xPS = ( XPropertySet ) UnoRuntime.queryInterface(
XPropertySet.class, xTextTable );
xPS.setPropertyValue( "HoriOrient", com.sun.star.text.HoriOrientation.NONE );
int iLeftMargin, iRightMargin; // I assume you know how do you want to align your table
xPS.setPropertyValue( "LeftMargin", iLeftMargin );
xPS.setPropertyValue( "RightMargin", iRightMargin );
You can experiment with different values for <idlm>com.sun.star.text.TextTable:HoriOrient</idlm> property. More info available in <idl>com.sun.star.text.TextTable</idl> IDL reference.
Columns width
This is tricky. I suggest you to look at <idlm>com.sun.star.text.TextTable:TableColumnRelativeSum</idlm> and <idlm>com.sun.star.text.TextTable:TableColumnSeparators</idlm> text table properties first.
Column width depends on <idl>com.sun.star.text.TableColumnSeparator</idl>, struct with two members - Position, IsVisible. Separators are used even if your table contains merged cells - they are just hidden - IsVisible is set to false. Position contains table column separator position from the left border of your table. Example - first column width equals to first table column separator position, etc. Last column width is calculated as table width less last table column separator position.
LibreOffice uses 1/100th mm as unit for width, margins, position, etc. Example - if your table width is 160 mm, <idlm>com.sun.star.text.TextTable:Width</idlm> property contains 16000. Doest it work for TableColumnSeparator.Position too? Bad news, it doesn't. Column separators do use relative values to <idlm>com.sun.star.text.TextTable:TableColumnRelativeSum</idlm> property. This property contains table width in unknown units (we do not need to know them). Position of a table column separator is in same units as <idlm>com.sun.star.text.TextTable:TableColumnRelativeSum</idlm> property. How to change column width? How to calculate separator positions?
We have a table with 5 columns. Table width is 160 mm. We want to set first column width to 80 mm and 20 mm for all remaining columns.
XPropertySet xPS = ( XPropertySet ) UnoRuntime.queryInterface(
XPropertySet.class, xTextTable );
// Get table Width and TableColumnRelativeSum properties values
int iWidth = ( Integer ) xPS.getPropertyValue( "Width" );
short sTableColumnRelativeSum = ( Short ) xPS.getPropertyValue( "TableColumnRelativeSum" );
// Calculate conversion ration
double dRatio = ( double ) sTableColumnRelativeSum / ( double ) iWidth;
// Convert our 20 mm (2000) to unknown ( relative ) units
double dRelativeWidth = ( double ) 2000 * dRatio;
// Get table column separators
Object xObj = xPS.getPropertyValue( "TableColumnSeparators" );
TableColumnSeparator[] xSeparators = ( TableColumnSeparator[] )UnoRuntime.queryInterface(
TableColumnSeparator[].class, xObj );
// Last table column separator position
double dPosition = sTableColumnRelativeSum - dRelativeWidth;
// Set set new position for all column separators
for ( int i = xSeparators.length - 1 ; i >= 0 ; i-- )
{
xSeparators[i].Position = (short) Math.ceil( dPosition );
dPosition -= dRelativeWidth;
}
// Do not forget to set TableColumnSeparators back! Otherwise, it doesn't work.
xPS.setPropertyValue( "TableColumnSeparators", xSeparators );
TextTable cell
Vertical alignment
It's quite easy to set cell vertical alignment. You can use cell property <idlm>com.sun.star.text.CellProperties:VertOrient</idlm>. Possible values comes from <idl>com.sun.star.text.VertOrientation</idl>.
XPropertySet xPS = ( XPropertySet ) UnoRuntime.queryInterface(
XPropertySet.class, xCell );
xPS.setPropertyValue( "VertOrient", com.sun.star.text.VertOrientation.TOP );
Horizontal alignment
Horizontal alignment is more complicated. There's no property HoriOrient and you have to use <idlm>com.sun.star.style.ParagraphProperties:ParaAdjust</idlm> property of <idl>com.sun.star.text.XParagraphCursor</idl>.
Example follows.
XText xText = ( XText ) UnoRuntime.queryInterface(
XText.class, xCell );
XParagraphCursor xPC = ( XParagraphCursor ) UnoRuntime.queryInterface(
XParagraphCursor.class, xText );
XPropertySet xPS = ( XPropertySet ) UnoRuntime.queryInterface(
XPropertySet.class, xPC );
xPS.setPropertyValue( "ParaAdjust", com.sun.star.style.ParagraphAdjust.RIGHT );
Cell content is right aligned.