用c#/ openxml重新编写脚注

时间:2013-12-11 09:44:12

标签: c# ms-word openxml footnotes

我的程序应该在word文件中重新编写脚注。

我们有一个 VBA-Macro ,它也是如此,但它太慢了。

    Dim fussnote As Footnote
    For Each fussnote In ActiveDocument.Footnotes
    fussnote.Reference.Select
    With Selection
       With .FootnoteOptions
            .Location = wdBottomOfPage
            .NumberingRule = wdRestartContinuous
            .StartingNumber = 1
            .NumberStyle = wdNoteNumberStyleArabic
            .NumberingRule = wdRestartSection
       End With
       .Footnotes.Add range:=Selection.range, Reference:=""
    End With
Next

我尝试克隆所有脚注,然后浏览它们(和它们的引用)并更改它们的 ID (是Id是正确的属性吗?)

我的代码看起来像这样(不工作):

        public override void Work(WordprocessingDocument args)
    {
        var __allFootnotes = (Footnotes)args.MainDocumentPart
            .FootnotesPart.Footnotes.Clone();
        var footnotes = __allFootnotes.Elements<Footnote>()
            .SkipWhile(f => !(f.Id.Value > 0)).ToList();
        RenumberFootnotes(footnotes, 
            args.MainDocumentPart.Document.Body.Descendants<Paragraph>().ToList());

        var __styles = args.MainDocumentPart
            .StyleDefinitionsPart.Styles;

        for (int i = 0; i < footnotes.Count(); i++)
        {
            //var footnote = footnotes[i];
        }

        args.MainDocumentPart.FootnotesPart
            .Footnotes = __allFootnotes;
    }


    private void RenumberFootnotes(List<Footnote> footnotes, List<Paragraph> paragraphs)
    {
        var __p = paragraphs.Where(p => p.Descendants<FootnoteReference>().Any());
        var __references = __p.SelectMany(p => p.Descendants<FootnoteReference>());
        for (int i = 1; i < footnotes.Count; i++)
        {
            var __tempId = footnotes[i].Id.Value;
            footnotes[i].Id.Value = i;
            var __reference = __references.First(fr => fr.Id.Value == __tempId);
            __reference.Id.Value = i;
        }
    }

1 个答案:

答案 0 :(得分:0)

从问题中提取的解决方案:

您必须在脚注的Run中添加一个新的“FootnoteReferenceMark”对象。 “脚注”是我脚注的变量。然后我只是选择'Run'类型的第一个后代并附加一个'FootnoteReferenceMark'的新子项。

footnote.Descendants<Run>().First().AppendChild(new FootnoteReferenceMark());