OpenOffice with .NET: how to iterate throught all paragraphs and read text
Posted
by Darius Kucinskas
on Stack Overflow
See other posts from Stack Overflow
or by Darius Kucinskas
Published on 2010-03-24T20:39:32Z
Indexed on
2010/03/31
19:13 UTC
Read the original article
Hit count: 523
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;
© Stack Overflow or respective owner