locked
itextsharp RRS feed

  • Question

  • User1382482195 posted

    i can get data with itextsharp just and empty grid.

    ineed that if shows in the pdf the associated image as i do in gridview see my code:

     

     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();
            }
    
    
            else
            {
    
                //let page render normally   
                base.Render(writer);
    
            }
        }
    }
    need with paggin option i found a demoe here but i dont know here to put it into my code http://archive.aspsnippets.com/demos/GridView2PDF.aspx

    what i need is sothinng similar to this but also with pagging http: //www.aspsnippets.com/Articles/Export-GridView-with-Images-to-Word-Excel-and-PDF-Formats-in-ASP.Net.aspx

    Tuesday, March 29, 2011 9:40 AM

Answers

  • User1508394307 posted

    It's up to you. I tested your code and found that if I use 5 as per your example, the text goes away. If I change to 1 or to 4 - it is visible. 

    Simply change it and see if it looks properly for you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 4, 2011 12:43 PM

All replies

  • User1508394307 posted

    I tested your code and it looks like it is due to 

    int padding = 5;

    If I change it to 1..4 I see the text. With 5 it disappears.

    Wednesday, June 1, 2011 5:15 PM
  • User1382482195 posted

    how do you change it to 1.4 if it dont accept decimal values?

    Saturday, June 4, 2011 11:06 AM
  • User1508394307 posted

    1..4 meaning 1 to 4, not 1.4

    Saturday, June 4, 2011 11:31 AM
  • User1382482195 posted

    you ar saying change padding to 4?

    Saturday, June 4, 2011 11:53 AM
  • User1508394307 posted

    It's up to you. I tested your code and found that if I use 5 as per your example, the text goes away. If I change to 1 or to 4 - it is visible. 

    Simply change it and see if it looks properly for you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 4, 2011 12:43 PM
  • User1382482195 posted

    i have an error that is easy fixed by setting the width because the error says table width must be grater than zero so i think this is not caculating the width so how could i set it to calculate it self than i fix manually

     

    but its strange why do not have width i just see this line in code:

    widths[x] = (float)GridView2.Columns[x].ItemStyle.Width.Value;

     

    edit: with autosize in column width it gets 0.0 of width so its why i get the error but how to fix it without putting the values?

    should i create a function to do he job and pass it to the avriable but how?

     

    also have found on another site how to set a column width n percentage but i dont know where to add it on my code.

    the code i found was

    /*
    * default table width => 80%
    */ 
      table.WidthPercentage = 100;
    // then set the column's __relative__ widths
      table.SetWidths(new Single[] {1, 5, 4});
    /*
    * by default tables 'collapse' on surrounding elements,
    * so you need to explicitly add spacing
    */
      table.SpacingBefore = 10;

      for (int i = 0; i < col.Length; ++i) {
        PdfPCell cell = new PdfPCell(new Phrase(col[i]));
        cell.BackgroundColor = new BaseColor(204, 204, 204);
        table.AddCell(cell);
      }

     

     

    for now i have set the width on itemstyle manually to 20% for each collumn (20%*5 collumns=100%)

    but i cant see nothing no data into it

    Saturday, June 4, 2011 6:03 PM
  • User1508394307 posted

    I would suggest to start from a simple code. Get rid of colors, width, paddings and other formatting. Be sure that the text goes to right cells and after that try to format the table.

    Sunday, June 5, 2011 11:53 AM