Page 1 of 1

[Solved] [Java] Example of inserting a section?

Posted: Mon Mar 02, 2009 6:07 pm
by groverblue
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.

Re: [Java] Example of inserting a section?

Posted: Mon Mar 02, 2009 7:55 pm
by groverblue
I think I've pieced together what needs to be done, but I'm still trying to figure out how to insert the entire contents of a document into a new section. I believe everything else if correct. Does anyone have any input?

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);
}

Re: [Java] Example of inserting a section?

Posted: Thu Mar 05, 2009 12:19 am
by groverblue
I got everything to work, except for images and horizontal line objects. Images are completely ignored and not imported into the document, whereas horizontal line objects are flushing themselves at the top of the current page.

I do have a question about the section I am using. If you look at the following code, am I properly inserting the second document (document2) into the newly created section?

Here is my routine:

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);

    }

Re: [Java] Example of inserting a section?

Posted: Thu Mar 05, 2009 6:11 pm
by groverblue
I would like to let everyone know that this issue has been resolved. The original file was a Word document that we ran through a converter. Apparently, the original image was a BMP, and the conversion saved that image as an object in the ODT file (instead of an embedded image). I deleted the BMP image object from the OpenOffice template and re-added it as a PNG. Since doing so everything is working fine.

I hope this thread proves useful to someone else.