6

In my application, I want to read a document file (.doc or .odt or .docx) and store that text in a string. For that, I am using the code below:

string text;     
using (var streamReader = new StreamReader(@"D:\Sample\Demo.docx", System.Text.Encoding.UTF8))
{
    text = streamReader.ReadToEnd();
}

But I am unable to read or copy proper text, as it shows like:

PK�����!��x%���E���[Content_Types].xml �(������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������IO�0��H���W��p@5��r�Jqv�Ij/�ۿg�%j��)P.���y��tf�N&�QY����0��T9���w,� L!jk gs@�л���0!����Bp�����Y�VJ�t�+���N�Kk�����z�'(Ÿ��/I��X�|/F�L騏��^��w$¹ZIho|b��tŔ�r����+?�W��6V�7*�W$}�ë�DΧ���r�i��q�=��,��Fݜ��t�5+Z(��?�a�z���i�[!0�k��,}O��Ta�\� �m?�i�|���ж�AT�SB�;'m;y\9�"La��o� %��@k8��?,Fc� hL_\��̱�9I����!�=��m��TT���|P�̩}}�$�|��� ��=�|��}�����PK��

How can I read or copy text from document files?

CarenRose
  • 1,266
  • 1
  • 12
  • 24
User805
  • 113
  • 1
  • 1
  • 11
  • 1
    You have an MS Word document, not a text-document. It is not stored as text (technically it's a ZIP file). You need to use an API to access the document - e.g. the [Open XML SDK](https://msdn.microsoft.com/en-us/library/office/bb448854.aspx) – RB. May 06 '16 at 12:50
  • 4
    Possible duplicate of [Read a Word Document Using c#](http://stackoverflow.com/questions/15065053/read-a-word-document-using-c-sharp) – ihimv May 06 '16 at 12:53
  • Quick n dirt version: Open as zip - extract word/document.xml - Throw away all the markup - voilà – a-ctor May 06 '16 at 12:57
  • 1
    Possible duplicate of [Read word document in C#](http://stackoverflow.com/questions/5130911/read-word-document-in-c-sharp) – alex May 06 '16 at 13:01
  • for openoffice documents how an i do it? – User805 May 06 '16 at 13:45
  • See my answer for openoffice documents http://stackoverflow.com/a/38352469/3462919 – Porkopek Jul 13 '16 at 13:08

3 Answers3

3

For that you need to use different libraries

Example for reading data from Word document using Microsoft.Office.Interop.Word

using System;
using Microsoft.Office.Interop.Word;

class Program
{
    static void Main()
    {
    // Open a doc file.
    Application application = new Application();
    Document document = application.Documents.Open("C:\\word.doc");

    // Loop through all words in the document.
    int count = document.Words.Count;
    for (int i = 1; i <= count; i++)
    {
        // Write the word.
        string text = document.Words[i].Text;
        Console.WriteLine("Word {0} = {1}", i, text);
    }
    // Close word.
    application.Quit();
    }
}
Raghuveer
  • 2,630
  • 3
  • 29
  • 59
1

Microsoft.Office.Interop.Word is very slow for large document. So i suggest OpenXml. For Using OpenXml you should install it.

  1. Install with Package Manager:

    Install-Package DocumentFormat.OpenXml -Version 2.8.1

2.Use OpenWordprocessingDocumentReadonly function:

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace Readdocx
{
    class Program
    {
        static void Main(string[] args)
        {
            string mytext = OpenWordprocessingDocumentReadonly("mytext.docx");
        }
        public static string OpenWordprocessingDocumentReadonly(string filepath)
        {
            // Open a WordprocessingDocument based on a filepath.
            using (WordprocessingDocument wordDocument =
                WordprocessingDocument.Open(filepath, false))
            {
                // Assign a reference to the existing document body.  
                Body body = wordDocument.MainDocumentPart.Document.Body;
                //text of Docx file 
                return body.InnerText.ToString();
             }
            return "-1";
        }
    }
}
reza jafari
  • 1,228
  • 13
  • 14
0

The Microsoft DocX-Format is a container and does not hold the data in simple plaintext (which your StreamReader tries to read.

You should consider using a third-party-library like the following: https://docx.codeplex.com/

Marco de Abreu
  • 663
  • 5
  • 17