【问题标题】:How to add a first page footer different from default one in NPOI - Docx?如何在 NPOI - Docx 中添加与默认页脚不同的首页页脚?
【发布时间】:2023-04-02 19:30:01
【问题描述】:

我正在使用 NPOI 2.5.2 生成一个 docx 文件,但我坚持使用首页的页眉/页脚。 我想要一个第一页自定义页脚并从第二页开始编号页面。

这是我的第一页页脚代码:

// First page
doc.Document.body.sectPr = new CT_SectPr();
var footer = new CT_Ftr();
var footerParagraph = footer.AddNewP();
footerParagraph.AddNewR().AddNewT().Value = $"FIRST PAGE CUSTOM FOOTER";
var footerPar = new XWPFParagraph(footerParagraph, doc);
var parsFooter = new XWPFParagraph[1];
parsFooter[0] = footerPar;
var headerFooterPolicy = doc.GetHeaderFooterPolicy();
if (headerFooterPolicy == null)
    headerFooterPolicy = doc.CreateHeaderFooterPolicy();
headerFooterPolicy.CreateFooter(XWPFHeaderFooterPolicy.FIRST, parsFooter);

这是我的带有页码的默认页脚的代码:

// Other pages
footerParagraph = footer.AddNewP();
footerParagraph.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.begin;
footerParagraph.AddNewR().AddNewInstrText().Value = " PAGE ";
footerParagraph.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate;
footerParagraph.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.end;
footerPar = new XWPFParagraph(footerParagraph, doc);
parsFooter = new XWPFParagraph[1];
parsFooter[0] = footerPar;
headerFooterPolicy.CreateFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);

使用上面的代码,我看不到第一页的自定义页脚,而是每页中的页码。我做错了什么?

我找到了this similar question,但在 NPOI 中找不到addNewTitlePg 方法。

是否有任何适当的文档,其中包含有关 NPOI 的示例?

【问题讨论】:

    标签: c# apache-poi footer docx npoi


    【解决方案1】:

    这是我的第一个页脚工作的代码。 (使用 NPOI 2.5.3)

    var doc = new XWPFDocument();
    using (var sw = File.Create("fileformat.docx"))
    {
        
        XWPFParagraph p1 = doc.CreateParagraph();
        XWPFParagraph p2 = doc.CreateParagraph();
        XWPFRun r1 = p1.CreateRun();
        XWPFRun r2 = p2.CreateRun();
    
    
        r1.SetText("The quick brown fox");
        r1.AddBreak(BreakType.PAGE);
        r2.SetText("Next page: The quick brown fox");
    
        doc.Document.body.sectPr = new CT_SectPr();
    
        var policy = doc.CreateHeaderFooterPolicy();
        var ctSectPr = doc.Document.body.sectPr;
        if (ctSectPr.titlePg == null)
        {
            ctSectPr.titlePg = new CT_OnOff() { val = true };
        }
        var firstFooter = policy.CreateFooter(ST_HdrFtr.first);
        
        var paragraph = firstFooter.CreateParagraph();
        var run = paragraph.CreateRun();
        run.SetText("First page footer...");
    
        var defaultFooter = policy.CreateFooter(ST_HdrFtr.@default);
    
        paragraph = defaultFooter.CreateParagraph();
        run = paragraph.CreateRun();
    
        paragraph.Alignment = ParagraphAlignment.RIGHT;
        paragraph.GetCTP().AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.begin;
        paragraph.GetCTP().AddNewR().AddNewInstrText().Value = " PAGE ";
        paragraph.GetCTP().AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate;
        paragraph.GetCTP().AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.end;
    
        run = paragraph.CreateRun();
    
    
        doc.Write(sw);
        
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 2015-11-27
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      • 1970-01-01
      相关资源
      最近更新 更多