Header, footer and large tables with iTextSharp

I added a header and footer to my PdfPageEventHelper document.

There are several β€œlarge” tables filled in at runtime in the document.

I tried adding these tables simply "documen.Add (table)", but my headers and my footers were overwritten.

I have already tried using both methods to add tables (WriteSelectedRows and document.Add (myPdfPtable).

Here is the PageEventHelper code:

private class MyPageEventHandler : PdfPageEventHelper { public iTextSharp.text.Image ImageHeader { get; set; } public iTextSharp.text.Image ImageFooter { get; set; } public override void OnEndPage(PdfWriter writer, Document document) { var fontintestazione = FontFactory.GetFont("Verdana", 10, Font.BOLD, BaseColor.LIGHT_GRAY); var fontRight = FontFactory.GetFont("Verdana", 8, Font.BOLD, BaseColor.WHITE); var fontFooter = FontFactory.GetFont("Verdana", 6, Font.NORMAL, BaseColor.BLUE); float cellHeight = document.TopMargin; Rectangle page = document.PageSize; PdfPTable head = new PdfPTable(3); head.TotalWidth = page.Width; PdfPCell c = new PdfPCell(ImageHeader, true); c.HorizontalAlignment = Element.ALIGN_LEFT; c.FixedHeight = cellHeight; c.Border = PdfPCell.NO_BORDER; head.AddCell(c); c = new PdfPCell(new Phrase("somePhrase", fontintestazione)); c.Border = PdfPCell.NO_BORDER; head.AddCell(c); c = new PdfPCell(new Phrase("someTextBlah", fontRight)); c.Border = PdfPCell.NO_BORDER; c.HorizontalAlignment = 1; c.BackgroundColor = new BaseColor(70, 130, 180); head.AddCell(c); head.WriteSelectedRows(0, -1, 10, page.Height - cellHeight + head.TotalHeight -30, writer.DirectContent); PdfPTable footer = new PdfPTable(2); footer.TotalWidth = 316f; float[] cfWidths = new float[] { 2f, 1f }; footer.SetWidths(cfWidths); PdfPCell cf = new PdfPCell(ImageFooter, true); cf.HorizontalAlignment = Element.ALIGN_RIGHT; cf.FixedHeight = cellHeight; cf.Border = PdfPCell.NO_BORDER; footer.AddCell(cf); cf = new PdfPCell(new Phrase("someEndingText", fontFooter)); cf.HorizontalAlignment = Element.ALIGN_LEFT; cf.Border = PdfPCell.NO_BORDER; footer.AddCell(cf); footer.WriteSelectedRows(0, -1, 10, 50, writer.DirectContent); } 

On my page, I just do:

 var document = new Document(PageSize.A4); var output = new MemoryStream(); var writer = PdfWriter.GetInstance(document, output); iTextSharp.text.Image imageHeader = iTextSharp.text.Image.GetInstance(Server.MapPath("/images/header.ong")); iTextSharp.text.Image imageFooter = iTextSharp.text.Image.GetInstance(Server.MapPath("/images/footer.png")); MyPageEventHandler eve = new MyPageEventHandler { ImageHeader = imageHeader , ImageFooter = imageFooter }; writer.PageEvent = eve; document.Open(); //adding a table PdfPTable cvTable = new PdfPtable(3); cvTable.TotalWidth = document.PageSize.Width; PdfPCell hCell = new PdfPCell(new Phrase("Jobs By User", aCustomFont)); cvTable.AddCell(hCell); for(int i = 0; i < myTable.Records.Count; i++) { PdfPCell idCell = new PdfPCell(new Phrase(myTable.Records[i]._id, aFont)); cvTable.Add(idCell); //same stuff for other fields of table } //first attempt.... failed: document.Add(cvTable) //<- header and footer are overwritten by table //second attempt..... failed too... cvTable.WriteSelectedRows(0, -1, 10, myPoisition, writer.DirectContent); //kind of fail...: //the table is large and need more pages. It is trunked on the first page and overwrite //the footer. 
+6
source share
1 answer

In your OnEndPage method, you have this line:

 head.WriteSelectedRows(0, -1, 10, page.Height - cellHeight + head.TotalHeight - 30, writer.DirectContent); 

This code correctly calculates where to place content based on page height and top margin, but also includes magic 30 , in which the title lights up on top of the table. Change it to this and your headline will be fine.

 head.WriteSelectedRows(0, -1, 10, page.Height - cellHeight + head.TotalHeight, writer.DirectContent); 

I assume that this 30 trying to include some addition between your heading and the table itself. What I would recommend actually modifies the document fields themselves in the main code:

 document.SetMargins(document.LeftMargin, document.RightMargin, document.TopMargin + 30, document.BottomMargin); 

And then, given this in the OnEndPage method:

 float cellHeight = document.TopMargin - 30; 

The footer code does not actually take into account the lower limit and simply draws it at 50 , so this will always overlap. A quick fix would be to change it to:

 footer.WriteSelectedRows(0, -1, 10, footer.TotalHeight, writer.DirectContent); 

This will at least lead to the alignment of the footer. If you want some more additions as described above, just change the document fields again:

 document.SetMargins(document.LeftMargin, document.RightMargin, document.TopMargin + 30, document.BottomMargin + 30); 
+13
source

Source: https://habr.com/ru/post/911623/


All Articles