API/Samples/Java/Writer/TextTable

From Apache OpenOffice Wiki
< API‎ | Samples‎ | Java
Jump to: navigation, search


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 com.sun.star.text.TextTable 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 com.sun.star.text.XTextContent in insertTextContent function to insert text table in to your Writer document.

XTextCursor xTC = xTextDocument.getText().createTextCursor();
xTextDocument.getText().insertTextContent( xTC, xTextTable, false );
Documentation caution.png If you want to work with this table, you have to insert it into a Writer's document after initialize() call. Otherwise, a lot of things simply don't work.

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 HoriOrient property. More info available in com.sun.star.text.TextTable IDL reference.

Columns width

This is tricky. I suggest you to look at TableColumnRelativeSum and TableColumnSeparators text table properties first.

Column width depends on com.sun.star.text.TableColumnSeparator, 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.

Apache OpenOffice uses 1/100th mm as unit for width, margins, position, etc. Example - if your table width is 160 mm, Width property contains 16000. Doest it work for TableColumnSeparator.Position too? Bad news, it doesn't. Column separators do use relative values to TableColumnRelativeSum 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 TableColumnRelativeSum 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 VertOrient. Possible values comes from com.sun.star.text.VertOrientation.

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 ParaAdjust property of com.sun.star.text.XParagraphCursor.

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.

Personal tools