Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to convert gridview to pdf form , that i have done it.
But now i want to add pagenumber at bottom of every page.
i have done this
C#
using (StringWriter sw = new StringWriter())
                {
                    using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                    {
                        HtmlForm hForm = new HtmlForm();
                        grd.AllowPaging = false;
                        grd.Parent.Controls.Add(hForm);
                       
                        hForm.Attributes["runat"] = "server";
                        hForm.Controls.Add(grdMessengerRequest);
                        hForm.RenderControl(hw);

                        StringReader sr = new StringReader(sw.ToString()); 
                        Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
                        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                       PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc,    Response.OutputStream);
                       pdfDoc.Open();

// at this place i have added table , which is displaying title
                       htmlparser.Parse(sr);
                       pdfDoc.Close();
}
}


Now , i have to spilt string reader / htmlworker , so i can add title to each n every page and page number of document too.
say looping for tile and page number.
so is there any solution , lte me know .
thank you.
Posted

fine here full code..
1st step : Assign DATAGRIDVIEW TABLE TO DATATABLE OBJECT DT..
After that change column names in follwing code..



string filename="TEST.PDF";
 Document document = new Document(new Rectangle(288f, 144f), 10, 10, 10, 10);
        document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());

        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));
        iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 11);
        
        document.Open();       
        PdfPTable table = new PdfPTable(dt.Columns.Count-2);

        PdfPRow row = null;
        float[] widths = new float[] { 4f, 4f,4f,4f,4f,4f,4f };
        table.SetWidths(widths);
        
        table.WidthPercentage = 100;  
        cell1.Colspan = dt.Columns.Count;
        cell1.Padding = 5;
        table.AddCell(cell1);

        PdfPCell cell;
        Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 15, Font.BOLD);
        Chunk chunkCols = new Chunk("Candidate's Interview List", ColFont);
        cell = new PdfPCell(new Paragraph(chunkCols));
        cell.Colspan = dt.Columns.Count;
        //cell.PaddingLeft = 10;
        cell.Padding = 5;
        table.AddCell(cell);

        PdfPCell cellH;
        Font ColFont3 = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD);
        Chunk chunkCols1 = new Chunk("Report From : " + (startdate == "01/01/0001" ? "-" : startdate) + "     To : " + (enddate == "12/31/9999" ? "-" : enddate), ColFont3);
        cellH = new PdfPCell(new Paragraph(chunkCols1));
        cellH.Colspan = dt.Columns.Count;
        //cell.PaddingLeft = 10;
        cellH.Padding = 5;
        table.AddCell(cellH);


        foreach (DataColumn c in dt.Columns)
        {
            if (c.ColumnName != "Recruiter Name" && c.ColumnName != "Type")
            {
                cell = new PdfPCell(new Paragraph(new Chunk(c.Caption, FontFactory.GetFont(FontFactory.HELVETICA, 11, Font.BOLD))));
                table.AddCell(cell);
            }
        }
        table.HeaderRows = 4;
        table.DefaultCell.BorderWidth = 1;
        DataTable uniqueCols = dt.DefaultView.ToTable(true, "Recruiter Name");
        Font ColFont1 = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD);
        if (uniqueCols.Rows.Count > 0)
        {
            for (int distRN = 0; distRN < uniqueCols.Rows.Count; distRN++)
            {
                string RecruiterName = uniqueCols.Rows[distRN]["Recruiter Name"].ToString();
                if (RecruiterName != "Not Available")
                {
                    DataRow[] dtDetails = dt.Select("[Recruiter Name] = '" + RecruiterName.Replace("'", "''") + "'");

                    Chunk chunkCols2 = new Chunk("Account Manager : " + RecruiterName, ColFont1);
                    cell = new PdfPCell(new Paragraph(chunkCols2));
                    cell.Colspan = dt.Columns.Count - 2;
                    //cell.PaddingLeft = 10;
                    cell.Padding = 5;
                    cell.MinimumHeight = 25f;
                    cell.Border = 0;
                    table.AddCell(cell);

                    foreach (DataRow r in dtDetails)
                    {

                        PdfPCell DateInterview = new PdfPCell(new Paragraph(r["Date of Interview"].ToString(), font5));
                        DateInterview.HorizontalAlignment = Element.ALIGN_LEFT;
                        table.AddCell(DateInterview);

                        PdfPCell CCandName = new PdfPCell(new Paragraph(r["Candidate Name"].ToString(), font5));
                        CCandName.HorizontalAlignment = Element.ALIGN_LEFT;
                        table.AddCell(CCandName);


                        PdfPCell CCompnyName = new PdfPCell(new Paragraph(r["Company Name"].ToString(), font5));
                        CCompnyName.HorizontalAlignment = Element.ALIGN_LEFT;
                        table.AddCell(CCompnyName);


                        PdfPCell CCompnyClintName = new PdfPCell(new Paragraph(r["Company Contact Name"].ToString(), font5));
                        CCompnyClintName.HorizontalAlignment = Element.ALIGN_LEFT;
                        table.AddCell(CCompnyClintName);


                        PdfPCell CTitle = new PdfPCell(new Paragraph(r["Title"].ToString(), font5));
                        CTitle.HorizontalAlignment = Element.ALIGN_LEFT;
                        table.AddCell(CTitle);


                        PdfPCell CSubject = new PdfPCell(new Paragraph(r["Subject"].ToString(), font5));
                        CSubject.HorizontalAlignment = Element.ALIGN_LEFT;
                        table.AddCell(CSubject);

                        PdfPCell DateAdded = new PdfPCell(new Paragraph(r["Date Added"].ToString(), font5));
                        DateAdded.HorizontalAlignment = Element.ALIGN_LEFT;
                        table.AddCell(DateAdded);
                    }
                }

            }       
        
        } 

        document.Add(table);
        Paragraph footer = new Paragraph("COMANY NAME", FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.NORMAL));
        footer.Alignment = Element.ALIGN_RIGHT;
        PdfPTable footerTbl = new PdfPTable(1);
        footerTbl.TotalWidth=1000;
        footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;
        PdfPCell cell2 = new PdfPCell(footer);
        cell2.Border = 0;
        cell2.PaddingLeft = 10;
        footerTbl.AddCell(cell2);
        footerTbl.WriteSelectedRows(0, -1, 550, 30, writer.DirectContent);
        document.Close();
 
