[Solved] [Java] Example of inserting a section?
Posted: Mon Mar 02, 2009 6:07 pm
Does anyone have an example of inserting a section break? I need to merge documents. Also, I need to ensure that their styles don't effect each other.
User community support forum for Apache OpenOffice, LibreOffice and all the OpenOffice.org derivatives
https://forum.openoffice.org/en/forum/
Code: Select all
public void appendDocument(XTextDocument document2)
throws IllegalArgumentException, java.lang.Exception {
XText xDocText = this.document.getText();
XTextCursor xOrigDocTextCursor = xDocText.createTextCursor();
XNamed xChildNamed;
XTextContent xChildSection;
XPropertySet xOrigDocTextCursorProp;
xOrigDocTextCursor.gotoEnd(false);
xDocText.insertControlCharacter(xOrigDocTextCursor, ControlCharacter.PARAGRAPH_BREAK, false);
xOrigDocTextCursorProp = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class,
xOrigDocTextCursor);
xOrigDocTextCursorProp.setPropertyValue("BreakType", BreakType.PAGE_BEFORE);
XComponentContext xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
XMultiComponentFactory xRemoteServiceManager = xRemoteContext.getServiceManager();
xChildNamed = (XNamed) UnoRuntime.queryInterface(
XNamed.class,
xRemoteServiceManager.createInstanceWithContext("com.sun.star.text.TextSection",xRemoteContext));
xChildNamed.setName("NewSection");
xChildSection = (XTextContent) UnoRuntime.queryInterface(
XTextContent.class,
xChildNamed);
/* Add code here to insert document2 into new section */
xDocText.insertTextContent(xOrigDocTextCursor, xChildSection, false);
}Code: Select all
public void appendDocument(XTextDocument document1, XTextDocument document2)
throws IllegalArgumentException, java.lang.Exception {
XText xDocText = document1.getText();
XTextCursor xOrigDocTextCursor = xDocText.createTextCursor();
XNamed xChildNamed;
XTextContent xChildContent;
String tempDoc = generateTempDocucmentPathName();
xOrigDocTextCursor.gotoEnd(false);
xDocText.insertControlCharacter(xOrigDocTextCursor, ControlCharacter.PARAGRAPH_BREAK, false);
xChildNamed = (XNamed) UnoRuntime.queryInterface(
XNamed.class,
serviceFactory.createInstance("com.sun.star.text.TextSection", document1));
xChildNamed.setName("" + document2.hashCode());
xChildContent = (XTextContent) UnoRuntime.queryInterface(
XTextContent.class,
xChildNamed);
/* Add document2 to the new section */
xDocText.insertTextContent(xOrigDocTextCursor, xChildContent, false);
PropertyValue[] storeProps = createPropertyValueArray(
createPropertyValue("FilterName", new Any(Type.STRING, "writer8_template")),
createPropertyValue("CompressionMode", new Any(Type.STRING, "1")),
createPropertyValue("Pages", new Any(Type.STRING, "All")),
createPropertyValue("Overwrite", new Any(Type.BOOLEAN, Boolean.TRUE)));
componentExport(document2, storeProps, tempDoc);
XDocumentInsertable xDocI = (XDocumentInsertable) UnoRuntime.queryInterface(
XDocumentInsertable.class,
xOrigDocTextCursor);
PropertyValue[] loadProps=new PropertyValue[0];
xDocI.insertDocumentFromURL("file:///" + tempDoc, loadProps);
deleteFile(tempDoc);
}