我正在PDF中添加大表格(大约涵盖4到5页的pdf(。我正在使用下面的代码在 PDF 中添加大表格(大约涵盖 pdf 中的 4 到 5 页(。(代码工作正常(
private static String CreateTableDocument()
{
Document document = new Document(PageSize.A4, 0, 0, 50, 50);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("D:\ttt.pdf", FileMode.Create));
document.Open();
PdfContentByte cb = writer.DirectContent;
// GetTable("1") is my custom method that will return big table that cover 4 to 5 pages -- no problem here -
document.Add(GetTable("1"));
document.Close();
return "";
}
上面的代码成功生成PDF,现在我想为所有生成的页面添加边框。我搜索并发现可以使用PdfContentByte
或Rectangle
但它没有为所有页面添加边框,或者可能是我错过了一些东西。
其他选项可能使用PageEvent
但我使用的是WEB API,因此可能无法实现事件侦听器。
更新:我的类定义如下:(是否可以覆盖页面事件(在EndPage((
public class PDFTaskController : ApiController
{
// here my all pdf task related methods i.e. CreateTableDocument()
}
如果你不能有 onEndPage,你可以尝试以下代码:
//Add border to page
PdfContentByte content = writer.DirectContent;
Rectangle rectangle = new Rectangle(document.PageSize);
rectangle.Left += document.LeftMargin;
rectangle.Right -= document.RightMargin;
rectangle.Top -= document.TopMargin;
rectangle.Bottom += document.BottomMargin;
content.SetColorStroke(Color.BLACK);
content.Rectangle(rectangle.Left, rectangle.Bottom, rectangle.Width, rectangle.Height);
content.Stroke();
据此。