Share this answer
 
Comments
thank you. but this is also useful for me.
This code helpfull for you...

  PdfPCell cell;
       Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 15, Font.BOLD);
       Chunk chunkCols = new Chunk("Candidate's Interview List", ColFont);
       cell = new PdfPCell(new Paragraph(chunkCols));
       cell.Colspan = dt.Columns.Count;
       //cell.PaddingLeft = 10;
       cell.Padding = 5;
       table.AddCell(cell);

table.HeaderRows = 4;


Thank you...
 
Share this answer
 
Hai..

Please find the following code.... SO you can add title to each n every page

 Document document = new Document(new Rectangle(288f, 144f), 10, 10, 10, 10);
       document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());

       PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));
       iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 11);

       document.Open();
       PdfPTable table = new PdfPTable(dt.Columns.Count-2);

       PdfPRow row = null;
       float[] widths = new float[] { 4f, 4f,4f,4f,4f,4f,4f };
       table.SetWidths(widths);

       table.WidthPercentage = 100;
       iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Server.MapPath("LOGO PATH"));
       PdfPCell cell1 = new PdfPCell(logo);
       cell1.Colspan = dt.Columns.Count;
       cell1.Padding = 5;
       table.AddCell(cell1);

       PdfPCell cell;
       Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 15, Font.BOLD);
       Chunk chunkCols = new Chunk("NAME OF THE HEADING", ColFont);
       cell = new PdfPCell(new Paragraph(chunkCols));
       cell.Colspan = dt.Columns.Count;
       //cell.PaddingLeft = 10;
       cell.Padding = 5;
       table.AddCell(cell);
table.HeaderRows = 4;
       table.DefaultCell.BorderWidth = 1;
 
Share this answer
 
v2
Comments
thank you but its not help full.
i want to add title and page in each and every page.
suppose in grid view i have 50 ros so 25 rows will be shown on page 1 and other 25 rows will be shown on page 2 , so i want to add title on both pages , same way want to show page number at bottom.
a.anvesh 20-May-14 5:38am    
PdfPCell cell;
Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 15, Font.BOLD);
Chunk chunkCols = new Chunk("NAME OF THE HEADING", ColFont);
cell = new PdfPCell(new Paragraph(chunkCols));
cell.Colspan = dt.Columns.Count;
//cell.PaddingLeft = 10;
cell.Padding = 5;
table.AddCell(cell);
table.HeaderRows = 4;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900