简体   繁体   中英

Itextsharp adding border to all pdf pages

I am adding Big table( approx cover 4 to 5 pages in pdf) in PDF. I am using below code to add Big table( approx cover 4 to 5 pages in pdf) in PDF. (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. 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.

Other option may be possible using PageEvent but i am using WEB API so may be not possible to implement event listener.

UPDATE: My class definition is like below: (is it possible to override Page Event (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:

 //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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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