2

I have a list of card and I have to create statement for each card number. Now I am generating individual pdf file for each number with different header footer argument for each. Now I want all this statement in single pdf file. But if I call pdffooter inside the loop, it doesn't work. How can I have different header and footer in a pdf for each card number?

protected void ReportGenerate_Click(object sender, EventArgs e)
{
       ..........

    try
    {
        cResult = oCommonMethod.GetCardNoList(ddl_BRANCH.SelectedValue);

        int cnt = 0;
        foreach (DataRow dr in cResult.DataTable.Rows)
        {
            cnt++;
            pdfDoc = new Document();
            string path = Server.MapPath("PDFs");
            Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 10, 10, 270f, 140f);
            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(path + "/Doc" + cnt + ".pdf", FileMode.Create));

                ...........



            dtStatement = oCommonMethod.GetCardStatement(dr["CARDNO"].ToString(), dr["BRCOD"].ToString(), dr["ACNO"].ToString(), dr["ACTCOD"].ToString(), dr["CARD_TYPE"].ToString(), sday);
            previous_balance = oCommonMethod.GetPreviousBalance(dr["BRCOD"].ToString(), dr["ACNO"].ToString(), dr["ACTCOD"].ToString(), sday);


            //here I call the pdffooter passing argument
            writer.PageEvent = new PDFFooter(CARDNO, CREDIT_LIMIT, ACTITLE, ADD1, ADD2, AVAILABLE_LIMIT, AVAILABLE_CASH_LIMIT, STATEMENT_DATE, DUE_DATE, MIN_DUE, outstanding);
            doc.Open();

            int stmntcnt = dtStatement.Rows.Count;
            int flag = 0;
            //double sum = 0;
            foreach (DataRow dt in dtStatement.Rows)
            {


                    ..........
        //pdf design between header and footer here
                    //////

                doc.Add(table);


            }

        }



    }
    catch (Exception ex)
    {

    }

}

public class PDFFooter : PdfPageEventHelper
{

    public string CARDNO { get; set; }
    public string ACTITLE { get; set; }
    public string ADD1 { get; set; }
    public string ADD2 { get; set; }
    public string CREDIT_LIMIT { get; set; }
    public string AVAILABLE_LIMIT { get; set; }
    public string AVAILABLE_CASH_LIMIT { get; set; }
    public string STATEMENT_DATE { get; set; }
    public string DUE_DATE { get; set; }
    public string MIN_DATE { get; set; }
    public string outstanding { get; set; }

    public PDFFooter(string CARDNO, string CREDIT_LIMIT, string ACTITLE, string ADD1, string ADD2, string AVAILABLE_LIMIT, string AVAILABLE_CASH_LIMIT, string STATEMENT_DATE, string DUE_DATE, string MIN_DATE, string outstanding)
    {

        this.CARDNO = CARDNO;
        this.CREDIT_LIMIT = CREDIT_LIMIT;
        this.ACTITLE = ACTITLE;
        this.ADD1 = ADD1;
        this.ADD2 = ADD2;
        this.AVAILABLE_LIMIT = AVAILABLE_LIMIT;
        this.AVAILABLE_CASH_LIMIT = AVAILABLE_CASH_LIMIT;
        this.STATEMENT_DATE = STATEMENT_DATE;
        this.DUE_DATE = DUE_DATE;
        this.MIN_DATE = MIN_DATE;
        this.outstanding = outstanding;

    }

    // write on top of document
    public override void OnOpenDocument(PdfWriter writer, Document document)
    {
        base.OnStartPage(writer, document);
    }

    // write on start of each page
    public override void OnStartPage(PdfWriter writer, Document document)
    {

        base.OnOpenDocument(writer, document);
            ////  --------------------------------------------Header Part--------------------------------------- ////



    }

    // write on end of each page
    public override void OnEndPage(PdfWriter writer, Document document)
    {
            ////  --------------------------------------------Footer Part--------------------------------------- ////
    }

    //write on close of document
    public override void OnCloseDocument(PdfWriter writer, Document document)
    {

    }
}
Gericke
  • 2,109
  • 9
  • 42
  • 71
  • 1
    **A** The iText mantra: Thou shall not add any content in `OnStartPage`, add content only in `OnEndPage`. **B** Why do you call `base.OnStartPage` in `OnOpenDocument`, and `base.OnOpenDocument` in `OnStartPage`? **C** Your `PDFFooter` class has setters and getters for the building blocks of headers and footers. How about setting new information when starting a new section? – mkl Jul 13 '15 at 11:10
  • 1
    @mkl **D** read the [documentation](http://pages.itextpdf.com/ebook-stackoverflow-questions.html) to discover that the question has been answered before: [How to generate a report with dynamic header in PDF using itextsharp?](http://stackoverflow.com/questions/21628429/itextsharp-how-to-generate-a-report-with-dynamic-header-in-pdf-using-itextsharp) – Bruno Lowagie Jul 13 '15 at 14:11
  • @BrunoLowagie Indeed. But in this case the OP already has everything he needs, he does have a page event listener with setters for the relevant information, and he still asks how to do it... – mkl Jul 13 '15 at 15:30
  • @mkl The OP isn't a developer. He is a copy/paste artist. However, if he reads the documentation, there is a chance that he'll start thinking as a developer. – Bruno Lowagie Jul 13 '15 at 15:32

0 Answers0