2013-10-03 13 views
2

下面我有一個代碼,使用OpenXML在文檔的末尾插入圖像。 我需要做的是在文檔中嘗試查找和稱爲[Image Holder]的項目,並將其替換爲傳遞的圖像。在OpenXML中替換文字支架與圖像

這裏是當前代碼,把它添加到一個文檔的末尾

 var element = 
      new Drawing(
       new DW.Inline(
        new DW.Extent() { Cx = 990000L, Cy = 792000L }, 
        new DW.EffectExtent() 
        { 
         LeftEdge = 0L, 
         TopEdge = 0L, 
         RightEdge = 0L, 
         BottomEdge = 0L 
        }, 
        new DW.DocProperties() 
        { 
         Id = (UInt32Value)1U, 
         Name = "NGSignature" 
        }, 
        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 = "NGSignature.jpg" 
            }, 
            new PIC.NonVisualPictureDrawingProperties()), 
           new PIC.BlipFill(
            new A.Blip(
             new A.BlipExtensionList(
              new A.BlipExtension() 
              { 
               Uri = 
                "{28A0092B-C50C-407E-A947-70E740481C1C}" 
              }) 
            ) 
            { 
             Embed = relationship_id, 
             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 = 990000L, Cy = 792000L }), 
            new A.PresetGeometry(
             new A.AdjustValueList() 
            ) { Preset = A.ShapeTypeValues.Rectangle })) 
         ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" }) 
       ) 
       { 
        DistanceFromTop = (UInt32Value)0U, 
        DistanceFromBottom = (UInt32Value)0U, 
        DistanceFromLeft = (UInt32Value)0U, 
        DistanceFromRight = (UInt32Value)0U, 
        EditId = "50D07946" 
       }); 

     word_doc.MainDocumentPart.Document.Body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(element))); 

UPDATE 確定我開在OpenXML的工具中的文件,並發現含有數據I以下行需要在XML文件內部進行替換。

<w:r> 
    <w:rPr> 
     <w:sz w:val="20" /> 
    </w:rPr> 
    <w:instrText xml:space="preserve"> REF NG_MACRO "HOLDER" "3fd95b6f-4c63-42fb-ba2e-dc6d57975c57" </w:instrText> 
    </w:r> 

<w:r> 
    <w:rPr> 
     <w:sz w:val="20" /> 
    </w:rPr> 
    <w:t xml:space="preserve">{HOLDER}</w:t> 
    </w:r> 

第二部分是我所看到的,當我打開該文檔。 第一部分我不知道,但創建它的應用程序把它放進去。

要正確地做到這一點,我猜想1部分需要刪除,然後圖像部分取代第二部分。

+0

圖像保持器是什麼樣的項目?它只是文本或內容佔位符? – Hans

+0

我相信這只是文字,但我可以拉開它來檢查 – Tsukasa

+0

我在命名空間中有衝突,請問您可以添加您的命名空間標籤所指的內容嗎? –

回答

4

要更換與給定的圖像文本持有者使用以下步驟:

  1. 搜索文本佔位符。
  2. 確定文本佔位符的父代
  3. 在文本佔位符後插入圖像(Drawing元素)。
  4. 取出文字支架。

下面的代碼實現上述步驟:

// Search for text holder 
Text textPlaceHolder = word_doc.MainDocumentPart.Document.Body.Descendants<Text>() 
    .Where((x) => x.Text == "$image_tag$").First(); 

if (textPlaceHolder == null) 
{ 
    Console.WriteLine("Text holder not found!");  
} 
else 
{ 
    var parent = textPlaceHolder.Parent; 

    if(!(parent is Run)) // Parent should be a run element. 
    { 
    Console.Out.WriteLine("Parent is not run"); 
    } 
    else 
    { 
    // Insert image (the image created with your function) after text place holder.   
    textPlaceHolder.Parent.InsertAfter<Drawing>(element, textPlaceHolder); 
    // Remove text place holder. 
    textPlaceHolder.Remove(); 
    } 
} 

你也可以使用內容佔位符(SdtElement),而不是簡單的文本佔位符。