Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7685589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:17:05+00:00 2026-05-31T19:17:05+00:00

I’ve added an header and a footer on my document by PdfPageEventHelper. The document

  • 0

I’ve added an header and a footer on my document by PdfPageEventHelper.

The document has several “large” tables populated at runtime.

I’ve tried to add those tables simply by “documen.Add(table)”, but my header and my footer results overwritten.

I’ve already tried both methods to add the tables (WriteSelectedRows and document.Add(myPdfPtable).

Here is the code of the PageEventHelper:

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 simply 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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:17:06+00:00Added an answer on May 31, 2026 at 7:17 pm

    In your OnEndPage method you have this line:

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

    That code correctly calculates where to put content based on the page’s height and top margin but also includes a magical 30 in there which is causing the header to be drawn on top of the table. Change it to this and your header will be fine.

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

    I’m guessing that that 30 is trying to include some padding between your header and the table itself. What I would recommend is actually changing the document’s margins themselves in the main code:

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

    And then accounting for that in the OnEndPage method:

    float cellHeight = document.TopMargin - 30;
    

    Your footer code doesn’t actually account for the bottom margin and just draws it at 50 so this will always overlap. The quick fix would be to change it to:

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

    This will at least get the footer bottom-aligned. If you want some more padding like above just adjust the document margins again:

    document.SetMargins(document.LeftMargin, document.RightMargin, document.TopMargin + 30, document.BottomMargin + 30);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.