使用C#中的itextsharp在一个pdf中使用不同的页眉页脚

时间:2015-07-13 05:48:56

标签: c# itextsharp

我有一张卡片清单,我必须为每张卡号创建声明。现在我为每个数字生成单独的pdf文件,每个数字都有不同的页眉页脚参数。现在我想在单个pdf文件中使用所有这些语句。但是如果我在循环中调用pdffooter,它就不起作用了。 如何在每个卡号的pdf中使用不同的页眉和页脚?

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)
    {

    }
}

0 个答案:

没有答案