Completely remove XTextTable

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
Jenthe_M
Posts: 8
Joined: Thu May 16, 2013 1:43 pm

Completely remove XTextTable

Post by Jenthe_M »

Hello

I use a .odt template to create multiple pdf-documents with a Java application.

I want to create a table in this document using the properties of an existing table in the template. So in the template there's an empty table and in code I 'copy' this table to create the table filled with data and remove the empty table. The next time I use the template I use the filled table to create a new one and delete the previously filled table and so one, and so one. The template is added to this post as an attachment.

When I remove the table from the template there is still an empty line in the template. So the next time I use the template there's a new empty line added and so one leading to empty pages in my pdf-documents.

This is the code where I create the table:

Code: Select all

public void createTable(String srcTableName, List<List<String>> data) throws Exception {
		// Get source table and styles
		XTextTable srcTable = getTable(srcTableName);
		XCell srcHeaderCell = srcTable.getCellByName("A1");
		XCell srcContentCell = srcTable.getCellByName("A2");

		// Create target table
		XMultiServiceFactory mxDocFactory = UnoRuntime.queryInterface(
				XMultiServiceFactory.class, textDoc);

		XTextTable targetTable = UnoRuntime.queryInterface(XTextTable.class,
				mxDocFactory.createInstance("com.sun.star.text.TextTable"));

		targetTable.initialize(data.size(), data.get(0).size());

		// get properties
		XPropertySet srcProps = UnoRuntime.queryInterface(XPropertySet.class, srcTable);
		XPropertySet targetProps = UnoRuntime.queryInterface(XPropertySet.class, targetTable);

		// Move cursor to table
		cursor.gotoRange(getAnchor(srcTableName), false);

		// Insert table
		textDoc.getText().insertTextContent(cursor, targetTable, false);

		// Insert table data
		int rowId = 1;
		char columnId;
		for (List<String> row : data) {
			columnId = 'A';
			for (String value : row) {
				String cellName = columnId + "" + rowId;
				insertIntoCell(cellName, value, targetTable);
				columnId++;
				// Copy properties
				XCell cellTarget = targetTable.getCellByName(cellName);
				copyProps(rowId == 1 ? srcHeaderCell : srcContentCell, cellTarget);
				copyProps(UnoRuntime.queryInterface(XText.class, rowId == 1 ? srcHeaderCell : srcContentCell),
						UnoRuntime.queryInterface(XText.class, cellTarget));
			}
			rowId++;
		}

		// Copy table properties
		copyProps(srcTable, targetTable);
		// Optimize table columns widths
		optimizeTableColums(targetTable);
		targetProps.setPropertyValue("Width", srcProps.getPropertyValue("Width"));
		srcTable.dispose();
		XNamed xNamedTable = UnoRuntime.queryInterface(XNamed.class, targetTable);
		xNamedTable.setName(srcTableName);
	}
Is it possible to completely remove the XTextTable so the blank line is also removed?
Attachments
ExampleTemplate.odt
Example of a template I use
(8.64 KiB) Downloaded 328 times
LibreOffice 3.6.5.2 on MacOS 10.8.3
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Completely remove XTextTable

Post by hanya »

Hi, welcome to the forum.
Removing the table by css.text.XText::removeTextContent method does not remains any paragraph for me.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
Jenthe_M
Posts: 8
Joined: Thu May 16, 2013 1:43 pm

Re: Completely remove XTextTable

Post by Jenthe_M »

Thank you for your quick response.

Can you please give an example of how to use the css.text.XText::removeTextContent method with my XTextTable? I'm pretty new to the OpenOffice api and I don't get it immediately.

Thanks in advance.
LibreOffice 3.6.5.2 on MacOS 10.8.3
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Completely remove XTextTable

Post by hanya »

Like this:

