簡體   English   中英

用C#/ openxml對腳注重新編號

[英]Renumber footnotes with c#/openxml

我的程序應該在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;
        }
    }

從問題中提取的解決方案:

您必須在腳注中的“運行”中添加一個新的“ FootnoteReferenceMark”對象。 “腳注”是我的腳注變量。 然后,我只是采用“運行”類型的第一個后代,並附加一個“ FootnoteReferenceMark”的新子代。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM