想要在 itextSharp 的最后一页上删除页脚
Want to remove footer when on last page in itextShap
实际上,我正在像 "Continued on next page.." 这样的页脚部分添加一些文本。但我希望页脚在最后一页时应该删除。
这是一些屏幕截图,用于详细描述我的问题。谢谢。
public partial class Footer : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, iTextSharp.text.Document doc)
{
Paragraph footer = new Paragraph("Continued on next page..", FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.NORMAL));
footer.Alignment = Element.ALIGN_RIGHT;
PdfPTable footerTbl = new PdfPTable(1);
footerTbl.TotalWidth = 300;
footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell cell = new PdfPCell(footer);
cell.Border = 0;
cell.PaddingLeft = 10;
footerTbl.AddCell(cell);
if (writer.PageNumber - 1 != writer.CurrentPageNumber)
{
footerTbl.WriteSelectedRows(0, -1, 415, 30, writer.DirectContent);
}
}
}
向您的 Footer
class 添加一个标志,并且只要该标志具有原始值,就只添加页脚字符串。当您将最后一段内容添加到 Document
后,就在关闭它之前,翻转该标志。
您的最后一个文档页面将在关闭 Document
的过程中完成(并且其 OnEndPage
将被调用),即在您更改标志之后。
例如:
public partial class Footer : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, iTextSharp.text.Document doc)
{
if (lastPage)
return;
Paragraph footer = new Paragraph("Continued on next page..", FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.NORMAL));
footer.Alignment = Element.ALIGN_RIGHT;
PdfPTable footerTbl = new PdfPTable(1);
footerTbl.TotalWidth = 300;
footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell cell = new PdfPCell(footer);
cell.Border = 0;
cell.PaddingLeft = 10;
footerTbl.AddCell(cell);
footerTbl.WriteSelectedRows(0, -1, 415, 30, writer.DirectContent);
}
public bool lastPage = false;
}
这样使用:
Document document = ...;
PdfWriter writer = PdfWriter.GetInstance(document, ...);
Footer footer = new Footer();
writer.PageEvent = footer;
document.Open();
[... add content to document ...]
footer.lastPage = true;
document.Close();
实际上,我正在像 "Continued on next page.." 这样的页脚部分添加一些文本。但我希望页脚在最后一页时应该删除。 这是一些屏幕截图,用于详细描述我的问题。谢谢。
public partial class Footer : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, iTextSharp.text.Document doc)
{
Paragraph footer = new Paragraph("Continued on next page..", FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.NORMAL));
footer.Alignment = Element.ALIGN_RIGHT;
PdfPTable footerTbl = new PdfPTable(1);
footerTbl.TotalWidth = 300;
footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell cell = new PdfPCell(footer);
cell.Border = 0;
cell.PaddingLeft = 10;
footerTbl.AddCell(cell);
if (writer.PageNumber - 1 != writer.CurrentPageNumber)
{
footerTbl.WriteSelectedRows(0, -1, 415, 30, writer.DirectContent);
}
}
}
向您的 Footer
class 添加一个标志,并且只要该标志具有原始值,就只添加页脚字符串。当您将最后一段内容添加到 Document
后,就在关闭它之前,翻转该标志。
您的最后一个文档页面将在关闭 Document
的过程中完成(并且其 OnEndPage
将被调用),即在您更改标志之后。
例如:
public partial class Footer : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, iTextSharp.text.Document doc)
{
if (lastPage)
return;
Paragraph footer = new Paragraph("Continued on next page..", FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.NORMAL));
footer.Alignment = Element.ALIGN_RIGHT;
PdfPTable footerTbl = new PdfPTable(1);
footerTbl.TotalWidth = 300;
footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell cell = new PdfPCell(footer);
cell.Border = 0;
cell.PaddingLeft = 10;
footerTbl.AddCell(cell);
footerTbl.WriteSelectedRows(0, -1, 415, 30, writer.DirectContent);
}
public bool lastPage = false;
}
这样使用:
Document document = ...;
PdfWriter writer = PdfWriter.GetInstance(document, ...);
Footer footer = new Footer();
writer.PageEvent = footer;
document.Open();
[... add content to document ...]
footer.lastPage = true;
document.Close();