none
MemoryStream and WordprocessingDocument RRS feed

  • Question

  • Hi Everyone, 

     I have a problem when using the WordProcessingDocument object together with a MemoryStream object. 

     My code is the following. 

    MemoryStream stream = new MemoryStream(loanDocumentViewModel.DocumentContent);
    
                //using (MemoryStream stream = new MemoryStream(data)) //;MemoryStream ms = new MemoryStream(dc.DocumentContent))
                //{
                using (WordprocessingDocument doc = WordprocessingDocument.Open(stream, true))
                //using (WordprocessingDocument doc = WordprocessingDocument.Open(@"Documents\OtherDocuments\copy.docx", true))
                {
    
                  DocumentSettingsPart docSett = doc.MainDocumentPart.DocumentSettingsPart;
                    docSett.RootElement.Append(new DocumentProtection { Edit = DocumentProtectionValues.ReadOnly });
                    docSett.RootElement.Save();
    
                    doc.MainDocumentPart.Document.Save();
                }
    

    The dc.DocumentContent is a Array readed from a Database. 

    When I save my object into the Database again and open it. 

       Word is telling me that the file is corrupt, but It can be opened. 

    Any clues on what wrong with the code?

    I have tried to do the same operation, with a plain file and that works perfectly. 

    Best Regards

      Lars 

    Sunday, March 8, 2015 9:54 PM

All replies

  • Hi Lars,

    Thanks for posting in MSDN forum.

    Based on the description, you are not able to open the Word document from stream after you save the document to a database.

    I suggest that you write the stream to the disk and try to open it manually to see wether the issue is realtive to the code you are opening the Word document.

    If if also failed to open it manually, the issue maybe relative to the way you save the document. Would you mind sharing the code how you save the documnt?

    And here is an article about opening the Word document from a stream for your reference:
    How to: Open a word processing document from a stream (Open XML SDK)

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 9, 2015 3:03 AM
    Moderator
  • Hi Fei, 

     I have tried the following scenarios.

     Load from file  - Save to file --> OK
     Load From file -> save to Database --> Not OK  (Corrupt file) - But able to open document with warnings. 

     I have my document in a Byte[] and using the EF and Automapper to save the items down to the database. 

     So No real coding in saving items. 

    if (File.Exists(@"Documents\OtherDocuments\copy.docx"))
                {
                    File.Delete(@"Documents\OtherDocuments\copy.docx");
                }
    
                File.Copy(@"Documents\OtherDocuments\Letter to redactor if not notary.docx", @"Documents\OtherDocuments\copy.docx");
    
                using (WordprocessingDocument doc = WordprocessingDocument.Open(@"Documents\OtherDocuments\copy.docx", true))
                {
                    DocumentSettingsPart docSett = doc.MainDocumentPart.DocumentSettingsPart;
                    docSett.RootElement.Append(new DocumentProtection { Edit = DocumentProtectionValues.ReadOnly });
                    docSett.RootElement.Save();
    
                    doc.MainDocumentPart.Document.Save();
    }

    When doing the above, the document works fine. But If I do this. 

    MemoryStream stream = new MemoryStream(ldV.DocumentContent);
    
                using (WordprocessingDocument doc = WordprocessingDocument.Open(stream, true))
                
                {
    
                    DocumentSettingsPart docSett = doc.MainDocumentPart.DocumentSettingsPart;
                    docSett.RootElement.Append(new DocumentProtection { Edit = DocumentProtectionValues.ReadOnly });
                    docSett.RootElement.Save();
    
                    doc.MainDocumentPart.Document.Save();
    }
    It fails when open the document again

     


    Monday, March 9, 2015 9:14 AM
  • Hi Lars,

    Sorry for the confusion.

    The mean that save the document to disk is that like use the method you are saveing the document to database instead of using the save method provided by document object.

    Also we can save the document to disk from the database and try to open the document manully.

    >>But able to open document with warnings<<

    Did you mean that you were trying to open the document which saved into disk from database and you got warnings?

    What's the exact warning message you get? And is the data in Excel is same as you expected?

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, March 10, 2015 9:08 AM
    Moderator
  • Hi Lars,

    Out of interest did you try rewinding the stream prior to saving it out?

    I found that for whatever reason my stream position had advanced and not all of the document was written out.

    I added in

    docStream.Seek(0, SeekOrigin.Begin);

    for my stream and then wrote it out to a file to test and all worked ok.

    -Courtney

    • Proposed as answer by D. Fahsig Wednesday, February 27, 2019 9:36 AM
    Sunday, May 3, 2015 4:14 PM
  • Thank you!This was the solution for a similar problem that we had:

    We opened a word file using a MemoryStream, edited it and were trying to save it back to disk, but the file was always corrupt. It could be opened with a ZipEditor, and the file list seemed ok, but there were files missing.

    Resetting the stream fixed that.


    • Edited by D. Fahsig Wednesday, February 27, 2019 9:42 AM
    Wednesday, February 27, 2019 9:41 AM