如何在 NPOI - Docx 中添加与默认页脚不同的首页页脚?

Posted

技术标签:

【中文标题】如何在 NPOI - Docx 中添加与默认页脚不同的首页页脚?【英文标题】:How to add a first page footer different from default one in NPOI - Docx? 【发布时间】:2021-06-15 18:41:04 【问题描述】:

我正在使用 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 的示例?

【问题讨论】:

【参考方案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);
    

【讨论】:

以上是关于如何在 NPOI - Docx 中添加与默认页脚不同的首页页脚?的主要内容,如果未能解决你的问题,请参考以下文章

RDLC页脚不显示visual studio 2008

DotNetCore.NPOI和.Net Core 2 MVC Web App

软键盘出现时如何保持Android webview中的固定页脚不覆盖输入字段?

在调整大小时,如何使位置固定页脚不覆盖浏览器的所有其他元素?

底部粘页脚不重叠

为啥openxml页脚不发布到文档