iTextSharp document close is causing an extra blank page

1.5k Views Asked by At

I am using the PdfPageEventHelper in order to be able to add Header and Footer to each page of my document automatically. As was mentioned in many places I am doing so while overriding the OnEndPage. On my class I am creating:

  1. PdfDocument
  2. creating a FileStream
  3. getting a PdfWriter vis the the static GetInstance method
  4. setting the specific PdfPageEventHelper class that I've created to the writer.PageEvent
  5. adding the writer to the document
  6. calling the document open
  7. adding some content to the document (one very small table having one row)
  8. calling the close document

Now - at step #8 the OnEndPage is being called, which is great, but somehow it is being called twice! both times it is called to page number 1 (as I see on runtime in the document parameter) therefore I am getting 2 pages in my document instead of one, where the second page is empty, and the first page is actually having my header and footer twice (overlapping). I am using iTextSharp version 5.5.1.0 , and I saw on the source file that in Document.Close method they are calling NewPage function... this is why I am getting to OnEndPage in the 2nd time. Any suggestions?

class MyPdfWriter
{
public MyPdfWriter()
{
//generate doc, file stream etc.
_document = _document = new PdfDocument();
_document.SetMargins(15, 15, 50, 50);
_document.SetPageSize(PageSize.A4);

_fs = new FileStream("myTest.pdf", FileMode.Create);
_writer = PdfWriter.GetInstance(_document, _fs);
_writer.PageEmpty = false;
_writer.PageEvent = new PdfPage(reportDetails.Header,reportDetails.Footer,reportDetails.LogoImage, reportDetails.ReportFileName);            

//open doc
_document.Open();

//add some content
var table = new PdfPTable(1);
table.AddCell("bla bla");
_document.Add(titleTable);

//close doc, stream etc.

if (_document != null && _document.IsOpen())
{
_document.Close();
 _document = null;
}

if (_writer != null)
{
 _writer.Close();
_writer = null;
}

if (_fs != null)
{
 _fs.Close();
 _fs.Dispose();
}
}
}


class PdfPage : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document document)
    {
        var footer = new PdfPTable(2);

        //Write some cells to footer
        //....


        //init the width
        footer.TotalWidth = (footer.TotalWidth.CompareTo(0f) != 0) ? footer.TotalWidth : document.PageSize.Width;
        //write the table with WriteSelectedRows
        footer.WriteSelectedRows(0, -1, document.LeftMargin, footer.TotalHeight + 10f, writer.DirectContent);


        var Header = new PdfPTable(2);

        //Write some cells to Header
        //....

        //init the width
        Header.TotalWidth = (Header.TotalWidth.CompareTo(0f) != 0) ? Header.TotalWidth : document.PageSize.Width;
        //write the table with WriteSelectedRows
        Header.WriteSelectedRows(0, -1, document.LeftMargin, document.PageSize.Height - 10,
                                     writer.DirectContent);
    }
}
0

There are 0 best solutions below