OpenXML向Word文档中添加图像会随机破坏文件

问题描述

在Word文档中添加一个或多个图像后,我尝试使用Word Interop将其打开。 随机地,文档已损坏,将引发COMException“文件可能已损坏”。

在尝试确定导致此损坏的原因时,我一遍又一遍地使用了相同的文档,图像,...。 当我的应用程序启动时,它将有70%的机会打开而不会出现问题。然后它将继续 重新生成并打开就好了,直到经过几次尝试后出现一个随机点,我才得到该COMException。 之后,它将继续引发该异常,直到我重新启动应用程序为止。

损坏的文档和打开的文档除了ID之外,在生成的XML中没有任何区别。 为图像生成。

  • .NET目标:.NET Framework 4.6.1
  • DocumentFormat.OpenXml版本:2.11.3

添加图片的代码:

        var element = new Drawing
        (
            new DW.Anchor
            (
                simPos,horPos,verPos,new DW.Extent() { Cx = dimensions.ImageWidth,Cy = dimensions.ImageHeight },new DW.EffectExtent() { LeftEdge = 0L,TopEdge = 0L,RightEdge = 0L,BottomEdge = 0L },wrapper,new DW.DocProperties() { Id = (X.UInt32Value)ticks,Name = $"Picture {ticks}" },new DW.NonVisualGraphicFrameDrawingProperties(new A.GraphicFrameLocks() { NoChangeAspect = true }),new A.Graphic
                (
                    new A.GraphicData
                    (
                        new PIC.Picture
                        (
                            new PIC.NonVisualPictureProperties
                            (
                                new PIC.NonVisualDrawingProperties() { Id = (X.UInt32Value)ticks,Name = $"New Bitmap Image {ticks}.jpg" },new PIC.NonVisualPictureDrawingProperties()
                            ),new PIC.BlipFill
                            (
                                new A.Blip(new A.BlipExtensionList(new A.BlipExtension { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }))
                                {
                                    Embed = relationshipId,CompressionState = A.BlipCompressionValues.None,},new A.Stretch(new A.FillRectangle())
                            ),new PIC.ShapeProperties
                            (
                                new A.Transform2D
                                (
                                    new A.Offset() { X = 0L,Y = 0L },new A.Extents() { Cx = dimensions.ImageWidth,Cy = dimensions.ImageHeight }
                                ),new A.PresetGeometry(new A.AdjustValueList()) { Preset = A.ShapeTypeValues.Rectangle }
                            )
                        )
                    )
                    { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" }
                )
            )
            {
                SimplePos = X.BooleanValue.FromBoolean(false),BehindDoc = X.BooleanValue.FromBoolean(true),LayoutInCell = X.BooleanValue.FromBoolean(true),AllowOverlap = X.BooleanValue.FromBoolean(true),Locked = X.BooleanValue.FromBoolean(false),RelativeHeight = 500U,DistanceFromBottom = 0U,DistanceFromLeft = 114300U,DistanceFromRight = 114300U,DistanceFromTop = 0U
            }
        );

为图像生成的XML如下:

<w:drawing xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="0" relativeHeight="500" behindDoc="1" locked="0" layoutInCell="1" allowOverlap="1" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
    <wp:simplePos x="0" y="0" />
    <wp:positionH relativeFrom="character">
      <wp:posOffset>0</wp:posOffset>
    </wp:positionH>
    <wp:positionV relativeFrom="line">
      <wp:posOffset>0</wp:posOffset>
    </wp:positionV>
    <wp:extent cx="2331720" cy="2935224" />
    <wp:effectExtent l="0" t="0" r="0" b="0" />
    <wp:wrapNone />
    <wp:docPr id="1747262326" name="Picture 637338535332223862" />
    <wp:cNvGraphicFramePr>
      <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1" />
    </wp:cNvGraphicFramePr>
    <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
      <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
        <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
          <pic:nvPicPr>
            <pic:cNvPr id="1747262326" name="New Bitmap Image 637338535332223862.jpg" />
            <pic:cNvPicPr />
          </pic:nvPicPr>
          <pic:blipFill>
            <a:blip r:embed="ID_465174ea-d95b-48b5-b6a8-451afacf449b" cstate="none" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
              <a:extLst>
                <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}" />
              </a:extLst>
            </a:blip>
            <a:stretch>
              <a:fillRect />
            </a:stretch>
          </pic:blipFill>
          <pic:spPr>
            <a:xfrm>
              <a:off x="0" y="0" />
              <a:ext cx="2331720" cy="2935224" />
            </a:xfrm>
            <a:prstGeom prst="rect">
              <a:avLst />
            </a:prstGeom>
          </pic:spPr>
        </pic:pic>
      </a:graphicData>
    </a:graphic>
  </wp:anchor>
</w:drawing>

如果文件打开5次,我认为第六次也没有问题吧? 我完全不知道为什么这会导致随机损坏的文件。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...