3

以下に、OpenXML を使用して Word でドキュメントの最後に画像を挿入するコードを示します。私がしなければならないことは、ドキュメント内で [Image Holder] というアイテムを探して見つけて、それを私が見過ごしている画像に置き換えることです。

ドキュメントの最後に追加する現在のコードは次のとおりです

        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)));

更新 OpenXML ツールでファイルを開くと、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>

2 番目の部分は、ドキュメントを開いたときに表示されるものです。最初の部分はよくわかりませんが、それを作成するアプリケーションが入れます。

これを正しく行うには、最初の部分を削除してから、画像セクションを2番目の部分に置き換える必要があると推測しています。

4

1 に答える 1

8

テキストホルダーを特定の画像に置き換えるには、次の手順を使用します。

  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単純なテキスト プレース ホルダーの代わりに、コンテンツ プレース ホルダー ( ) を使用することもできます。

于 2013-10-04T11:27:29.300 に答える