Click here to Skip to main content
16,013,338 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello

i want to print data grid, so i use itextsharp to print, which bring the desire data grid to PDF format .but i one problem occur ..the problem is that i have 14 columns in my data grid. but in PDF it will display all jumble(scatter where and here).
now i want all 14 colums should be display as it is in gridview with same format of data grid view .. and the code i have tried is
Document doc;
        public bool pdffunction(string sfilepdf)
        {
            bool check = false;
            doc = new Document();
            PdfWriter.GetInstance(doc, new FileStream(sfilepdf, FileMode.Create));
            doc.Open();
            return check;
        }
     
       int count;
        private void toolStripButton_Print_Click(object sender, EventArgs e)
        {
                BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
                iTextSharp.text.Font times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.ITALIC, iTextSharp.text.BaseColor.BLUE);
     
                SqlConnection con = new SqlConnection(str);
                con.Open();
                String query = "select [Title] as [Book Title],Type.TypeName as [Type],Category.Name as [category],ISBN,Author,[Publisher]as[Publisher Name],Edition,Quantity,[Requested Date]= CONVERT(char(10),Request_Book.EnteredDt,101),[Expected Date]= CONVERT(char(10),Request_Book.ExpectedDt,101),Request_Book.EnteredBy as [Requested By],Request_Book.Processed as [Status],Url from Request_Book,Type,Category where Type.TypeId = Request_Book.TypeId and Category.CategoryId = Request_Book.CategoryId";
                SqlCommand command = new SqlCommand(query, con);
                SqlDataAdapter dtadpter = new SqlDataAdapter(command);
                SqlDataReader dtreader = command.ExecuteReader();
                count = dtreader.FieldCount;
                string path = "D:/rabintestfile.pdf";
                pdffunction(path);

               
                doc.Add(new Paragraph("From:",times));
                doc.Add(new Paragraph(dateTimePicker1.Value.ToShortDateString()));
          
                doc.Add(new Paragraph("To:",times));
                doc.Add(new Paragraph(dateTimePicker2.Value.ToShortDateString()));
                doc.Add(new Paragraph(" "));

                PdfPTable table = new PdfPTable(count);
                PdfPCell cell = new PdfPCell(new Phrase("Request Book"));
                cell.Colspan = 1;
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);
                table.AddCell("S.No");
                table.AddCell("Book Title");

                table.AddCell("Type");

                table.AddCell("Category");

                table.AddCell("ISBN");

                table.AddCell("Author");

                table.AddCell("Quantity");

                table.AddCell("Publisher Name");

                table.AddCell("Edition");

                table.AddCell("Requested Date");

                table.AddCell("Expected Date");

                table.AddCell("Requested By");

                table.AddCell("Status");

                table.AddCell("Url");

                table.AddCell("");

                while (dtreader.Read() && dtreader.HasRows)
                {
                    for (int i = 0; i < count; i++)
                    {
                        table.AddCell(dtreader[i].ToString());
                    }
                }
                dtreader.Close();
                doc.Add(table);
                doc.Close();

                System.Diagnostics.Process.Start("D:/rabintestfile.pdf");

            }

Above code will sucessfully creates PDF but the value is jumbled up..

please help me out
Posted
Updated 8-Aug-11 21:16pm
v2
Comments
Christian Graus 9-Aug-11 3:15am    
Have you tried stepping through the code to see what it's doing wrong ? Is your data source as you expect ? Is the issue that your table.AddCell is called an inconsistent number of times ? What have you done to understand the issue ?

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