Как мне создать XTextTable в OpenOffice.org uno с использованием C #?
-
21-09-2019 - |
Вопрос
В python, используя pyuno, я могу сделать это следующим образом:
table = self.model.createInstance("com.sun.star.text.TextTable")
Похоже, это не работает в C #.Вот мой тестовый код (я понимаю, что мне, вероятно, не нужны все эти инструкции using, но я адаптирую чужой код):
using System;
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.document;
using unoidl.com.sun.star.text;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.util;
using unoidl.com.sun.star.table;
using unoidl.com.sun.star.beans;
namespace FromScratch
{
class MainClass
{
public static void Main(string[] args)
{
XComponentContext componentContext =
uno.util.Bootstrap.bootstrap();
XMultiServiceFactory multiServiceFactory = (XMultiServiceFactory)
componentContext.getServiceManager();
XTextDocument document;
XComponentLoader loader = (XComponentLoader)
multiServiceFactory.createInstance
("com.sun.star.frame.Desktop");
document = (XTextDocument) loader.loadComponentFromURL
("private:factory/swriter", "_blank", 0,
new PropertyValue[0]);
XText text = document.getText();
XTextCursor cursor = text.createTextCursor();
XTextTable table = (XTextTable)
multiServiceFactory.createInstance
("com.sun.star.text.TextTable");
table.initialize(2, 2);
text.insertTextContent(cursor, table, false);
}
}
}
Большая часть этого, кажется, работает нормально, но когда дело доходит до этой строки:
table.initialize(2, 2);
Я получаю сообщение об ошибке во время выполнения:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
at FromScratch.MainClass.Main (System.String[] args) [0x00063] in /home/matthew/Desktop/OpenOfficeSample/FromScratch/Main.cs:37
По-видимому, эта строка:
XTextTable table = (XTextTable)
multiServiceFactory.createInstance
("com.sun.star.text.TextTable");
на самом деле это ни к чему не привязывает table.
Что здесь вообще происходит?
Решение
Решение (из OOoForum.org):
Вы должны получить текстовую таблицу с мультисервисной фабрики document, а не с мультисервисной фабрики service manager.Вы можете сделать это, приведя свой документ (модель) к XMultiServiceFactory и вызвав его метод CreateInstance.
XTextTable table = (XTextTable)
((XMultiServiceFactory)document).createInstance
("com.sun.star.text.TextTable");
Видишь Руководство по разработке.