0

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);
    }
}
royhowie
  • 11,075
  • 14
  • 50
  • 67
Einav
  • 1
  • 2
  • 2
    please show some code? – Shams Ahmed Jul 14 '14 at 16:08
  • I don't understand step #5. How are you adding a writer to the document? This should all be done in step #3 when you create a writer by binding to a document and a stream. – Chris Haas Jul 14 '14 at 20:18
  • Yes, we need to see some code. My initial guess is that you're adding something in the ´OnStartPage()´ method (which would be in violation with the documentation). My second guess is that you're using `document.add()` in the `OnEndPage()` method (which is also a big mistake). Chris also has a point: your step 5 is strange. Without code, we can't tell you what you're doing wrong. – Bruno Lowagie Jul 15 '14 at 05:50
  • @BrunoLowagie - No I am not doing anything in the OnStartPage() .... as I mentioned, according to your many remarks to other (some respect that I already checked it !!!). regarding your second guess - I also don't do it, I already saw that it is a problem and I actually using the same structure as you mentioned in previous remarks to other people (!) regarding step 5 - this is a must since I am using PdfDocument and not Document as I mentioned in step 1. I will add the code in few minutes.... – Einav Jul 15 '14 at 07:31
  • Why are you using `PdfDocument`? This is a class for internal use only. That's probably what is causing your problem. I assumed you were mistakenly writing `PdfDocument` instead of `Document`. – Bruno Lowagie Jul 15 '14 at 07:34
  • @ShamsAhmed I updated the code sample I've created for you. Please note that I am using PdfDocument and NOT Document. the problem is the override iTextSharp made for Close() in PdfDocument as I mentioned. they are calling to NewPage in this override, and this is the core of the problem. why they are doing this??? – Einav Jul 15 '14 at 07:48
  • @BrunoLowagie in version 5.5.1.0 (which is the latest as far as I know) PdfDocument is not internal class, it has a Public ctor. – Einav Jul 15 '14 at 09:17
  • Nevertheless: have you seen any documentation that states that it's a good idea to use it? Maybe you're overlooking the fact that I'm the original developer of iText, so I also have some knowledge about how iText works ;-) Tell me why you'd use `PdfDocument` and I'll explain why it's a bad idea. – Bruno Lowagie Jul 15 '14 at 09:23
  • well, if it is public I may use it. I actually didn't see any documentation on using it - if it's good or bad... I know now that it is better to use Document, but I still want to understand why they made the close override like that, which makes some problems if I wish to use PdfDocument (which is public, so I may use it). – Einav Jul 15 '14 at 09:39
  • @Einav, that class _is_ public so you _are_ free to use it. No one will try to stop you but unfortunately you won't get much help either since the solution to every case I've ever seen of people using that class is to just use `Document` instead. Related to your last comment, there actually is a very, very, very good reason for `NewPage()` being called in the close method, too. "The class is intended for internal use and internally they needed to do it." (Actually, if you really want to know, I think it has to do with flushing buffers.) – Chris Haas Jul 15 '14 at 13:22

0 Answers0