简体   繁体   English

itextsharp在所有pdf页面上添加边框

[英]Itextsharp adding border to all pdf pages

I am adding Big table( approx cover 4 to 5 pages in pdf) in PDF. 我在PDF中添加了大表格(pdf约占4至5页)。 I am using below code to add Big table( approx cover 4 to 5 pages in pdf) in PDF. 我正在使用下面的代码在PDF中添加Big table(大约覆盖pdf的4至5页)。 (Code working Fine) (代码工作正常)

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 "";
    }

Above code generate PDF successfully, Now i want to add border to all generated pages. 上面的代码成功生成了PDF,现在我想为所有生成的页面添加边框。 I search through and found that it may be possible using PdfContentByte or Rectangle but its not adding border to all pages or may be i am missing something. 我搜索通过,发现使用PdfContentByteRectangle可能是可行的,但是它没有为所有页面添加边框,或者可能是我缺少了某些东西。

Other option may be possible using PageEvent but i am using WEB API so may be not possible to implement event listener. 使用PageEvent可能会出现其他选项,但是我正在使用WEB API,因此可能无法实现事件侦听器。

UPDATE: My class definition is like below: (is it possible to override Page Event (onEndPage)) 更新:我的类定义如下:(是否可以覆盖页面事件(onEndPage))

public class PDFTaskController : ApiController
{
 // here my all pdf task related methods i.e. CreateTableDocument()
}

If you cannot have onEndPage, you can try following code: 如果无法使用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();

According to this . 根据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM