0

I am creating a OO Writer document with C#.

Any help would be appreciated - I no longer know whether I am coming or going, I have tried so many variations....

using C#, has anybody successfully got the following to work? I just have a simple table of 2 columns and want to set the column widths to different values (actual value at this stage immaterial - just not identical widths).

This code is adapted from various web sources given as examples of how to do column widths. I cannot get it to work....

//For OpenOffice....  
using unoidl.com.sun.star.lang;  
using unoidl.com.sun.star.uno;  
using unoidl.com.sun.star.bridge;  
using unoidl.com.sun.star.frame;  
using unoidl.com.sun.star.text;  
using unoidl.com.sun.star.beans;  
..............................  
XTextTable odtTbl = (XTextTable) ((XMultiServiceFactory)oodt).createInstance("com.sun.star.text.TextTable");  
odtTbl.initialize(10, 2);  
XPropertySet xPS = (XPropertySet)odtTbl;  
Object xObj = xPS.getPropertyValue("TableColumnSeparators")**; // << Runtime ERROR**
TableColumnSeparator[] xSeparators = (TableColumnSeparator[])xObj;  
xSeparators[0].Position = 500;  
xSeparators[1].Position = 5000;  
xPS.setPropertyValue("TableColumnSeparators", new uno.Any(typeof(unoidl.com.sun.star.text.XTextTable),xSeparators));  

// Runtime ERROR indicates the ; at the end of the Object line, with message of IllegalArgumentException

Now this is only one type of error out of all the combinations of attempts. Not many allowed execution at all, but the above code did actually run until the error.

What is the correct code for doing this in C# please?

In addition, what is the correct C# code to set an O'Writer heading to a particular style (such as "Heading 1") so that it looks and prints like that style in the document?

Thank you.

Alec Gorge
  • 17,110
  • 10
  • 59
  • 71
Edunt
  • 1
  • 1

1 Answers1

0
        unoidl.com.sun.star.uno.XComponentContext localContext = uno.util.Bootstrap.bootstrap();
        unoidl.com.sun.star.lang.XMultiServiceFactory multiServiceFactory = (unoidl.com.sun.star.lang.XMultiServiceFactory)localContext.getServiceManager();
        XComponentLoader componentLoader =(XComponentLoader)multiServiceFactory.createInstance("com.sun.star.frame.Desktop");
        XComponent xComponent = componentLoader.loadComponentFromURL(
        "private:factory/swriter", //a blank writer document
        "_blank", 0, //into a blank frame use no searchflag
        new unoidl.com.sun.star.beans.PropertyValue[0]);//use no additional arguments.
        //object odtTbl = null;
        //odtTbl = ((XMultiServiceFactory)xComponent).createInstance("com.sun.star.text.TextTable");

        XTextDocument xTextDocument = (unoidl.com.sun.star.text.XTextDocument)xComponent;
        XText xText = xTextDocument.getText();
        XTextCursor xTextCursor = xText.createTextCursor();

                    XPropertySet xTextCursorProps = (unoidl.com.sun.star.beans.XPropertySet) xTextCursor;
                    XSimpleText xSimpleText = (XSimpleText)xText;

                    XTextCursor xCursor = xSimpleText.createTextCursor();

        object objTextTable = null;
        objTextTable = ((XMultiServiceFactory)xComponent).createInstance("com.sun.star.text.TextTable");
        XTextTable xTextTable = (XTextTable)objTextTable;
        xTextTable.initialize(2,3);
        xText.insertTextContent(xCursor, xTextTable, false);


        XPropertySet xPS = (XPropertySet)objTextTable;  
        uno.Any xObj = xPS.getPropertyValue("TableColumnSeparators");
        TableColumnSeparator[] xSeparators = (TableColumnSeparator[])xObj.Value;  //!!!! xObj.Value

        xSeparators[0].Position = 2000;  
        xSeparators[1].Position = 3000;
        xPS.setPropertyValue("TableColumnSeparators", new uno.Any(typeof(TableColumnSeparator[]), xSeparators)); //!!!! TableColumnSeparator[]
mavnn
  • 9,101
  • 4
  • 34
  • 52