Finding and replacing text in memystream in C # .NET.

I have uploaded a memystream with a text document and I want to be able to modify certain text in the memystream and save it back to the text document, for example search and replace functionality. Please help me with this because I don't want to use Word Interop libraries. I have a code to load and save a document, see below. The problem is that if I convert the storage device to a string and use the string replacement method, when I save the string, all formatting inside the word document is lost and when I open the document, all it shows are black boxes all over the place.

        private void ReplaceInFile(string filePath, string searchText, string replaceText)
    {
        byte[] inputFile = File.ReadAllBytes(filePath);
        MemoryStream memory = new MemoryStream(inputFile);

        byte[] data = memory.ToArray();

        string pathStr = Request.PhysicalApplicationPath + "\\Docs\\OutputDocument.doc";
        FileInfo wordFile = new FileInfo(pathStr);
        FileStream fileStream = wordFile.Open(FileMode.Create, FileAccess.Write, FileShare.None);

        fileStream.Write(data, 0, data.Length);

        fileStream.Close();

        memory.Close();
    }

      


I copied the code from the sample code online. So this is why memystream was used as I had no idea how to do it. My problem is that the company I work with doesn't want to use the word interop as they sometimes found that the word could display popup dialogs on randomness, which prevents the coded functionality from executing. This is why I want to look at ways to achieve the merge functionality, but programmatically. I did very similar to what I want to do here many years ago, but not in C # in Delphi and I tend to lose the code. So if anyone can shed some light on this, I would be grateful.

+2


source to share


2 answers


You will need to use the Word Interop Libraries or at least something similar. It's not like Word documents - they are just text documents - they are binaries. Converting the bytes to a string and doing this replacement will completely destroy the document.



With the new open formats, you can write your own code to parse them, but this will be significantly more difficult than using a library.

+5


source


Your best bet is to convert the file to OOXML - then it's an XML file that you can update programmatically using the find / replace string, System.XML, or LINQ.



(see http://blogs.msdn.com/b/ericwhite/archive/2008/09/19/bulk-convert-doc-to-docx.aspx for more information on the server-side conversion process.)

0


source







All Articles