API/Samples/Java/Writer/TextTable

From Apache OpenOffice Wiki
< API‎ | Samples‎ | Java
Revision as of 19:28, 14 November 2007 by Rvojta (Talk | contribs)

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 TextTable with 10 rows and 5 columns.

[java,N] XTextTable xTextTable = ( XTextTable ) UnoRuntime.queryInterface(

   XTextTable.class, xMSF.createInstance( "com.sun.star.text.TextTable" ) );
           

xTextTable.initialize( 10, 5 );

You can use xTextTable object as XTextContent in XText.insertTextContent() function to insert text table in to your Writer document.

[java,N] XTextCursor xTC = xTextDocument.getText().createTextCursor(); xTextDocument.getText().insertTextContent( xTC, xTextTable, false );

Warning: If you want to work with this table, you have to insert it in to a Writer's document after initialize() call. Otherwise, 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.

[java,N] 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 [XTextTable] IDL reference.

Personal tools