create pdf RRS feed

  • Question

  • User1223895861 posted
    i have this code to create a pdf but its givin an erros saying table witdth must be grater than zero,  and also on the generated pdf sould show the items with the associated image, the items are ina  gridview and each item has an image. also a pagging option i want to create to if pagging is true it must generate the pdf with the actual gridview page else sould create a pdf with all items and ignore wich page are selected
    bool gerarpdf; 
        protected void btn_pdf_Click(object sender, ImageClickEventArgs e)  
            gerarpdf = true;  
        public override void VerifyRenderingInServerForm(Control control)  
            /* Verifies that the control is rendered */      
            /* Necessario para gerar pdf com gridview */     
        protected override void Render(HtmlTextWriter writer) 
            if (gerarpdf == true) 
                //pdf generation code called here        
                int columns = GridView2.Columns.Count; 
                // Table and PdfTable classes removed in version 5.XXX           
                iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(columns); 
                // padding can only be set for cells, __NOT__ PdfPTable object      
                int padding = 5; float[] widths = new float[columns]; for (int x = 0; x < columns; x++) 
                    widths[x] = (float)GridView2.Columns[x].ItemStyle.Width.Value; string cellText = Server.HtmlDecode(GridView2.HeaderRow.Cells[x].Text); 
                    // Cell and Color classes are gone too                      
                    iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new iTextSharp.text.Phrase(cellText)) { BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#008000")), Padding = padding }; table.AddCell(cell); 
                // next two lines set the table's __ABSOLUTE__ width      
                table.SetTotalWidth(widths); table.LockedWidth = true; for (int i = 0; i < columns; i++) { if (GridView2.Rows[i].RowType == DataControlRowType.DataRow) { for (int j = 0; j < columns; j++) { string cellText = Server.HtmlDecode(GridView2.Rows[i].Cells[j].Text); iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new iTextSharp.text.Phrase(cellText)) { Padding = padding }; if (i % 2 != 0) { cell.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#C2D69B")); } table.AddCell(cell); } } } Response.ContentType = "application/pdf"; iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 10f, 10f, 10f, 0f); iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); pdfDoc.Add(table); pdfDoc.Close(); Response.End(); 
                //let page render normally      
    my item template: (of image field)
           <a href='<%# Eval("URLImagem", "imagens/uploads/{0}") %>'  class='lightview' title='<%#Eval("Descricao") %>'> 
                           <asp:Image ID="Image1" runat="server" 
                        ImageAlign="Middle" ImageUrl='<%#Eval("URLImagem", "~/imagens/uploads/{0}") %>' Height="50px" 
                        Width="50px"   />
    i'm using ms access 2003 for database 
    Sunday, January 29, 2012 7:20 AM

All replies

  • User1223895861 posted

    please if someone can move this to the appropriate board, i post on wrong place

    Sunday, January 29, 2012 4:56 PM
  • User-1956819616 posted

    you should consider using "wkhtmltopdf" to create pdf from html document that you have.

    its a great tool

    Sunday, January 29, 2012 5:13 PM
  • User-1956819616 posted

    you shouls consider using wkhtmltopdf

    its a great tool

    Sunday, January 29, 2012 5:23 PM
  • User555306248 posted

    Hi, ItextShapt is well know free component used in c# you can download it from here 

    iTextSharp |Download iTextSharp software for free at SourceForge.net 

    C# Free Component to Generate PDF - Convert HTML to PDF - Code ...

     See how To  written tool in c# to generate PDF 

    Generate PDF Using C# - CodeProject

    Sunday, January 29, 2012 10:18 PM
  • User1223895861 posted

    i'm using itextsharp on the code above but on their version 5 they have change a lot of things and the examples i found on the internet i post also in the post because others are related with old versions if someone can understand why i gives the error and get a fix for it post please

    Monday, January 30, 2012 5:32 AM
  • User1223895861 posted

    someone can help me with this? thanks.

    i need to export the gridview data into the pdf

    Wednesday, February 1, 2012 6:33 AM
  • User1223895861 posted

    i'm using itextsharp, but why it gives that error?

    i need to export data into the pdf

    Thursday, February 2, 2012 5:57 AM