如何使用iTextSharp为页面添加边框? [英] How do I add a border to a page using iTextSharp?

查看:363
本文介绍了如何使用iTextSharp为页面添加边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 iTextSharp 为PDF文档中的页面添加边框?我正在从头开始生成PDF文件,因此我不需要为现有文档添加边框。

Is it possible to add a border to a page in a PDF document using iTextSharp? I'm generating the PDF file from scratch, so I don't need to add borders to an already existing document.

这是我的代码示例:

Document pdfDocument = new Document(PageSize.LETTER);
Font headerFont = new Font(baseFont, 13);
Font font = new Font(baseFont, 10);
PdfWriter writer = PdfWriter.GetInstance(pdfDocument, 
                                         new FileStream(fileName, FileMode.Create));
pdfDocument.Open();

//I add IElements here.

pdfDocument.Close();


推荐答案

这是一个答案(改编自Mark Storer) C#。这个例子使用页面的边距来绘制边框,我有时会发现它有助于调试页面布局。

Here is an answer (adapted from Mark Storer) in C#. This example uses the margins of the page to draw the border, which I sometimes find useful for debugging the page layout.

public override void OnEndPage(PdfWriter writer, Document document)
{
    base.OnEndPage(writer, document);

    var content = writer.DirectContent;
    var pageBorderRect = new Rectangle(document.PageSize);

    pageBorderRect.Left += document.LeftMargin;
    pageBorderRect.Right -= document.RightMargin;
    pageBorderRect.Top -= document.TopMargin;
    pageBorderRect.Bottom += document.BottomMargin;

    content.SetColorStroke(BaseColor.RED);
    content.Rectangle(pageBorderRect.Left, pageBorderRect.Bottom, pageBorderRect.Width, pageBorderRect.Height);
    content.Stroke();
}

这篇关于如何使用iTextSharp为页面添加边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