2016-06-18 114 views
3

我正在使用Office Open XML,并且必须在特定位置添加图像。在文件上我有一个'标签',所以我可以找到它很好。但是,当我添加多个图像时,会损坏该文件。将多个图像添加到位于特定位置的Word文档OpenXML

enter image description here

这里是我的代码:(从https://msdn.microsoft.com/en-us/library/office/bb497430.aspx修改)

使用:

using DocumentFormat.OpenXml.Wordprocessing; 
using A = DocumentFormat.OpenXml.Drawing; 
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing; 
using PIC = DocumentFormat.OpenXml.Drawing.Pictures; 

我公司主营:

private const string Loc = @"D:\Documents\2-Mass Output.docx"; 
static void Main(string[] args) 
{ 
    var images = new[] 
    { 
     @"D:\Documents\NPL.bmp", 
     @"D:\Documents\NVT.bmp", 
     @"D:\Documents\MPL.bmp", 
     @"D:\Documents\MVT.bmp" 
    }; 
    var tags = new[] 
    { 
     "NormalGraph1", 
     "NormalGraph2", 
     "MisGraph1", 
     "MisGraph2" 
    }; 

    InsertPicture(Loc, images, tags); 
} 

这里是方法:

/// <summary> 
/// Inserts a picture in a paragraph with a specific text 
/// </summary> 
/// <param name="document">Where the file is located</param> 
/// <param name="fileName">Name of the image file</param> 
/// <param name="tag">Insert Image in the first paragraph that contains this text</param> 
public static void InsertPicture(string document, string[] fileName, string[] tag) 
{ 
    using (var wordprocessingDocument = WordprocessingDocument.Open(document, true)) 
    { 
     var mainPart = wordprocessingDocument.MainDocumentPart; 

     for (var i = 0; i < fileName.Length; i++) 
     { 
      var imagePart = mainPart.AddImagePart(ImagePartType.Bmp); 
      using (var stream = new FileStream(fileName[i], FileMode.Open)) 
      { 
       imagePart.FeedData(stream); 
      } 
      AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart), tag[i]); 
     } 
    } 
} 

private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId, string tag) 
{ 
    // Define the reference of the image. 
    var element = 
      new Drawing(
       new DW.Inline(
        new DW.Extent { Cx = 5d.Inches(), Cy = 2.66.Inches() }, // Width and Height of the image in inches. 1" = 1000000L 
        new DW.EffectExtent 
        { 
         LeftEdge = 0L, 
         TopEdge = 0L, 
         RightEdge = 0L, 
         BottomEdge = 0L 
        }, 
        new DW.DocProperties 
        { 
         Id = 1U, 
         Name = tag // Make sure all of the images have a different name 
        }, 
        new DW.NonVisualGraphicFrameDrawingProperties(
         new A.GraphicFrameLocks { NoChangeAspect = true }), 
        new A.Graphic(
         new A.GraphicData(
          new PIC.Picture(
           new PIC.NonVisualPictureProperties(
            new PIC.NonVisualDrawingProperties 
            { 
             Id = (UInt32Value)0U, 
             Name = $"{tag}.bmp" // Make sure all of the images have a different name 
            }, 
            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.Print 
            }, 
            new A.Stretch(
             new A.FillRectangle())), 
           new PIC.ShapeProperties(
            new A.Transform2D(
             new A.Offset { X = 0L, Y = 0L }, 
             new A.Extents { Cx = 5d.Inches(), Cy = 2.66.Inches() }), // Width and Height of the image in inches. 1" = 1000000L 
            new A.PresetGeometry(
             new A.AdjustValueList() 
            ) 
            { Preset = A.ShapeTypeValues.Rectangle })) 
         ) 
         { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" }) 
       ) 
       { 
        DistanceFromTop = 0U, 
        DistanceFromBottom = 0U, 
        DistanceFromLeft = 0U, 
        DistanceFromRight = 0U, 
        EditId = "50D07946" 
       }); 

    // Append the reference to the specific paragraph that contains the 'tag'. 
    wordDoc.MainDocumentPart.Document.Body.Elements<Paragraph>().FirstOrDefault(f => f.InnerText.Contains(tag))?.AppendChild(new Paragraph(new Run(element))); 
} 

函数根据字转换一英寸到一英寸:

private static long Inches(this double size) 
{ 
    return (long) (size*1000000); 
} 

编辑:以下是header1.xml的内容。在添加图像之前和之后它们是相同的。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<w:hdr xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se wp14"><w:p w:rsidR="007D4EA8" w:rsidRPr="00E6041C" w:rsidRDefault="00292B4C" w:rsidP="00292B4C"><w:pPr><w:spacing w:after="0" w:line="240" w:lineRule="auto"/><w:rPr><w:sz w:val="24"/></w:rPr></w:pPr><w:r w:rsidRPr="00E6041C"><w:rPr><w:noProof/><w:sz w:val="28"/></w:rPr><w:drawing><wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="0" relativeHeight="251658240" behindDoc="1" locked="0" layoutInCell="1" allowOverlap="1" wp14:anchorId="2F59D91E" wp14:editId="24EB15EA"><wp:simplePos x="0" y="0"/><wp:positionH relativeFrom="page"><wp:align>right</wp:align></wp:positionH><wp:positionV relativeFrom="paragraph"><wp:posOffset>-157972</wp:posOffset></wp:positionV><wp:extent cx="7792085" cy="661254"/><wp:effectExtent l="0" t="0" r="0" b="5715"/><wp:wrapNone/><wp:docPr id="1" name="Picture 1"/><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="0" name="Picture 1" descr="T:\Reich USA Commercial\Logo\Logos - US\Reich USA Letterhead.png"/><pic:cNvPicPr><a:picLocks noChangeAspect="1" noChangeArrowheads="1"/></pic:cNvPicPr></pic:nvPicPr><pic:blipFill><a:blip r:embed="rId1" cstate="print"><a:extLst><a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}"><a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/></a:ext></a:extLst></a:blip><a:stretch><a:fillRect/></a:stretch></pic:blipFill><pic:spPr bwMode="auto"><a:xfrm><a:off x="0" y="0"/><a:ext cx="7792085" cy="661254"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom><a:noFill/><a:ln><a:noFill/></a:ln><a:effectLst/><a:extLst><a:ext uri="{53640926-AAD7-44D8-BBD7-CCE9431645EC}"><a14:shadowObscured xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main"/></a:ext></a:extLst></pic:spPr></pic:pic></a:graphicData></a:graphic><wp14:sizeRelH relativeFrom="margin"><wp14:pctWidth>0</wp14:pctWidth></wp14:sizeRelH><wp14:sizeRelV relativeFrom="margin"><wp14:pctHeight>0</wp14:pctHeight></wp14:sizeRelV></wp:anchor></w:drawing></w:r> 
<w:r w:rsidRPr="00E6041C"><w:rPr><w:sz w:val="28"/></w:rPr><w:t>TV</w:t></w:r> 
<w:r w:rsidR="007843C2"><w:rPr><w:sz w:val="28"/></w:rPr><w:t>A</w:t></w:r> 
<w:r w:rsidRPr="00E6041C"><w:rPr><w:sz w:val="28"/></w:rPr><w:t xml:space="preserve"> Number: </w:t></w:r> 
<w:r w:rsidR="00F972A0"><w:rPr><w:sz w:val="28"/></w:rPr><w:t>&lt;</w:t></w:r> 
<w:proofErr w:type="spellStart"/><w:r w:rsidR="00F972A0"><w:rPr><w:sz w:val="28"/></w:rPr><w:t>TVCNum</w:t></w:r> 
<w:proofErr w:type="spellEnd"/><w:r w:rsidR="00F972A0"><w:rPr><w:sz w:val="28"/></w:rPr><w:t>&gt;</w:t></w:r> 
</w:p><w:p w:rsidR="00940058" w:rsidRDefault="00940058"><w:pPr><w:pStyle w:val="Header"/><w:rPr><w:sz w:val="16"/></w:rPr></w:pPr></w:p><w:p w:rsidR="00BB3259" w:rsidRPr="00940058" w:rsidRDefault="00292B4C"><w:pPr><w:pStyle w:val="Header"/><w:rPr><w:sz w:val="16"/></w:rPr></w:pPr><w:r w:rsidRPr="00926942"><w:rPr><w:sz w:val="16"/></w:rPr><w:t xml:space="preserve">Torsional Vibration </w:t></w:r> 
<w:r w:rsidR="007843C2"><w:rPr><w:sz w:val="16"/></w:rPr><w:t>Analysis</w:t></w:r> 
</w:p><w:p w:rsidR="00BB3259" w:rsidRPr="0037430D" w:rsidRDefault="00BB3259" w:rsidP="00F909DF"><w:pPr><w:tabs><w:tab w:val="center" w:pos="4680"/><w:tab w:val="right" w:pos="9360"/></w:tabs><w:spacing w:after="0" w:line="240" w:lineRule="auto"/><w:rPr><w:sz w:val="28"/></w:rPr></w:pPr><w:r w:rsidRPr="00292B4C"><w:rPr><w:sz w:val="28"/></w:rPr><w:t xml:space="preserve">Customer: </w:t></w:r> 
<w:r w:rsidR="00F972A0"><w:rPr><w:sz w:val="28"/></w:rPr><w:t>&lt;Customer&gt;</w:t></w:r> 
<w:r w:rsidR="00F909DF"><w:rPr><w:sz w:val="28"/></w:rPr><w:tab/></w:r> 
<w:r w:rsidR="00F909DF"><w:rPr><w:sz w:val="28"/></w:rPr><w:tab/></w:r> 
<w:r w:rsidR="00F909DF" w:rsidRPr="0037430D"><w:rPr><w:sz w:val="20"/></w:rPr><w:t xml:space="preserve">Performed </w:t></w:r> 
<w:proofErr w:type="gramStart"/><w:r w:rsidR="00F909DF" w:rsidRPr="0037430D"><w:rPr><w:sz w:val="20"/></w:rPr><w:t>By</w:t></w:r> 
<w:proofErr w:type="gramEnd"/><w:r w:rsidR="00F909DF" w:rsidRPr="0037430D"><w:rPr><w:sz w:val="20"/></w:rPr><w:t xml:space="preserve">: </w:t></w:r> 
<w:r w:rsidR="00F972A0"><w:rPr><w:sz w:val="20"/></w:rPr><w:t>&lt;User&gt;</w:t></w:r> 
</w:p><w:p w:rsidR="00AF585E" w:rsidRPr="00BB3259" w:rsidRDefault="00645FF6" w:rsidP="00F909DF"><w:pPr><w:tabs><w:tab w:val="right" w:pos="9360"/></w:tabs><w:spacing w:after="0" w:line="240" w:lineRule="auto"/></w:pPr><w:r><w:rPr><w:noProof/><w:sz w:val="28"/></w:rPr><mc:AlternateContent><mc:Choice Requires="wps"><w:drawing><wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="0" relativeHeight="251660288" behindDoc="0" locked="0" layoutInCell="1" allowOverlap="1" wp14:anchorId="26F2E2C8" wp14:editId="4EF2E467"><wp:simplePos x="0" y="0"/><wp:positionH relativeFrom="page"><wp:posOffset>-74874</wp:posOffset></wp:positionH><wp:positionV relativeFrom="paragraph"><wp:posOffset>173822</wp:posOffset></wp:positionV><wp:extent cx="7950241" cy="0"/><wp:effectExtent l="38100" t="19050" r="69850" b="114300"/><wp:wrapNone/><wp:docPr id="3" name="Straight Connector 3"/><wp:cNvGraphicFramePr/><a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"><wps:wsp><wps:cNvCnPr/><wps:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="7950241" cy="0"/></a:xfrm><a:prstGeom prst="line"><a:avLst/></a:prstGeom><a:ln w="12700"><a:solidFill><a:srgbClr val="335BA3"/></a:solidFill></a:ln><a:effectLst><a:outerShdw blurRad="50800" dist="38100" dir="5400000" algn="t" rotWithShape="0"><a:prstClr val="black"><a:alpha val="40000"/></a:prstClr></a:outerShdw></a:effectLst></wps:spPr><wps:style><a:lnRef idx="3"><a:schemeClr val="accent5"/></a:lnRef><a:fillRef idx="0"><a:schemeClr val="accent5"/></a:fillRef><a:effectRef idx="2"><a:schemeClr val="accent5"/></a:effectRef><a:fontRef idx="minor"><a:schemeClr val="tx1"/></a:fontRef></wps:style><wps:bodyPr/></wps:wsp></a:graphicData></a:graphic><wp14:sizeRelH relativeFrom="margin"><wp14:pctWidth>0</wp14:pctWidth></wp14:sizeRelH><wp14:sizeRelV relativeFrom="margin"><wp14:pctHeight>0</wp14:pctHeight></wp14:sizeRelV></wp:anchor></w:drawing></mc:Choice><mc:Fallback><w:pict><v:line w14:anchorId="04485610" id="Straight Connector 3" o:spid="_x0000_s1026" style="position:absolute;z-index:251660288;visibility:visible;mso-wrap-style:square;mso-width-percent:0;mso-height-percent:0;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:page;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-width-percent:0;mso-height-percent:0;mso-width-relative:margin;mso-height-relative:margin" from="-5.9pt,13.7pt" to="620.1pt,13.7pt" o:gfxdata="UEsDBBQABgAIAAAAIQC2gziS/gAAAOEBAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbJSRQU7DMBBF&#xA;90jcwfIWJU67QAgl6YK0S0CoHGBkTxKLZG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&#xA;AAAuAgAAZHJzL2Uyb0RvYy54bWxQSwECLQAUAAYACAAAACEAmCQ3TdsAAAAKAQAADwAAAAAAAAAA&#xA;AAAAAACSBAAAZHJzL2Rvd25yZXYueG1sUEsFBgAAAAAEAAQA8wAAAJoFAAAAAA==&#xA;" strokecolor="#335ba3" strokeweight="1pt"><v:stroke joinstyle="miter"/><v:shadow on="t" color="black" opacity="26214f" origin=",-.5" offset="0,3pt"/><w10:wrap anchorx="page"/></v:line></w:pict></mc:Fallback></mc:AlternateContent></w:r> 
<w:r w:rsidR="008453E8"><w:t xml:space="preserve">Customer Ref. – No. </w:t></w:r> 
<w:r w:rsidR="00BB3259"><w:t xml:space="preserve"> </w:t></w:r> 
<w:r w:rsidR="00F972A0" w:rsidRPr="00BB1CB1"><w:rPr><w:i/></w:rPr><w:t>&lt;</w:t></w:r> 
<w:proofErr w:type="spellStart"/><w:r w:rsidR="00F972A0" w:rsidRPr="00BB1CB1"><w:rPr><w:i/></w:rPr><w:t>CustRef</w:t></w:r> 
<w:proofErr w:type="spellEnd"/><w:r w:rsidR="00F972A0" w:rsidRPr="00BB1CB1"><w:rPr><w:i/></w:rPr><w:t>&gt;</w:t></w:r> 
<w:r w:rsidR="00F909DF"><w:tab/></w:r> 
<w:r w:rsidR="00DE1633"><w:rPr><w:sz w:val="16"/></w:rPr><w:t>Mahwah</w:t></w:r> 
<w:r w:rsidR="0029773E"><w:rPr><w:sz w:val="16"/></w:rPr><w:t>,</w:t></w:r> 
<w:r w:rsidR="00DE1633"><w:rPr><w:sz w:val="16"/></w:rPr><w:t xml:space="preserve"> NJ</w:t></w:r> 
<w:r w:rsidR="00EE3F20" w:rsidRPr="00EE3F20"><w:rPr><w:sz w:val="16"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r> 
<w:r w:rsidR="0029773E"><w:rPr><w:sz w:val="16"/></w:rPr><w:t>-</w:t></w:r> 
<w:r w:rsidR="00EE3F20" w:rsidRPr="00EE3F20"><w:rPr><w:sz w:val="16"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r> 
<w:r w:rsidR="00F972A0"><w:rPr><w:sz w:val="16"/></w:rPr><w:t>&lt;Date&gt;</w:t></w:r> 
<w:r w:rsidR="0037430D" w:rsidRPr="00EE3F20"><w:rPr><w:b/><w:sz w:val="10"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r> 
</w:p></w:hdr> 
+0

上面的代码是否正在运行?使用相同的代码我不能重现错误。该错误消息也表明了Header XML的问题,但是您的代码仅涉及文档的主体。 – petelids

+0

对,这是令人困惑的部分,这是我所有的代码。 –

+0

你能列出header1.xml文件的内容吗? – petelids

回答

2

我可以看到您发布的代码有3个问题,但我不确定为什么只有输出多个图像时才会看到错误。

第一个问题是DocPropertiesIdId属性在文档中的所有DocProperties之间应该是唯一的。

new DW.DocProperties 
{ 
    Id = 1U, //this should be unique amongst ALL DocProperties within the document 
    Name = tag // Make sure all of the images have a different name 
}, 

第二个问题非常相似;每个NonVisualDrawingPropertiesId在每个NonVisualDrawingProperties实例中也应该是唯一的。

请注意,在这两种情况下,您都需要这些值根据标题中已存在的值来唯一。从您发布的,你可以看到你已经有了这些属性的情况下,头XML:

<wp:docPr id="1" name="NormalGraph2" /> 

<pic:cNvPr id="0" name="NormalGraph2.bmp" /> 

最后,第三个问题,我可以看到的是在代码中添加的element。在该代码中,您找到包含tagParagraph,然后将Paragraph附加到现有的Paragraph。 A Paragraph不允许为另一个Paragraph的孩子。相反,你应该只需添加Run现有Paragraph像这样:

wordDoc.MainDocumentPart.Document.Body.Elements<Paragraph>().FirstOrDefault(f => f.InnerText.Contains(tag))?.AppendChild(new Run(element)); 

或者,如果你需要单独段落则标签Paragraph后添加Paragraph针对同一Parent节点:

var tagNode = wordDoc.MainDocumentPart.Document.Body.Elements<Paragraph>().FirstOrDefault(f => f.InnerText.Contains(tag)); 
if (tagNode != null) 
{ 
    tagNode.Parent.InsertAfter(new Run(element), tagNode); 
} 
+0

谢谢,这是ID是相同的。这很奇怪,因为那是我上次认为的。这解决了我的问题。我只是通过'i'作为索引,并将它们作为'(uint)(index + 4)'给出。 –

+0

很酷,很高兴我可以帮助@蓝眼巨兽 – petelids

+0

我刚刚改变了''部分的'id',它工作正常。谢谢!! –

相关问题