4
votes

I need to use the OpenOffice Java API to copy a document section and paste it over another document section. So far I have managed to copy the text of the section of the source document and paste it over the section at the target document (see the example below).

However, the problem is that non-text elements (graphics, formats, tables, etc.) don't get pasted on the destination document.

The code I have used to extract the text of the source section is:

// Read source file text
XComponent xComponentSource = this.ooHelper.loadDocument("file://" + fSource);
// Get sections
XTextSectionsSupplier textSectionsSupplierSource = (XTextSectionsSupplier)UnoRuntime.queryInterface(XTextSectionsSupplier.class, xComponentSource);
XNameAccess nameAccessSource = textSectionsSupplierOrigen.getTextSections();
// Get sections by name
XTextSection textSectionSource = (XTextSection)UnoRuntime.queryInterface(XTextSection.class, nameAccessOrigen.getByName("SeccEditable"));
//Get section text
String sectionSource = textSectionSource.getAnchor().getString();

To paste the text over the target section, the code to select the section is the same, and I set the string:

textSectionDest.getAnchor().setString(sectionSource);

I have read the API Javadoc, and I haven't found any method to copy the entire section. Is there any way to do it?

1
This OOOForum tutorial should help: Get a content and transfer it to another document.user641502

1 Answers

1
votes

I was having this same problem. I ended up solving by creating two cursors, one at the start of the content of what I wanted duplicated, then another at the end of the content by using, then extending the cursor selection of the first one to the second. This used the gotoRange method on the first cursor, passing in the second cursor and a True to tell it to expand selection.

Cursor Example: http://api.openoffice.org/docs/DevelopersGuide/Text/Text.xhtml#1_3_1_1_Editing_Text

Then I created an autoText container, group and element containing the selection. and inserted /pasted the content at a cursor position using the applyTo method of the autotext entry. I used a guid for the name of the autoText container so it would be unique and then deleted the container when I was done.

AutoText Example: http://api.openoffice.org/docs/DevelopersGuide/Text/Text.xhtml#1_3_1_6_Auto_Text

I can post my code if you want, however it's written in Python.