我与合并OpenXML的SDK两个Word文档却得到了一个腐败的文件复制图像插入头部时, [英] I am merging two Word documents with OpenXML SDK but get a corrupt document when copying an image into a header

查看:769
本文介绍了我与合并OpenXML的SDK两个Word文档却得到了一个腐败的文件复制图像插入头部时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码,在各种不同的情况,将影像复制到文档的身体的时候,包括工作。

I have code which works in all sorts of different situations, including when copying images into the body of the document.

复制(添加)头时的代码工作并从一个文档页脚到其他,只要正被复制的页眉/页脚不包含图像。

The code works when copying (adding) headers and footers from one document to the other, as long as the headers/footers being copied do not contain images.

当我复制它在其具有图像的报头,然后将得到的文件已损坏,当我尝试使用OpenXML的SDK打开它抛出一个异常说压缩部分有不一致的数据长度。
我知道的图像具有HeaderPart要创建(作为对复制时进入人体MainDocumentPart)。

When I copy a header which has an image in it, then the resulting file is corrupt, and when I try to open it with the OpenXML SDK it throws an exception saying "Compressed part has inconsistent data length". I do know that the image has to be created in the HeaderPart (as against the MainDocumentPart when copying into the body).

由它来完成合并代码图像看起来是这样的:

The code which does the merging of the image looks something like:

    private void AddSourceImagesToDestination(XElement sourceXml, OpenXmlPart sourcePart, OpenXmlPart destPart) {
      foreach(XElement drawingElement in sourceXml.Descendants(_mswDrawingElementName)) {

        XAttribute aBlipEmbedAttribute = drawingElement.Descendants(_ablipElementName).First().Attribute(_embedAttributeName);
        string relationshipId = aBlipEmbedAttribute.Value;
        ImagePart sourceImagePart = (ImagePart)sourcePart.GetPartById(relationshipId);
        ImagePart destinationImagePart = ((HeaderPart)destPart).AddImagePart(sourceImagePart.ContentType);
        string newRelationshipId = destPart.GetIdOfPart(destinationImagePart);
        aBlipEmbedAttribute.SetValue(newRelationshipId);

        destinationImagePart.FeedData(sourceImagePart.GetStream(FileMode.Open, FileAccess.Read));
      }  
  }



上面被称为传递的源和目标HeaderParts,和源头,这将在此之后被复制到目标文档的XML。调用上述步骤后,destinationHeaderPart.Header.Save()被调用。

The above is called passing the source and destination HeaderParts, and the XML of the source header which will after this be copied into the destination document. After calling the above procedure, destinationHeaderPart.Header.Save() is called.

正如我前面所说,如果有源头没有图像,然后将得到的文件是罚款(即当在foreach没有找到源XML任何绘图元素)。

As I said above, if there are no images in the source header, then the resulting document is fine (i.e. when the foreach doesn't find any drawing elements in the source XML).

我不知道,不过,在头此症状图像是否也许是一个红色的鲱鱼和真正的问题是其他地方。

I wonder, though, whether this symptom of the images in the header is perhaps a red herring and the real problem is somewhere else.

推荐答案

正如我在这个问题的评论称,该代码包括图像到页眉和页脚是好的 - 它没有的伎俩。

As I said in the comment on the question, the code to include the images into the header and footer was fine - it did the trick.

我怎么解决,我的代码(其他地方)的创建是一个有点试验和错误的损坏的文件的问题。至于其他的贡献者说,周围的OpenXML文档时,说得客气一点,不是很好。因此,有可能是另一个解决这个问题,也许我的解决方案只是工作的,因为一些其他副作用。

How I solved the problem of the corrupt file that my code (elsewhere) was creating was by a bit of trial and error. As other contributors have said, the documentation around OpenXML is, to put it mildly, not very good. So there might be another resolution to this problem, and maybe my "solution" just works because of some other side effects.

不过,我有一些代码,看起来像这样

Anyway, I have some code which looks like this:

    private MemoryStream _memoryStream;
    private WordprocessingDocument _wordDocument;
      ...
    _wordDocument = WordprocessingDocument.Open(_memoryStream, true);
      ... 

    private void ReopenDocument() {
      _wordDocument.Package.Flush();
      _wordDocument.Close();
      MemoryStream newStream = new MemoryStream();
      _memoryStream.WriteTo(newStream);
      _memoryStream.Close();
      _memoryStream = newStream;
      _memoryStream.Position = 0L;
      _wordDocument = WordprocessingDocument.Open(_memoryStream, true);
    }

如果我写_memoryStream到一个FileStream之前,立即拨打了ReopenDocument方法,那么腐败是可以避免的。

If I call the ReopenDocument method immediately prior to writing the _memoryStream to a FileStream, then the corruption is avoided.

这篇关于我与合并OpenXML的SDK两个Word文档却得到了一个腐败的文件复制图像插入头部时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