2016-10-31 10 views
1

私は、Word文書を繰り返し処理し、脚注を抽出して、段落にどこに属しているかを参照しています。
私はこれを行う方法がわかりません。しかしOpenXml Word脚注

FootnotesPart footnotesPart = doc.MainDocumentPart.FootnotesPart; 
if (footnotesPart != null) 
{ 
    IEnumerable<Footnote> footnotes = footnotesPart.Footnotes.Elements<Footnote>(); 

    foreach (var footnote in footnotes) 
    { 
     ... 
    } 
} 

、私はそれぞれの脚注は段落に属している場所を知る方法がわからない:

は、私はこのような何かを行うことができ、すべての脚注を得るためにいるのを見ました。
例えば、脚注をとり、前に脚注だったテキストの中に角かっこで囲んでおきたいと思います。
どうすればよいですか?

答えて

2

FootnoteReference要素は、FootNoteと同じIDで見つけなければなりません。これにより、脚注が配置されているRun要素が表示されます。

サンプルコード:

FootnotesPart footnotesPart = doc.MainDocumentPart.FootnotesPart; 
if (footnotesPart != null) 
{ 
    var footnotes = footnotesPart.Footnotes.Elements<Footnote>(); 
    var references = doc.MainDocumentPart.Document.Body.Descendants<FootnoteReference>().ToArray(); 
    foreach (var footnote in footnotes) 
    { 
     long id = footnote.Id; 
     var reference = references.Where(fr => (long)fr.Id == id).FirstOrDefault(); 
     if (reference != null) 
     { 
      Run run = reference.Parent as Run; 
      reference.Remove(); 
      var fnText = string.Join("", footnote.Descendants<Run>().SelectMany(r => r.Elements<Text>()).Select(t => t.Text)).Trim(); 
      run.Parent.InsertAfter(new Run(new Text("(" + fnText + ")")), run); 
     } 
    } 
} 
doc.MainDocumentPart.Document.Save(); 
doc.Close(); 
関連する問題