[Solved] Writer XTextTable width

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
John23
Posts: 13
Joined: Tue Feb 16, 2021 10:24 pm

[Solved] Writer XTextTable width

Post by John23 »

Hi everyone,

I'm struggling with getting the correct width of newly created XTextTable.
Here is the portion of the code I'm using to create and insert text table into the text document.

Code: Select all

public object CreateTable(object document, int rowCount, int colCount)
{
	object table = null;
	try
	{
                XMultiServiceFactory docFactory = (XMultiServiceFactory)document;
                table = docFactory.createInstance("com.sun.star.text.TextTable");

                ((XTextTable)table).initialize(rowCount, colCount);
	}
	catch (Exception e)
	{
                LogException($"{MethodBase.GetCurrentMethod().DeclaringType.ToString()}.{MethodBase.GetCurrentMethod().Name}", e);
	}
	return table;
}

Code: Select all

public void InsertTextContentAtRange(object content, object range)
{
	try
	{
		XText rangeText = ((XTextRange)range).getText();
		rangeText.insertTextContent((XTextRange)range, (XTextContent)content, true);
	}
	catch (Exception e)
	{
		LogException($"{MethodBase.GetCurrentMethod().DeclaringType.ToString()}.{MethodBase.GetCurrentMethod().Name}", e);
	}
}
Every time the table is created with width: 115596 (1/100 mm) but I expect it to be the size of workspace's width: 16500 (1/100 mm).
Visually table looks OK and fits the document but I can't calculate width properly for other functions.
E.g. properly set columns width, insert line shape into table cell with width of this table cell, etc.

Tried creating table with relative width, tried setting width manually, tried different hori orientation options.
Nothing is working except with HoriOrient = Center it actually shows that table is wide and goes beyond document.
---
Actually same happens in Writer application if I create new document and insert table manually.
Inspector shows that the width property of the table is 115596 which is way bigger than expected.
If I interact with table somehow(drag border, change hori orientation, etc.) the width is changed to a proper value.

Using LibreOffice 7.3.1.3 X64 with Win10 X64.
---
Could you please help me with this.
Thanks.
Last edited by MrProgrammer on Mon Aug 29, 2022 4:03 pm, edited 1 time in total.
Reason: Tagged ✓ [Solved] -- MrProgrammer, forum moderator
LibreOffice 7.2.7 x64 on Windows 10 x64
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Writer XTextTable width

Post by JeJe »

Its because the width alignment is set to automatic by default - if you change that you'll get the width measure you want.

See Choosing table spacing and alignment here:

https://wiki.openoffice.org/wiki/Docume ... ng_a_table
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
John23
Posts: 13
Joined: Tue Feb 16, 2021 10:24 pm

Re: Writer XTextTable width

Post by John23 »

JeJe thanks for the suggestion.
There was no issue with changing width using Writer.
Struggled doing it programmatically.

I've changed the approach and tried to set table properties after it is inserted into document and it worked.
Used these properties:

Code: Select all

XPropertySet propSet = (XPropertySet)table;
propSet.setPropertyValue("HoriOrient", new Any(HoriOrientation.CENTER));
propSet.setPropertyValue("Width", new Any(width));
Setting same properties before insertion into document doesn't work.
Thanks.
Problem solved.
LibreOffice 7.2.7 x64 on Windows 10 x64
Post Reply