Goran Bibic

Goran Bibic

  • 482
  • 2.9k
  • 197.8k

Itextsharp PDF c#

Apr 23 2018 4:26 AM
Datetime column format in pdf document
 
sql column is type date
  1. private void button2_Click(object sender, EventArgs e)  
  2.         {  
  3.              
  4.             PdfPTable pdfTable = new PdfPTable(testtableDataGridView.ColumnCount);  
  5.             pdfTable.DefaultCell.Padding = 3;  
  6.             pdfTable.WidthPercentage = 100;  
  7.             pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;  
  8.             pdfTable.DefaultCell.BorderWidth = 1;  
  9.             pdfTable.DefaultCell.BorderWidth = 1;  
  10.   
  11.             float[] sirina = new float[] { 50f, 90f, 45f, 40f, 40f };  
  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, 10);  
  15.   
  16.             //Adding Header row  
  17.             foreach (DataGridViewColumn column in testtableDataGridView.Columns)  
  18.             {  
  19.                 PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, calibri));  
  20.                 cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240);  
  21.                 pdfTable.AddCell(cell);  
  22.             }  
  23.   
  24.             //Adding DataRow  
  25.             foreach (DataGridViewRow row in testtableDataGridView.Rows)  
  26.             {  
  27.   
  28.                 foreach (DataGridViewCell cell in row.Cells)  
  29.                 {  
  30.                     string yourDateString = testtableDataGridView.Rows[row][dataGridViewTextBoxColumn12].ToString("dd/MM/yyyy");  
  31.                     PdfPCell cell2 = new PdfPCell(new Phrase(new Chunk(DateTime.Parse(yourDateString).ToShortDateString(), calibri)));  
  32.                  
  33.                     pdfTable.AddCell(cell2);  
  34.                 }  
  35.             }  
  36.   
  37.   
  38.             //Exporting to PDF  
  39.   
  40.             string folderPath = "C:\\PDFs\\";  
  41.             if (!Directory.Exists(folderPath))  
  42.             {  
  43.                 Directory.CreateDirectory(folderPath);  
  44.             }  
  45.             using (FileStream stream = new FileStream(folderPath + "Ulazni racun roba.pdf", FileMode.Create))  
  46.             {  
  47.                 iTextSharp.text.Font calibriTitle = new iTextSharp.text.Font(bfCalibri, 12);  
  48.                 iTextSharp.text.Font calibriSubTitle = new iTextSharp.text.Font(bfCalibri, 10);  
  49.                 Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);  
  50.                 PdfWriter.GetInstance(pdfDoc, stream);  
  51.                 pdfDoc.Open();  
  52.   
  53.                 pdfDoc.Add(pdfTable);  
  54.                 pdfDoc.Close();  
  55.                 stream.Close();  
  56.                 System.Diagnostics.Process.Start(folderPath + "Ulazni racun roba.pdf");  
  57.   
  58.             }  
  59.   
  60.         } 
 Error is line 30 Rows.row here...  Severity Code Description Project File Line Suppression State
Error CS1503 Argument 1: cannot convert from 'System.Windows.Forms.DataGridViewRow' to 'int'
  1. string yourDateString = testtableDataGridView.Rows[row][dataGridViewTextBoxColumn12].ToString("dd/MM/yyyy");

Answers (5)