OpenOffice with .NET:如何迭代所有段落并读取文本

发布于 2024-08-26 14:41:45 字数 1630 浏览 18 评论 0原文

如何迭代 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲸落 2024-09-02 14:41:45

好的,我找到了答案,我希望它能为遇到同样问题的人节省一些时间......

using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.table;
using unoidl.com.sun.star.text;

namespace OOApp
{
    class Program
    {
        static void Main(string[] args)
        {
            InitOpenOfficeEnvironment();
            XMultiServiceFactory multiServiceFactory = Connect();
            XComponent xComponent = null;

            string docFileName = @"C:\test3.doc";

            try
            {
                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 =
                    componentLoader
                    .loadComponentFromURL(PathConverter(docFileName), 
                        "_blank", 0, propertyValue);

                XTextDocument xTextDocument = ((XTextDocument)xComponent);
                XEnumerationAccess xEnumerationAccess =    
                    (XEnumerationAccess)xTextDocument.getText();
                XEnumeration xParagraphEnumeration = 
                    xEnumerationAccess.createEnumeration();

                while (xParagraphEnumeration.hasMoreElements())
                {
                    uno.Any element = xParagraphEnumeration.nextElement();
                    XServiceInfo xinfo = (XServiceInfo)element.Value;

                    if (xinfo.supportsService("com.sun.star.text.TextTable"))
                    {
                        Console.WriteLine("Found Table!");

                        XTextTable xTextTable = (XTextTable)element.Value;
                        String[] cellNames = xTextTable.getCellNames();

                        for (int i = 0; i < cellNames.Length; i++) 
                        {
                            XCell xCell = xTextTable.getCellByName(cellNames[i]);
                            XText xTextCell = (XText)xCell;

                            String strText = xTextCell.getString();
                            Console.WriteLine(strText);
                        } 
                    }
                    else
                    {
                        XTextContent xTextElement = (XTextContent)element.Value;
                        XEnumerationAccess xParaEnumerationAccess = 
                            (XEnumerationAccess)xTextElement;

                        // create another enumeration to get all text portions of 
                        //the paragraph
                        XEnumeration xTextPortionEnum = 
                            xParaEnumerationAccess.createEnumeration();

                        //step 3  Through the Text portions Enumeration, get interface 
                        //to each individual text portion
                        while (xTextPortionEnum.hasMoreElements())
                        {
                            XTextRange xTextPortion = 
                                (XTextRange)xTextPortionEnum.nextElement().Value;
                            Console.Write(xTextPortion.getString());
                        }
                    }                    
                }
            }
            catch (unoidl.com.sun.star.uno.Exception exp1)
            {
                Console.WriteLine(exp1.Message);
                Console.WriteLine(exp1.StackTrace);
            }
            catch(System.Exception exp2)
            {
                Console.WriteLine(exp2.Message);
                Console.WriteLine(exp2.StackTrace);
            }
            finally
            {
                xComponent.dispose();
                xComponent = null;
            }

            Console.WriteLine("Done.");
            Console.ReadLine();
        }

        static private void InitOpenOfficeEnvironment()
        {
            string baseKey;
            // OpenOffice being a 32 bit app, 
            //its registry location is different in a 64 bit OS
            if (Marshal.SizeOf(typeof(IntPtr)) == 8)
                baseKey = @"SOFTWARE\Wow6432Node\OpenOffice.org\";
            else
                baseKey = @"SOFTWARE\OpenOffice.org\";

            // Get the URE directory
            string key = baseKey + @"Layers\URE\1";
            RegistryKey reg = Registry.CurrentUser.OpenSubKey(key);
            if (reg == null) reg = Registry.LocalMachine.OpenSubKey(key);
            string urePath = reg.GetValue("UREINSTALLLOCATION") as string;
            reg.Close();
            urePath = Path.Combine(urePath, "bin");

            // Get the UNO Path
            key = baseKey + @"UNO\InstallPath";
            reg = Registry.CurrentUser.OpenSubKey(key);
            if (reg == null) reg = Registry.LocalMachine.OpenSubKey(key);
            string unoPath = reg.GetValue(null) as string;
            reg.Close();

            string path;
            path = string.Format("{0};{1}",     
                System.Environment.GetEnvironmentVariable("PATH"), urePath);
            System.Environment.SetEnvironmentVariable("PATH", path);
            System.Environment.SetEnvironmentVariable("UNO_PATH", unoPath);
        }

        public static XMultiServiceFactory Connect()
        {
            unoidl.com.sun.star.uno.XComponentContext m_xContext = null;
            try
            {
                m_xContext = uno.util.Bootstrap.bootstrap();
                System.Console.WriteLine("OO: bootstrap finished with success");
            }
            catch (System.Exception exp)
            {
                System.Console.WriteLine("OO: Exception during bootstrap <" + 
                    exp.Message + ">");
                System.Console.WriteLine("OO: Source" + exp.Source);
                if (exp.InnerException != null)
                {
                    System.Console.WriteLine("OO: Inner Message" + 
                        exp.InnerException.Message);
                    System.Console.WriteLine("OO: Inner Source" + 
                        exp.InnerException.Source);
                }
            } 
            return (m_xContext != null) ? 
                (XMultiServiceFactory)m_xContext.getServiceManager() : null;
        }

