Difference between revisions of "API/Samples/Java/Writer/TextTable"

From Apache OpenOffice Wiki
< API‎ | Samples‎ | Java
Jump to: navigation, search
m (Columns width)
m
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
<noinclude>[[Category:API]] [[Category:Samples]] [[Category:Java]] [[Category:Writer/API]]</noinclude>
 
<noinclude>[[Category:API]] [[Category:Samples]] [[Category:Java]] [[Category:Writer/API]]</noinclude>
  
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.
+
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 [[http://api.openoffice.org/docs/DevelopersGuide/Text/Text.xhtml#1_3_4_1_Table_Architecture Tables]] chapter replacement. The goal is to provide more samples with some comments.
+
This page is not Developer's Guide [[Documentation/DevGuide/Text/Tables|Tables]] chapter replacement. The goal is to provide more samples with some comments.
  
 
== TextTable ==
 
== TextTable ==
Line 10: Line 10:
  
 
If you want to create a new table, you need access to XMultiServiceFactory. I assume you have an idea how to get it.
 
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 [[http://api.openoffice.org/docs/common/ref/com/sun/star/text/TextTable.html TextTable]] with 10 rows and 5 columns.
+
Next sample creates new <idl>com.sun.star.text.TextTable</idl> with 10 rows and 5 columns.
 +
 
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
XTextTable xTextTable = ( XTextTable ) UnoRuntime.queryInterface(
 
XTextTable xTextTable = ( XTextTable ) UnoRuntime.queryInterface(
Line 18: Line 19:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
You can use ''xTextTable'' object as ''[[http://api.openoffice.org/docs/common/ref/com/sun/star/text/XTextContent.html XTextContent]]'' in
+
You can use ''xTextTable'' object as ''<idl>com.sun.star.text.XTextContent</idl>'' in
''[[http://api.openoffice.org/docs/common/ref/com/sun/star/text/XText.html#insertTextContent XText.insertTextContent()]]'' function to insert text table in to your Writer document.
+
''<idlm>com.sun.star.text.XText:insertTextContent</idlm>'' function to insert text table in to your Writer document.
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
XTextCursor xTC = xTextDocument.getText().createTextCursor();
 
XTextCursor xTC = xTextDocument.getText().createTextCursor();
Line 25: Line 26:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
{{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.}}
+
{{Warn|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 ===
 
=== Horizontal alignment ===
Line 43: Line 44:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
You can experiment with different values for ''[[http://api.openoffice.org/docs/common/ref/com/sun/star/text/TextTable.html#HoriOrient HoriOrient]]'' property. More info available in [[http://api.openoffice.org/docs/common/ref/com/sun/star/text/TextTable.html XTextTable]] IDL reference.
+
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 ===
 
=== Columns width ===
Line 94: Line 95:
 
=== Vertical alignment ===
 
=== Vertical alignment ===
  
It's quite easy to set cell vertical alignment. You can use cell property ''[[http://api.openoffice.org/docs/common/ref/com/sun/star/text/CellProperties.html#VertOrient VertOrient]]''. Possible values comes from ''[[http://api.openoffice.org/docs/common/ref/com/sun/star/text/VertOrientation.html com.sun.star.text.VertOrientation]]''.
+
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>''.
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
XPropertySet xPS = ( XPropertySet ) UnoRuntime.queryInterface(
 
XPropertySet xPS = ( XPropertySet ) UnoRuntime.queryInterface(
Line 104: Line 105:
 
=== Horizontal alignment ===
 
=== Horizontal alignment ===
  
Horizontal alignment is more complicated. There's no property ''HoriOrient'' and you have to use ''[[http://api.openoffice.org/docs/common/ref/com/sun/star/style/ParagraphProperties.html#ParaAdjust ParaAdjust]]'' property of ''[[http://api.openoffice.org/docs/common/ref/com/sun/star/text/XParagraphCursor.html XParagraphCursor]]''. Example follows.
+
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.
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
XText xText = ( XText ) UnoRuntime.queryInterface(
 
XText xText = ( XText ) UnoRuntime.queryInterface(
Line 119: Line 122:
  
 
Cell content is right aligned.
 
Cell content is right aligned.
 +
 +
[[Category:API]]
 +
[[Category:Samples]]
 +
[[Category:Java]]
 +
[[Category:Writer/API]]

Latest revision as of 12:04, 31 January 2021


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