簡體   English   中英

itextsharp在所有pdf頁面上添加邊框

[英]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,現在我想為所有生成的頁面添加邊框。 我搜索通過,發現使用PdfContentByteRectangle可能是可行的,但是它沒有為所有頁面添加邊框,或者可能是我缺少了某些東西。

使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM