2016-10-31 4 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

FootNote과 동일한 ID로 FootnoteReference 요소를 찾아야합니다. 각주가있는 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();