emoryStream和WordprocessingDocume

emoryStream和WordprocessingDocume

本文介绍了MemoryStream和WordprocessingDocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好, 


 将WordProcessingDocument对象与MemoryStream对象一起使用时出现问题。 


 我的代码如下。 

 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();
}

dc.DocumentContent是从数据库中获取的数组。 


当我将我的对象再次保存到数据库中并打开它。 


    Word告诉我该文件已损坏,但可以打开。 


有关代码错误的任何线索?


我尝试使用普通文件进行相同的操作,并且工作正常。 



最好的问候


  Lars 


解决方案

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 

解决方案


这篇关于MemoryStream和WordprocessingDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 05:03