2016-10-27 8 views
0

OpenXMLを使用してベンダー生成ドキュメントのヘッダーとフッターにあるフィールドを反復して、フィールドに格納された結果値に置き換えてフィールドを削除しようとしています。以下は、いくつかのフィールドのうちの1つのためのフッタからのコードです(このフィールドの後に4つまたは5つ以上が想像されます)。私はSSISスクリプトタスクの一部としてこれを実行するため、.Net 3.5フレームワークとOpenXML SDK 2.0に限定されています。OpenXMLフィールド値を結果値で置き換える

<w:r> 
     <w:rPr> 
      <w:rFonts w:ascii="Segoe UI" w:hAnsi="Segoe UI" w:eastAsia="Segoe UI"/> 
      <w:sz w:val="20"/> 
     </w:rPr> 
     <w:fldChar w:fldCharType="begin"/> 
    </w:r> 
    <w:r> 
     <w:rPr> 
      <w:rFonts w:ascii="Segoe UI" w:hAnsi="Segoe UI" w:eastAsia="Segoe UI"/> 
      <w:sz w:val="20"/> 
     </w:rPr> 
     <w:instrText xml:space="preserve"> REF NG_MACRO "STANDARD" "patient_lname" </w:instrText> 
    </w:r> 
    <w:r> 
     <w:rPr> 
      <w:rFonts w:ascii="Segoe UI" w:hAnsi="Segoe UI" w:eastAsia="Segoe UI"/> 
      <w:sz w:val="20"/> 
     </w:rPr> 
     <w:fldChar w:fldCharType="separate"/> 
    </w:r> 
    <w:r> 
     <w:rPr> 
      <w:rFonts w:ascii="Segoe UI" w:hAnsi="Segoe UI" w:eastAsia="Segoe UI"/> 
      <w:sz w:val="20"/> 
     </w:rPr> 
     <w:t xml:space="preserve">Test</w:t> 
    </w:r> 

私は研究の過去3週間を通じて発見した複数のアプローチを試みたが、すべては残りを失敗したり、最初のフィールドだけに影響を与えますが、ではないように見えます。

以下は、私がやったことが一番うまくいくように思った例ですが、最初のフィールドを見つけて残りを無視しています。注:これらのベンダーフィールドの後ろには、変更したくないページ番号フィールドがあります。

+0

、変換プロセスは、私が解決策として、それを受け入れるだろう実行したとき、彼らは更新できないので、プログラムヘッダーとフッター内のフィールドをロックする方法があるかどう。文書を開くときにWordで完璧に見えるが、PDF形式で印刷または保存したり、Aspose.Wordsなどの第三者の変換ツールを使用しようとするとすぐにフィールドエラーメッセージがスローされる - 私はすでに最新かつ最高のAspose.Wordsは文字通り3行のコードでこの問題を修正する予定だが、誰もコストを承認することはない。 –

答えて

0

はこれを試してみてください

 using (WordprocessingDocument document = WordprocessingDocument.Open("Plan.doc", true)) 
     { 
      MainDocumentPart main = document.MainDocumentPart; 

      foreach (FooterPart foot in main.FooterParts) 
      { 
       foreach(var fld in foot.RootElement.Descendants<FieldCode>()) 
       { 
        if (fld != null && fld.InnerText.Contains("REF NG_MACRO")) 
        { 
         Run rFldCode = (Run)fld.Parent; 

         // Get the three (3) other Runs that make up our merge field 
         Run rBegin = rFldCode.PreviousSibling<Run>(); 
         Run rSep = rFldCode.NextSibling<Run>(); 
         Run rText = rSep.NextSibling<Run>(); 
         Run rEnd = rText.NextSibling<Run>(); 

         // Get the Run that holds the Text element for our merge field 
         // Get the Text element and replace the text content 
         Text t = rText.GetFirstChild<Text>(); 
         //t.Text = replacementText; 

         // Remove all the four (4) Runs for our merge field 
         rFldCode.Remove(); 
         rBegin.Remove(); 
         rSep.Remove(); 
         rEnd.Remove(); 
        } 
       } 

       foot.Footer.Save(); 
      } 
      document.MainDocumentPart.Document.Save(); 
      document.Close(); 
     } 

は私がなど任意の洞察力と誰もが、私が欠けているものにOpenXMLのでこれを達成するためのより良い方法を提供することができ思考を、感謝しています。わたしにはできる。また

using (WordprocessingDocument wordDocument = WordprocessingDocument.Open("Plan.doc", true)) 
{ 

    if (null != wordDocument) 
     { 

      const string FieldDelimeter = @" MERGEFIELD "; 
      List<string> listeChamps = new List<string>(); 

      foreach (FooterPart footer in wordDocument.MainDocumentPart.FooterParts) 
      { 

       foreach(var field in footer.RootElement.Descendants<FieldCode>()) 
       { 

        int fieldNameStart = field.Text.LastIndexOf(FieldDelimeter, System.StringComparison.Ordinal); 

        if (fieldNameStart >= 0) 
        { 
         var fieldName = field.Text.Substring(fieldNameStart + FieldDelimeter.Length).Trim(); 

         Run xxxfield = (Run)field.Parent; 

         Run rBegin = xxxfield.PreviousSibling<Run>(); 
         Run rSep = xxxfield.NextSibling<Run>(); 
         Run rText = rSep.NextSibling<Run>(); 
         Run rEnd = rText.NextSibling<Run>(); 

         if (null != xxxfield) 
         { 

          Text t = rText.GetFirstChild<Text>(); 
          t.Text = replacementText; 

         } 
        } 

       } 


      } 
     } 
} 
関連する問題