添加页码itextsharp

时间:2014-01-07 04:34:22

标签: asp.net c#-4.0 itextsharp

我想将页码添加到itextsharp PDF文件的标题中。我在点击按钮时从html(asp.net repeater)生成pdf。我正在将数据提取到数据表中的PDF表格中。

protected void btnExportPdf_Click(object sender, EventArgs e)
    {
        BaseFont bf = BaseFont.CreateFont(Server.MapPath("~/Fonts/THSarabunNew.ttf"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        Font defaultFont = new Font(bf, 12);
        Font header = new Font(bf, 14, Font.BOLD);
        Font headTB = new Font(bf, 12, Font.BOLD);

        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment;filename=Report.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        //this.Page.RenderControl(hw); แปลงข้อมูลทั้งหน้าให้เป็น pdf

        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 25f, 25f, 15f, 15f); // left , right , top , bottom

        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

        PdfPTable PdfTable = new PdfPTable(GridView1.Columns.Count);
        PdfPCell PdfPCell = null;

        // Write Header ที่ PdfCell ใน PdfTable
        for (int column = 0; column < GridView1.Columns.Count; column++)
        {
            PdfPCell = new PdfPCell(new Phrase(new Chunk(GridView1.Columns[column].HeaderText.ToString(), headTB)));
            PdfPCell.Column.Alignment = Element.ALIGN_CENTER;
            PdfTable.AddCell(PdfPCell);
        }

        // Write Data ที่ PdfCell ใน PdfTable
        for (int row = 0; row < GridView1.Rows.Count; row++)
        {
            for (int col = 0; col < GridView1.Columns.Count; col++)
            {
                PdfPCell = new PdfPCell(new Phrase(new Chunk(GridView1.Rows[row].Cells[col].Text, defaultFont)));
                PdfTable.AddCell(PdfPCell);

            }
        }


        PdfTable.TotalWidth = 530f;
        PdfTable.LockedWidth = true;

        pdfDoc.Open();


        Paragraph head1 = new Paragraph(" รายงานข้อมูลการใช้งานรถยนต์ รายวัน ของ วันที่ " + System.DateTime.Now.ToString("dd/MM/yyyy"), header);
        head1.Alignment = Element.ALIGN_CENTER;
        pdfDoc.Add(head1);


        Paragraph head2 = new Paragraph(" บริษัท ซีพี รีเทลลิ้งค์ จำกัด ", header);
        head2.Alignment = Element.ALIGN_CENTER;
        pdfDoc.Add(head2);


        Paragraph head3 = new Paragraph("ของ ", header);
        head3.Alignment = Element.ALIGN_CENTER;
        pdfDoc.Add(head3);

        Paragraph HR = new Paragraph(" ", defaultFont);
        HR.Alignment = Element.ALIGN_CENTER;
        pdfDoc.Add(HR);

        pdfDoc.Add(PdfTable);

        Paragraph count = new Paragraph("จำนวนที่ได้ทำการอนุมัติทั้งหมด : " + GridView1.Rows.Count, defaultFont);
        count.Alignment = Element.ALIGN_RIGHT;
        pdfDoc.Add(count);

        //htmlparser.Parse(sr); 

        pdfDoc.Close();

        Response.Write(pdfDoc);
        Response.End();



    }

1 个答案:

答案 0 :(得分:0)

   I have added the footer by using following code in one of my project 

        MemoryStream mem = new MemoryStream();       // PDF data will be written here
        PdfWriter pdfwriter1 = PdfWriter.GetInstance(doc, mem);  //doc Document class Instance of itextsharp
        doc.Open();
        pdfwriter1.PageEvent = new Footer(category);
        doc.Add(table); // table is pregenrated PDFtable which i want to add in documnet
        doc.Close();


    so in your scenario use following line before **pdfDoc.Close();**
        PdfWriter pdfwriter1 = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfwriter1.PageEvent = new Footer(); // add you logic to show page number