Reputation: 137
im using iTextSharp for PDF Doc creater. i want to add header which is a text and a line below it and then the table to appear. the footer should contain the page number at the bottom right. im able to create the table but the header and footer seems to be an issue. can someone give an example code to solve this issue. also i want to color the header columns in the table i.e the heading columns of only the first row to have a background color. im new to this so please help me out. thanks in advance.
Upvotes: 2
Views: 16484
Reputation: 77528
Please check the keywords list listing topics that are related to iText. Now select the keyword headers / footers. You'll discover that headers and footers are added using page events.
This is how it works: when you create a document, you add objects to the document without worrying about the distribution of these objects over different pages. For instance: a PdfPTable
is split when it doesn't fit a page, and the part that doesn't fit is forwarded to the next page.
Every time this happens, iText calls an event. As documented, it is dangerous to add content in the OnStartPage()
method, so if you want to add a header and a footer, you need to do so in the OnEndPage()
event.
Let's pick a C# example from Chapter 5 of my book. In MovieHistory2, we create a page event like this:
class HeaderFooter : PdfPageEventHelper {
/** Alternating phrase for the header. */
Phrase[] header = new Phrase[2];
/** Current page number (will be reset for every chapter). */
int pagenumber;
/**
* Initialize one of the headers.
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public override void OnOpenDocument(PdfWriter writer, Document document) {
header[0] = new Phrase("Movie history");
}
/**
* Initialize one of the headers, based on the chapter title;
* reset the page number.
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onChapter(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document, float,
* com.itextpdf.text.Paragraph)
*/
public override void OnChapter(
PdfWriter writer, Document document,
float paragraphPosition, Paragraph title)
{
header[1] = new Phrase(title.Content);
pagenumber = 1;
}
/**
* Increase the page number.
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onStartPage(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public override void OnStartPage(PdfWriter writer, Document document) {
pagenumber++;
}
/**
* Adds the header and the footer.
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public override void OnEndPage(PdfWriter writer, Document document) {
Rectangle rect = writer.GetBoxSize("art");
switch(writer.PageNumber % 2) {
case 0:
ColumnText.ShowTextAligned(writer.DirectContent,
Element.ALIGN_RIGHT,
header[0],
rect.Right, rect.Top, 0
);
break;
case 1:
ColumnText.ShowTextAligned(
writer.DirectContent,
Element.ALIGN_LEFT,
header[1],
rect.Left, rect.Top, 0
);
break;
}
ColumnText.ShowTextAligned(
writer.DirectContent,
Element.ALIGN_CENTER,
new Phrase(String.Format("page {0}", pagenumber)),
(rect.Left + rect.Right) / 2,
rect.Bottom - 18, 0
);
}
}
Note that the OnChapter()
method may not be relevant for you: it stores the title of a chapter in the event and uses that title for the header. The part that should interest you, can be found in the OnEndPage()
method. Every time a page ends, we add content at an absolute position.
You want the content to be a table? No problem, that's exactly what is done in the MovieCountries1 example.
Once you've created your page event, implementing the OnEndPage()
method, all you have to do, is to declare the event to the PdfWriter
:
TableHeader tevent = new TableHeader();
writer.PageEvent = tevent;
Coloring the background of cells in a table is a no-brainer. This is explained in chapter 4.
PdfPCell cell = new PdfPCell(new Phrase("red cell"));
cell.BackgroundColor = BaseColor.RED;
I assume that you just added that remark for our information and that it wasn't an actual question, but I answered it anyway in case that setting the background color of a cell wasn't trivial to you. Surely you had no problem finding the examples that create PDFs like this one which is a PDF with repeating header and footer rows where the header and footer cells have a different color.
Upvotes: 5