Skip to content
Loading
  • +1
    I try this...in line 13 ..
     
    Error 1: cannot convert from object to float
    Error 2:  cannot convert from ItextSharp.text.Font to ITextSharp.text.Chunk
     
    1. foreach (DataGridViewRow row in mp_faktura_listaDataGridView.Rows)  
    2.             {  
    3.   
    4.                 foreach (DataGridViewCell cell in row.Cells)  
    5.                 {  
    6.                     if (cell.ColumnIndex == 2) // change 2 wiyh your datetime column    
    7.                     {  
    8.                         string dateVal = Convert.ToDateTime(cell.Value).ToString("dd-MM-yyyy"); PdfPCell cell2 = new PdfPCell(new Phrase(dateVal, calibri));  
    9.                     }  
    10.                     
    11.                      else  
    12.                     {  
    13.                         PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value, calibri));  
    14.                         pdfTable.AddCell(cell2);  
    15.                     }  
    16.   
    17.               
    18.         } 
     
    +1
  • Amit Gupta
    +1
    Look at the line no 36. You haven't​ applied the code that I had posted. 
     
    Please apply the code before asking that something is not working
    +1
  • 0
    Error is
     
    ,,Invalid cast Int32 to DateTime,,
     
    Have couple columns...not only one...
    0
  • 0
    Reslut is... in line 13 need ToString() and open pdf document
    Column are mixed and datetimevalue is same  15.04.2018. 00:00:00
     
    No have changes
    0
  • 0
    No have solution?
    0
  • 0
    Complete code for pdf click
    1. private void button9_Click(object sender, EventArgs e)  
    2.         {  
    3.             //Creating iTextSharp Table from the DataTable data  
    4.             //Console.WriteLine(drzaveDataGridView.ColumnCount); test za itext sharp  
    5.             PdfPTable pdfTable = new PdfPTable(mp_faktura_listaDataGridView.ColumnCount);  
    6.             pdfTable.DefaultCell.Padding = 3;  
    7.             pdfTable.WidthPercentage = 100;  
    8.             pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;  
    9.             pdfTable.DefaultCell.BorderWidth = 1;  
    10.             pdfTable.DefaultCell.BorderWidth = 1;  
    11.             float[] sirina = new float[] { 0f, 15f, 40f, 70f, 25f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f };  
    12.             pdfTable.SetWidths(sirina);  
    13.             BaseFont bfCalibri = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);  
    14.             iTextSharp.text.Font calibri = new iTextSharp.text.Font(bfCalibri, 9);  
    15.             iTextSharp.text.Font calibri10bold3 = new iTextSharp.text.Font(bfCalibri, 9, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.WHITE);  
    16.   
    17.             //Adding Header row  
    18.             foreach (DataGridViewColumn column in mp_faktura_listaDataGridView.Columns)  
    19.             {
    20.                     PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, calibri10bold3));  
    21.                 cell.BackgroundColor = new iTextSharp.text.BaseColor(243, 70, 5);  
    22.                 pdfTable.AddCell(cell);     
    23.             }
    24.             //Adding DataRow  
    25.             foreach (DataGridViewRow row in mp_faktura_listaDataGridView.Rows)  
    26.             {
    27.                 foreach (DataGridViewCell cell in row.Cells)  
    28.                 {           
    29.                     PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString(), calibri));  
    30.                         pdfTable.AddCell(cell2);  
    31.                 }
    32.             }
    33.     string folderPath = "C:\\PDFs\\";  
    34.             if (!Directory.Exists(folderPath))  
    35.             {  
    36.                 Directory.CreateDirectory(folderPath);  
    37.             }  
    38.             using (FileStream stream = new FileStream(folderPath + "Lista MP faktura.pdf", FileMode.Create))  
    39.             {  
    40.                                 iTextSharp.text.Font calibriTitle = new iTextSharp.text.Font(bfCalibri, 14, iTextSharp.text.Font.BOLD);  
    41. iTextSharp.text.Font calibriSubTitle = new iTextSharp.text.Font(bfCalibri, 12);  
    42. iTextSharp.text.Font calibriSubTitlebold = new iTextSharp.text.Font(bfCalibri, 14, iTextSharp.text.Font.BOLD);  
    43. iTextSharp.text.Font calibri10bold = new iTextSharp.text.Font(bfCalibri, 10, iTextSharp.text.Font.BOLD);  
    44. iTextSharp.text.Font calibri22bold = new iTextSharp.text.Font(bfCalibri, 22, iTextSharp.text.Font.BOLDITALIC);  
    45. iTextSharp.text.Font calibrinaslov = new iTextSharp.text.Font(bfCalibri, 16);  
    46. iTextSharp.text.Font calibriukupno = new iTextSharp.text.Font(bfCalibri, 12, iTextSharp.text.Font.BOLDITALIC);  
    47. Document pdfDoc = new Document(PageSize.A4, 27f, 10f, 10f, 30f);  
    48. // PdfWriter.GetInstance(pdfDoc, stream);  
    49. PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);  
    50. pdfDoc.Open();  
    51.                                 pdfDoc.Add(new ListItem(korisnik_programa, calibriTitle));  
    52.                                 pdfDoc.Add(new ListItem(adresa, calibriSubTitle));  
    53.                                 pdfDoc.Add(new ListItem(mesto, calibriSubTitle));  
    54.                                 pdfDoc.Add(new ListItem("JIB:" + " " + jib2, calibriSubTitle));  
    55.                                 pdfDoc.Add(new ListItem("PIB:" + " " + pib2, calibriSubTitle));  
    56.                                 pdfDoc.Add(new ListItem("Žiro-racun:" + " " + ziro_racun2, calibriSubTitle));  
    57.                                 pdfDoc.Add(new ListItem("E-mail:" + " " + e_mail, calibriSubTitle));  
    58.                                 pdfDoc.Add(new ListItem(" "));  
    59.                                 // pdfDoc.Add(new ListItem(" "));  
    60.                                 pdfDoc.Add(new ListItem("                                                    Lista maloprodajnih faktura ", calibrinaslov));  
    61.                                 pdfDoc.Add(new ListItem(" "));  
    62.                                 // pdfDoc.Add(new ListItem(" "));  
    63.                                 pdfDoc.Add(new ListItem(" "));  
    64.                                 PdfContentByte cb = writer.DirectContent;  
    65. PdfContentByte cb1 = writer.DirectContent;  
    66. cb.SetColorStroke(new BaseColor(41, 128, 185));// boja linija 
    67.                                 iTextSharp.text.Rectangle fakturisao = new iTextSharp.text.Rectangle(pdfDoc.PageSize.Width - 140f, 120f, pdfDoc.PageSize.Width - 520f, 50f);  
    68. iTextSharp.text.Rectangle primio = new iTextSharp.text.Rectangle(pdfDoc.PageSize.Width - 140f, 120f, pdfDoc.PageSize.Width - 60f, 50f);  
    69. iTextSharp.text.Rectangle otpremio = new iTextSharp.text.Rectangle(pdfDoc.PageSize.Width - 140f, 120f, pdfDoc.PageSize.Width - 330f, 50f);  
    70. iTextSharp.text.Rectangle ukupno_za_uplatu = new iTextSharp.text.Rectangle(pdfDoc.PageSize.Width - 140f, 120f, pdfDoc.PageSize.Width - 462f, 590f);  
    71. //     iTextSharp.text.Rectangle slogan_korisnika2 = new iTextSharp.text.Rectangle(pdfDoc.PageSize.Width - 340f, 10f, pdfDoc.PageSize.Width - 20f, 10f);  
    72. //cb.SetColorStroke(new CMYKColor(1f, 0f, 0f, 0f));// boja linija  
    73. // cb.SetColorStroke(new BaseColor(41,128,185));// boja linija  
    74. // cb.SetColorFill(new CMYKColor(0f, 0f, 1f, 0f));  
    75. cb1.SetLineWidth(2);  
    76.                                 cb1.RoundRectangle(20f, 691f, 560f, 140f, 10f); // zaglavlje rectangle  
    77.                                 cb1.Stroke();  
    78.                                 cb.SetLineWidth(1);  
    79.                                 cb.MoveTo(20f, 681f);  //linija gornja zaglavlje
    80.                                 cb.LineTo(580f, 681f);  
    81.                                 cb.Stroke();
    82.                                 cb.SetLineWidth(1);  
    83.                                 cb.MoveTo(20f, 652f); // linija donja zaglavlje
    84.                                 cb.LineTo(580f, 652f);  
    85.                                 cb.Stroke();
    86.                                 cb.SetLineWidth(0.5);  
    87.                                 cb.MoveTo(20f, 20f); // linija donja slogan firme  
    88.                                 cb.LineTo(585f, 20f);  
    89.                                 cb.Stroke();
    90.                                 pdfDoc.Add(pdfTable);
    91.                                 Paragraph par = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 40.0F, BaseColor.BLACK, Element.ALIGN_RIGHT, 1)));  
    92. pdfDoc.Add(par);
    93.                                 double[] columnData = new double[mp_faktura_listaDataGridView.Rows.Count];  
    94. columnData = (from DataGridViewRow row in mp_faktura_listaDataGridView.Rows  
    95.               where row.Cells["iznos_sa_pdv"].FormattedValue.ToString() != string.Empty  
    96.               select Convert.ToDouble(row.Cells["iznos_sa_pdv"].FormattedValue)).ToArray();  
    97. label16.Text = columnData.Sum().ToString("#,0.00");  
    98.   
    99. par = new Paragraph("UKUPNO:   " + label16.Text + "   KM", calibriukupno);  
    100. par.Alignment = Element.ALIGN_RIGHT;  
    101.                                 pdfDoc.Add(par);  
    102.                                 cb.SetLineWidth(0.5);  
    103.                                 cb.MoveTo(20f, 20f); // linija donja slogan firme  
    104.                                 cb.LineTo(585f, 20f);  
    105.                                 cb.Stroke();  
    106.                                 //     cb.SetLineWidth(1.5);  
    107.                                 //   cb.MoveTo(5f, 5f);   //linija korisnik programa  
    108.                                 //   cb.LineTo(5f, 820f);  
    109.                                 //   cb.Stroke();  
    110.   
    111.                                 pdfDoc.Close();  
    112.                 stream.Close();  
    113.                 System.Diagnostics.Process.Start(folderPath + "Lista MP faktura.pdf");  
    114.   
    115.                 }  
    116.             }  
    117.             }  
    118.                 }  
    119.             }
    120.         }
    0
  • 0
    Amit my dear friend
     
    1. //Adding DataRow  
    2.            foreach (DataGridViewRow row in mp_faktura_listaDataGridView.Rows)  
    3.            {  
    4.   
    5.                foreach (DataGridViewCell cell in row.Cells)  
    6.                {  
    7.   
    8.   
    9.                    //PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString(), calibri));  
    10.                    //    pdfTable.AddCell(cell2);  
    11.   
    12.                    if (cell.ColumnIndex == 2) // change 2 wiyh your datetime column    
    13.                    {  
    14.                        string dateVal = Convert.ToDateTime(cell.Value).ToString("dd-MM-yyyy"); PdfPCell cell2 = new PdfPCell(new Phrase(dateVal, calibri));  
    15.                    }  
    16.                   
    17.                    else  
    18.                    {  
    19.                        PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString(), calibri));  
    20.                    }  
    21.                }  
    22.      
    23.   
    24.    } 
     
    0