Code: Select all

		XModel oInitialTarget = xScriptContext.getDocument();
		try
		{
			XTextTablesSupplier xTextTablesSupplier = UnoRuntime.queryInterface(
				XTextTablesSupplier.class, oInitialTarget);
			XNameAccess xNameAccess = xTextTablesSupplier.getTextTables();
			
			XIndexAccess xIndexAccess = UnoRuntime.queryInterface(
				XIndexAccess.class, xNameAccess);
			XTextTable xTextTable = UnoRuntime.queryInterface(
				XTextTable.class, xIndexAccess.getByIndex(0));
			
			XTextDocument xTextDocument = UnoRuntime.queryInterface(
				XTextDocument.class, oInitialTarget);
			
			XText xText = xTextDocument.getText();
			
			xText.removeTextContent(UnoRuntime.queryInterface(XTextContent.class, xTextTable));
			
		}
		catch (WrappedTargetException e1)
		{
			e1.printStackTrace();
		}
		catch (NoSuchElementException e2)
		{
			e2.printStackTrace();
		}
		catch (com.sun.star.lang.IndexOutOfBoundsException e3)
		{
			e3.printStackTrace();
		}
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
Jenthe_M
Posts: 8
Joined: Thu May 16, 2013 1:43 pm

Re: Completely remove XTextTable

Post by Jenthe_M »

This doesn't seam to work for me neither. I still get empty lines in my generated documents.

Although thank you for your response.
LibreOffice 3.6.5.2 on MacOS 10.8.3
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Completely remove XTextTable

Post by hanya »

When I remove the table by removeTextContent method on your templatet attached, the table and the paragraph shared by the table is gone. The table has own paragraph, so I have thought you mentioned about it.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
Jenthe_M
Posts: 8
Joined: Thu May 16, 2013 1:43 pm

Re: Completely remove XTextTable

Post by Jenthe_M »

Sorry but I don't completely get what you are saying.

Do you mean that it is normal that I get blank lines and pages when using my template? Or does it work properly when you use my template?

Did you also try creating multiple documents using the same template? I save the template in my Java-code and don't read it every time again. So the modifications in the template are also used for the next document to generate leading to multiple blank lines and after a few documents blank pages.

Thank you for your help.
LibreOffice 3.6.5.2 on MacOS 10.8.3
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Completely remove XTextTable

Post by hanya »

Which paragraph do you want to remove from your attached template? I have tried to remove the table placed on the second page of your template, and it was gone but two more paragraph remained. But they are not part of the table, so it should be remained.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
Jenthe_M
Posts: 8
Joined: Thu May 16, 2013 1:43 pm

Re: Completely remove XTextTable

Post by Jenthe_M »

To clear things up a bit I took a few screenshots and attached them. Of course there is data in the table when I generate the documents but I removed it because it's confident data.

ScreenShot1 is a screenshot of the template. So there's a paragraph before the table and one after the table.
ScreenShot1
ScreenShot1
ScreenShot2 is a screenshot of the first document generated. So the table is removed and replaced with another. Now there's 1 paragraph before the table and 2 after the table.
ScreenShot2
ScreenShot2
ScreenShot2.png (9.66 KiB) Viewed 5865 times
ScreenShot3 is a screenshot of the second document generated. Now there's 1 paragraph before the table but 3 after the table.
ScreenShot3
ScreenShot3
ScreenShot3.png (11.51 KiB) Viewed 5865 times
I hope it's clear now.
LibreOffice 3.6.5.2 on MacOS 10.8.3
Jenthe_M
Posts: 8
Joined: Thu May 16, 2013 1:43 pm

Re: Completely remove XTextTable

Post by Jenthe_M »

Anyone a solution for this?

I've searched deeper into the odt-template and discovered that it is actually a compressed folder. When I decompressed the template there were a couple of xml-files. Is there any property or something I can change in these files so I don't keep getting empty paragraphs after removing the table?

Thanks in advance.
LibreOffice 3.6.5.2 on MacOS 10.8.3
Post Reply