0

我正在用 C# 创建一个 OO Writer 文档。

任何帮助将不胜感激 - 我不再知道我是来还是去,我尝试了很多变化......

使用 C#,有没有人成功完成以下工作?我只有一个包含 2 列的简单表格,并且想将列宽设置为不同的值(此阶段的实际值无关紧要 - 只是宽度不同)。

此代码改编自各种 Web 资源,作为如何处理列宽的示例。我无法让它工作....

//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

现在,这只是所有尝试组合中的一种错误。根本没有多少允许执行,但上面的代码确实一直运行到出现错误。

请问在 C# 中执行此操作的正确代码是什么?

此外,将 O'Writer 标题设置为特定样式(例如“标题 1”)以使其在文档中看起来和打印出该样式的正确 C# 代码是什么?

谢谢你。

4

1 回答 1

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[]
于 2011-02-09T11:54:12.817 回答