How to grab text from .odt file
Asked Answered
A

1

6

I need to grab all text from odf files (open document format) in C#. I found AODL library, and installed it. I visited AODL's page https://wiki.openoffice.org to find examples on how to do the task I need, but they were all unsuccessful. For a reason that I can't imagine, all examples build new document, and there's no example in how to load a document and grab all the text (something like OpenXML). Do you guys know any reference that can guide me?

My "try"

var doc = new AODL.Document.TextDocuments.TextDocument();
        doc.Load(@"C:\path/to/Sample.odt");

But I can't figure out how to iterate with the doc document.

Acknowledgment answered 7/7, 2016 at 16:22 Comment(7)
Have you considered using the Novacode DocX Library? I have used this in the past to create or manipulate word documents. Here is a link for it on codeplex: docx.codeplex.comHemichordate
I need it to read odf (open office) files, and I think DocX just reads .docx filesAcknowledgment
If you're struggling getting access to your .odt file, you could save your file as a .docx first, then use a Word lib to read it.Distant
Do you know a library that converts from odt to docx or doc or rtf?Acknowledgment
Well, it seems that AODL library can export to html. It's just a work around and I can survive with this for the next days. Still, I'd still want to open the odf files. It can't be hard, is just open source!Acknowledgment
You don't really need a library. Just save the file as .docx in OpenOffice.Distant
I'm doing the operations in real time. This is my site kennistranslations.com/wordcount. Users upload documents, I read them and grab all the text and then I perform a wordcount, so I'm not using any external executable programs.Acknowledgment
A
4

Finally, I figured out. This is the method I created to extract all the text. Maybe is not complete, because I don't know all the parts that form the .odt file. This method grabs headers and footers, textboxes and paragraphs and concatenate it with return carriage separator. You need the AODL package, that can be installed through package manager console: PM> Install-Package AODL. And add

using AODL.Document.TextDocuments;
using AODL.Document.Content;

at the top of your program.

/// <summary>
    /// Gets all plain text from an .odt file
    /// </summary>
    /// <param name="path">
    /// the physical path of the file
    /// </param>
    /// <returns>a string with all text content</returns>
    public String GetTextFromOdt(String path)
    {
        var sb = new StringBuilder();
        using (var doc = new TextDocument())
        {
            doc.Load(path);

            //The header and footer are in the DocumentStyles part. Grab the XML of this part
            XElement stylesPart = XElement.Parse(doc.DocumentStyles.Styles.OuterXml);
            //Take all headers and footers text, concatenated with return carriage
            string stylesText = string.Join("\r\n", stylesPart.Descendants().Where(x => x.Name.LocalName == "header" || x.Name.LocalName == "footer").Select(y => y.Value));

            //Main content
            var mainPart = doc.Content.Cast<IContent>();
            var mainText = String.Join("\r\n", mainPart.Select(x => x.Node.InnerText));

            //Append both text variables
            sb.Append(stylesText + "\r\n");
            sb.Append(mainText);
        }




        return sb.ToString();
    }
Acknowledgment answered 13/7, 2016 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.