[英]Itextsharp adding border to all pdf pages
我在PDF中添加了大表格(pdf约占4至5页)。 我正在使用下面的代码在PDF中添加Big table(大约覆盖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,因此可能无法实现事件侦听器。
更新:我的类定义如下:(是否可以覆盖页面事件(onEndPage))
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();
根据此 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.