1
votes

I'm blocked in my code because I can't find an answer to this topic anywhere. So what I would like to do is to add an Header to just the new chapter pages. I've already achieved that but when adding a new table that needs two or more pages, I can't manage to add the header just to the page that I wanted. I have the following code:

Somewhere in my main function:

            document.NewPage();
            writer.PageEvent = null;
            addHeader(writer, document, chapter3, title3);
            addFooter(writer, document, chapter3, title3);
            table = createVariablesTable();                
            headerOffsetSpace(document);
            addTableHeader(table);
            addTableBody(table, firsPage);
            document.Add(table);

            document.NewPage();
            writer.PageEvent = null;
            addFooter(writer, document, chapter3, title3);
            table = createVariablesTable();
            addTableHeader(table);
            firstPage = false;
            addTableBody(table, firstPage);
            document.Add(table);

Where:

        private void addHeader(PdfWriter writer, Document document, String chapterText, String titleText)
    { 
        Font ArialBold = new Font(Font.FontFamily.HELVETICA, 24, Font.BOLD);
        Font ArialItalic = new Font(Font.FontFamily.HELVETICA, 24, Font.ITALIC);

        Chunk headerChapterChunk = new Chunk(chapterText, ArialItalic);
        Chunk headerTitleChunk = new Chunk(titleText, ArialBold);
        Phrase headerText = new Phrase();
        headerText.Add(headerChapterChunk);
        headerText.Add(headerTitleChunk);

        HeaderPageEvent headerEvent = new HeaderPageEvent() { header = headerText };
        writer.PageEvent = headerEvent;
    }

    private void addFooter(PdfWriter writer, Document document, String chapterText, String titleText)
    {
        Font TimesNewRomanNormal = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);
        Font TimesNewRomanItalic = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.ITALIC);

        Chunk footerChapterChunk = new Chunk(chapterText, TimesNewRomanItalic);
        Chunk footerTitleChunk = new Chunk(titleText, TimesNewRomanNormal);
        Phrase footerText = new Phrase();
        footerText.Add(footerChapterChunk);
        footerText.Add(footerTitleChunk);

        FooterPageEvent footerEvent = new FooterPageEvent() { footer = footerText };
        writer.PageEvent = footerEvent;
    }

and the class PageEvent is:

class HeaderPageEvent : PdfPageEventHelper
    {
        float marginHorizontal = 42.6f;
        float marginTop = 777;
        float marginTopHeaderSeparator = 738.1f;
        float indentHeaderSeparator = 1.3f;
        float headerLeading = 27.6f;

        public Phrase header { get; set; }

        public override void OnEndPage(PdfWriter writer, Document document)
        {
            iTextSharp.text.Rectangle pageSize = document.PageSize;

            PdfContentByte cb = writer.DirectContent;

            ColumnText columnHeader = new ColumnText(cb);
            columnHeader.SetSimpleColumn(header, marginHorizontal, marginTop + headerLeading, pageSize.Width - 2 * marginHorizontal, 0, headerLeading, Element.ALIGN_LEFT);
            columnHeader.Go();

            ColumnText columnHeaderSeparator = new ColumnText(cb);
            Phrase headerSeparator = new Phrase(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.25f, 100.0f, BaseColor.BLACK, Element.ALIGN_LEFT, 0)));
            columnHeaderSeparator.SetSimpleColumn(headerSeparator, marginHorizontal - indentHeaderSeparator, marginTopHeaderSeparator, pageSize.Width - marginHorizontal, 0, 0, Element.ALIGN_LEFT);
            columnHeaderSeparator.Go();
        }
    }

    class FooterPageEvent : PdfPageEventHelper
    {
        float marginHorizontal = 42.6f;
        float indentFooterSeparator = 1.5f;
        float marginBottomFooter = 37.9f;
        float marginBottomFooterSeparator = 48.2f;

        float pageNumberEvenOffset = 35.4f;
        float pageNumberOddOffset = 28.4f;

        public Phrase footer { get; set; }

        Font TimesNewRomanNormal = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);

        public override void OnEndPage(PdfWriter writer, Document document)
        {
            iTextSharp.text.Rectangle pageSize = document.PageSize;

            PdfContentByte cb = writer.DirectContent;

            ColumnText columnFooterSeparator = new ColumnText(cb);
            Phrase footerSeparator = new Phrase(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.25f, 100.0f, BaseColor.BLACK, Element.ALIGN_LEFT, 0)));
            columnFooterSeparator.SetSimpleColumn(footerSeparator, marginHorizontal - indentFooterSeparator, 0, pageSize.Width - marginHorizontal + indentFooterSeparator, marginBottomFooterSeparator, 0, Element.ALIGN_LEFT);
            columnFooterSeparator.Go();

            ColumnText columnPageNumber = new ColumnText(cb);
            int pageN = writer.PageNumber;
            Phrase pageNumber = new Phrase(pageN.ToString(), TimesNewRomanNormal);

            ColumnText columnFooter = new ColumnText(cb);

            switch(pageN % 2)
            {
                case 0:
                    columnPageNumber.SetSimpleColumn(pageNumber, marginHorizontal, 0, pageSize.Width, marginBottomFooter, 0, Element.ALIGN_LEFT);
                    columnPageNumber.Go();

                    columnFooter.SetSimpleColumn(footer, marginHorizontal + pageNumberEvenOffset, 0, pageSize.Width, marginBottomFooter, 0, Element.ALIGN_LEFT);
                    columnFooter.Go();
                    break;

                case 1:
                    columnPageNumber.SetSimpleColumn(pageNumber, 0, 0, pageSize.Width - marginHorizontal, marginBottomFooter, 0, Element.ALIGN_RIGHT);
                    columnPageNumber.Go();

                    columnFooter.SetSimpleColumn(footer, 0, 0, pageSize.Width - marginHorizontal - pageNumberOddOffset, marginBottomFooter, 0, Element.ALIGN_RIGHT);
                    columnFooter.Go();
                    break;
            }
        }
    }

So... Doing my code like this, it works ok. I add the header and the footer event to the new chapter page. Then, I set the event to null and add the footer event again on the next page and for the following pages, the document will always have the footer event. This would solve my problem but, sometimes the "addTableBody" function is bigger than one page so the document automatically adds a new page without cleaning the page event, so it will be so many pages with the header as many pages the table needs to be displayed. Can anyone help me? I just would like to add the Header event to the new chapter page.

Thanks in advance

1

1 Answers

0
votes

Add an extra parameter to your HeaderPageEvent:

class HeaderPageEvent : PdfPageEventHelper
{
    boolean ignoreHeader = true;
    float marginHorizontal = 42.6f;
    float marginTop = 777;
    // rest of your code...
}

Use this extra parameter like this:

public override void OnEndPage(PdfWriter writer, Document document)
{
    if (ignoreHeader) return;
    ignoreHeader = true;
    // rest of your code...
}

By doing so, you tell the event only to show the header if the value of ignoreHeader is false and if it's false, it's set to true immediately.

By default, the header will never be shown, which is what you want. You only want to show the header when you want it. You can accomplish this by adding this method to your HeaderPageEvent:

void ShowHeader() {
    ignoreHeader = false;
}

You no longer need to set the page event to null. You can now just trigger the showHeader() method every time you want a header to turn up.

HeaderPageEvent event = new HeaderPageEvent();
writer.PageEvent = event;
// Add stuff, no header will be added
event.ShowHeader();
document.NewPage();
// A header will be shown, but only on one page
document.Add(table);

If you want the header to appear on the first page, you need to change the default value of ignoreHeader to false.