2017-10-05 6 views
0

Open XML SDKで作業を開始したばかりで、次の問題があります。 Open xml SDKを使用して、テキストファイルをテキストファイルに置き換えようとしています。 画像が希望の場所にありますが、「画像を表示できません」というアイコンが表示されます。ドキュメントの最後にイメージを追加すると、すべて正常に動作します。何故ですか?xml SDKを開きます。画像を挿入すると画像が表示されない

using (WordprocessingDocument wordDoc1 = WordprocessingDocument.Open(link, true)) 
{ 
    Text textPlaceHolder = wordDoc1.MainDocumentPart.Document.Body.Descendants<Text>().Where((x) => x.Text == "(Imageplaceholder)").FirstOrDefault(); 
    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 
     { 
      var element = 
     new DocumentFormat.OpenXml.Wordprocessing.Drawing(
      new DW.Inline(
       new DW.Extent() { Cx = 480000L, Cy = 792000L }, 
       new DW.EffectExtent() 
       { 
        LeftEdge = 980000L, 
        TopEdge = 0L, 
        RightEdge = 0L, 
        BottomEdge = 0L 
       }, 
       new DW.DocProperties() 
       { 
        Id = (UInt32Value)1U, 
        Name = "Picture 1" 
       }, 
       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 = "Test.jpg" 
           }, 
           new PIC.NonVisualPictureDrawingProperties()), 
          new PIC.BlipFill(new A.Blip(
            new A.BlipExtensionList(
             new A.BlipExtension() 
             { 
              Uri = 
              "{28A0092B-C50C-407E-A947-70E740481C1C}" 
             }) 
           ) 
           { 
            Embed = "C:\\Users\\Me\\Desktop\\Test.jpg", 
            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" 
      }); 

      // Insert image (the image created with your function) after text place holder.   
      textPlaceHolder.Parent.InsertAfter<DocumentFormat.OpenXml.Wordprocessing.Drawing>(element, textPlaceHolder); 
      // Remove text place holder. 
      textPlaceHolder.Remove(); 
      wordDoc1.Close(); 
     } 
    } 
} 

答えて

-1

あなたは埋め込みプロパティにパスを入れてきましたが、それは、画像の一部のIDでなければなりません。残りの部分をコピーした同じMSDN URLにこのパーツを作成する方法があります。

+0

外部リソースへのリンクが推奨されていますが、リンクの周りにコンテキストを追加して、仲間のユーザーには何か、その理由を理解させてください。ターゲットサイトに到達できない場合や、永続的にオフラインになる場合は、常に重要なリンクの最も関連性の高い部分を引用してください。 [回答方法​​](https://stackoverflow.com/help/how-to-answer)を参照してください。 – SilverNak

関連する問題