Itextsharp 使页脚贴在每个 pdf 页面的底部

Itextsharp make footer stick at bottom of every pdf page

您好,感谢您阅读本文 post。

我正在使用 ItextSharp 创建一个包含我数据库内容的 pdf,但这不是重要的部分。

我的页脚贴在除最后一页以外的每一页的底部,因为可能没有足够的内容 "push" 放下去。

Document ResultPDF = new Document(iTextSharp.text.PageSize.A4, 25, 10, 20, 30);
PdfWriter Write = PdfWriter.GetInstance(ResultPDF, new FileStream(Server.MapPath("~") + "/PDF/" + fileName, FileMode.Create));
ResultPDF.Open();

PdfPTable Headtable = new PdfPTable(7);
Headtable.TotalWidth = 525f;
Headtable.LockedWidth = true;
Headtable.HeaderRows = 5;
Headtable.FooterRows = 2;
Headtable.KeepTogether = true;

 .....
PdfPCell Footer = new PdfPCell(new Phrase(" ")) { Colspan = 7, UseVariableBorders = true, BorderColorTop = BaseColor.BLACK, BorderColorLeft = BaseColor.WHITE, BorderColorBottom = BaseColor.WHITE, BorderColorRight = BaseColor.WHITE };
PdfPCell Footer2 = new PdfPCell(new Phrase(Session["Surveyname"].ToString() + " - " + DateTime.Now.Date.ToString("dd-MM-yyyy") + " - " + email, smallText)) { Colspan = 6 };
PdfPCell Footer3 = new PdfPCell(new Phrase("", smallText)) { Colspan = 1, HorizontalAlignment = 1 };


Headtable.AddCell(Footer);
Headtable.AddCell(Footer2);
Headtable.AddCell(Footer3);

如何确保我的页脚无论如何都位于每一页的底部?

谢谢你的时间。

请查看 PdfPTable 的源代码,更具体地说是 SetExtendLastRow() 方法:

/**
 * When set the last row on every page will be extended to fill
 * all the remaining space to the bottom boundary; except maybe the
 * final row.
 * 
 * @param extendLastRows true to extend the last row on each page; false otherwise
 * @param extendFinalRow false if you don't want to extend the final row of the complete table
 * @since iText 5.0.0
 */
public void SetExtendLastRow(bool extendLastRows, bool extendFinalRow) {
    extendLastRow[0] = extendLastRows;
    extendLastRow[1] = extendFinalRow;
}

如果你想让最后一行延伸到最后一页的底部,你需要将extendFinalRow设置为trueextendLastRows和[=13=的默认值) ] 是 false).