Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a border to a page using iTextSharp?

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.

Here's my code for example:

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();
like image 361
Only Bolivian Here Avatar asked Jun 13 '11 20:06

Only Bolivian Here


3 Answers

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();
}
like image 154
TimS Avatar answered Nov 19 '22 01:11

TimS


I suggest you get the current page's direct content as you generate it, and your border with PdfContentByte.

You'll probably want a PdfPageEventHelper-derived class that does its drawing in the onEndPage event.

You can query the current page size via the document parameter's getPageSize(), and use that (tweaked a bit) to draw your borders. Given that you're using iTextSharp, you probably have a PageSize property instead of a "get" method.

Something like:

public void onEndPage(PdfWriter writer, Document doc) {
  PdfContentByte content = writer.getDirectContent();
  Rectangle pageRect = doc.getPageSize();

  pageRect.setLeft( pageRect.getLeft() + 10 );
  pageRect.setRight( pageRect.getRight() - 10 );
  pageRect.setTop( pageRect.getTop() - 10 );
  pageRect.setBottom( pageRect.getBottom() + 10 );

  content.setColorStroke( Color.red );
  content.rectangle(pageRect.getLeft(), pageRect.getBottom(), pageRect.getWidth(), pageRect.getHeight());
  content.stroke();
}

Note that you can actually pass a Rectangle into content.rectangle(), at which point that rectangle's border & fill settings are used. I figured that might be a little confusing, so didn't code it that way.

like image 1
Mark Storer Avatar answered Nov 19 '22 00:11

Mark Storer


PdfContentByte content = pdfwrite1.DirectContent;
            Rectangle rectangle = new Rectangle(doc.PageSize);
            rectangle.Left += doc.LeftMargin;
            rectangle.Right -= doc.RightMargin;
            rectangle.Top -= doc.TopMargin;
            rectangle.Bottom += doc.BottomMargin;
            content.SetColorStroke(BaseColor.BLACK);
            content.Rectangle(rectangle.Left, rectangle.Bottom, rectangle.Width, rectangle.Height);
            content.Stroke();
like image 1
Code Avatar answered Nov 19 '22 01:11

Code