OpenOffice with .NET:如何迭代所有段落并读取文本
如何迭代 OpenOffice Writer 文档中的所有段落并输出文本。 我有 Java 示例,但不知道如何将代码转换为 C#。 Java 示例可以在这里找到: http://wiki.services .openoffice.org/wiki/API/Samples/Java/Writer/TextDocumentStructure
我的 C# 代码:
InitOpenOfficeEnvironment();
XMultiServiceFactory multiServiceFactory = connect();
XComponentLoader componentLoader =
XComponentLoader)multiServiceFactory.createInstance("com.sun.star.frame.Desktop");
//set the property
PropertyValue[] propertyValue = new PropertyValue[1];
PropertyValue aProperty = new PropertyValue();
aProperty.Name = "Hidden";
aProperty.Value = new uno.Any(false);
propertyValue[0] = aProperty;
XComponent xComponent =
componentLoader.loadComponentFromURL(
@"file:///C:/code/test3.doc",
"_blank", 0, propertyValue);
XEnumerationAccess xEnumerationAccess = (XEnumerationAccess)xComponent;
XEnumeration xParagraphEnumeration = xEnumerationAccess.createEnumeration();
while ( xParagraphEnumeration.hasMoreElements() )
{
// ???
// The problem is here nextElement() returns uno.Any but
// I some how should get XTextContent????
uno.Any textElement = xParagraphEnumeration.nextElement();
// create another enumeration to get all text portions of
//the paragraph
XEnumeration xParaEnumerationAccess = textElement.createEnumeration();
//step 3 Through the Text portions Enumeration,
//get interface to each individual text portion
}
xComponent.dispose();
xComponent = null;
How to iterate through all paragraphs in OpenOffice Writer document and output text.
I have Java examples, but don't know how to convert code to C#.
Java example could be found here: http://wiki.services.openoffice.org/wiki/API/Samples/Java/Writer/TextDocumentStructure
My C# code:
InitOpenOfficeEnvironment();
XMultiServiceFactory multiServiceFactory = connect();
XComponentLoader componentLoader =
XComponentLoader)multiServiceFactory.createInstance("com.sun.star.frame.Desktop");
//set the property
PropertyValue[] propertyValue = new PropertyValue[1];
PropertyValue aProperty = new PropertyValue();
aProperty.Name = "Hidden";
aProperty.Value = new uno.Any(false);
propertyValue[0] = aProperty;
XComponent xComponent =
componentLoader.loadComponentFromURL(
@"file:///C:/code/test3.doc",
"_blank", 0, propertyValue);
XEnumerationAccess xEnumerationAccess = (XEnumerationAccess)xComponent;
XEnumeration xParagraphEnumeration = xEnumerationAccess.createEnumeration();
while ( xParagraphEnumeration.hasMoreElements() )
{
// ???
// The problem is here nextElement() returns uno.Any but
// I some how should get XTextContent????
uno.Any textElement = xParagraphEnumeration.nextElement();
// create another enumeration to get all text portions of
//the paragraph
XEnumeration xParaEnumerationAccess = textElement.createEnumeration();
//step 3 Through the Text portions Enumeration,
//get interface to each individual text portion
}
xComponent.dispose();
xComponent = null;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,我找到了答案,我希望它能为遇到同样问题的人节省一些时间......
OK, I found the answer, I hope it will save some time for those who faced the same problem…
令人惊讶的是,似乎有更多的人不了解 ODF 工具包。我没怎么用过,只是浏览了一下源码。您可能遇到的一个问题是您无法保存到流,而只能保存到文件。尽管解决方法是将其保存到临时文件,然后进行流式传输。
It's amazing that more people don't seem to know about the ODF Toolkit. I haven't used it for much, just looked through the source. An issue you might have with it is that you cannot save to a stream, just a file. Though a workaround is to save to a temporary file, then stream that.