OpenOffice with .NET: how to iterate throught all paragraphs and read text
- by Darius Kucinskas
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;