        private static string PathConverter(string file)
        {
            try
            {
                file = file.Replace(@"\", "/");
                return "file:///" + file;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
    }
}

OK, I found the answer, I hope it will save some time for those who faced the same problem…

using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.table;
using unoidl.com.sun.star.text;

namespace OOApp
{
    class Program
    {
        static void Main(string[] args)
        {
            InitOpenOfficeEnvironment();
            XMultiServiceFactory multiServiceFactory = Connect();
            XComponent xComponent = null;

            string docFileName = @"C:\test3.doc";

            try
            {
                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 =
                    componentLoader
                    .loadComponentFromURL(PathConverter(docFileName), 
                        "_blank", 0, propertyValue);

                XTextDocument xTextDocument = ((XTextDocument)xComponent);
                XEnumerationAccess xEnumerationAccess =    
                    (XEnumerationAccess)xTextDocument.getText();
                XEnumeration xParagraphEnumeration = 
                    xEnumerationAccess.createEnumeration();

                while (xParagraphEnumeration.hasMoreElements())
                {
                    uno.Any element = xParagraphEnumeration.nextElement();
                    XServiceInfo xinfo = (XServiceInfo)element.Value;

                    if (xinfo.supportsService("com.sun.star.text.TextTable"))
                    {
                        Console.WriteLine("Found Table!");

                        XTextTable xTextTable = (XTextTable)element.Value;
                        String[] cellNames = xTextTable.getCellNames();

                        for (int i = 0; i < cellNames.Length; i++) 
                        {
                            XCell xCell = xTextTable.getCellByName(cellNames[i]);
                            XText xTextCell = (XText)xCell;

                            String strText = xTextCell.getString();
                            Console.WriteLine(strText);
                        } 
                    }
                    else
                    {
                        XTextContent xTextElement = (XTextContent)element.Value;
                        XEnumerationAccess xParaEnumerationAccess = 
                            (XEnumerationAccess)xTextElement;

                        // create another enumeration to get all text portions of 
                        //the paragraph
                        XEnumeration xTextPortionEnum = 
                            xParaEnumerationAccess.createEnumeration();

                        //step 3  Through the Text portions Enumeration, get interface 
                        //to each individual text portion
                        while (xTextPortionEnum.hasMoreElements())
                        {
                            XTextRange xTextPortion = 
                                (XTextRange)xTextPortionEnum.nextElement().Value;
                            Console.Write(xTextPortion.getString());
                        }
                    }                    
                }
            }
            catch (unoidl.com.sun.star.uno.Exception exp1)
            {
                Console.WriteLine(exp1.Message);
                Console.WriteLine(exp1.StackTrace);
            }
            catch(System.Exception exp2)
            {
                Console.WriteLine(exp2.Message);
                Console.WriteLine(exp2.StackTrace);
            }
            finally
            {
                xComponent.dispose();
                xComponent = null;
            }

            Console.WriteLine("Done.");
            Console.ReadLine();
        }

        static private void InitOpenOfficeEnvironment()
        {
            string baseKey;
            // OpenOffice being a 32 bit app, 
            //its registry location is different in a 64 bit OS
            if (Marshal.SizeOf(typeof(IntPtr)) == 8)
                baseKey = @"SOFTWARE\Wow6432Node\OpenOffice.org\";
            else
                baseKey = @"SOFTWARE\OpenOffice.org\";

            // Get the URE directory
            string key = baseKey + @"Layers\URE\1";
            RegistryKey reg = Registry.CurrentUser.OpenSubKey(key);
            if (reg == null) reg = Registry.LocalMachine.OpenSubKey(key);
            string urePath = reg.GetValue("UREINSTALLLOCATION") as string;
            reg.Close();
            urePath = Path.Combine(urePath, "bin");

            // Get the UNO Path
            key = baseKey + @"UNO\InstallPath";
            reg = Registry.CurrentUser.OpenSubKey(key);
            if (reg == null) reg = Registry.LocalMachine.OpenSubKey(key);
            string unoPath = reg.GetValue(null) as string;
            reg.Close();

            string path;
            path = string.Format("{0};{1}",     
                System.Environment.GetEnvironmentVariable("PATH"), urePath);
            System.Environment.SetEnvironmentVariable("PATH", path);
            System.Environment.SetEnvironmentVariable("UNO_PATH", unoPath);
        }

        public static XMultiServiceFactory Connect()
        {
            unoidl.com.sun.star.uno.XComponentContext m_xContext = null;
            try
            {
                m_xContext = uno.util.Bootstrap.bootstrap();
                System.Console.WriteLine("OO: bootstrap finished with success");
            }
            catch (System.Exception exp)
            {
                System.Console.WriteLine("OO: Exception during bootstrap <" + 
                    exp.Message + ">");
                System.Console.WriteLine("OO: Source" + exp.Source);
                if (exp.InnerException != null)
                {
                    System.Console.WriteLine("OO: Inner Message" + 
                        exp.InnerException.Message);
                    System.Console.WriteLine("OO: Inner Source" + 
                        exp.InnerException.Source);
                }
            } 
            return (m_xContext != null) ? 
                (XMultiServiceFactory)m_xContext.getServiceManager() : null;
        }

        private static string PathConverter(string file)
        {
            try
            {
                file = file.Replace(@"\", "/");
                return "file:///" + file;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
    }
}
北城孤痞 2024-09-02 14:41:45

令人惊讶的是,似乎有更多的人不了解 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文