Goran Bibic

Goran Bibic

  • 455
  • 2.9k
  • 178k

Pdf sum column from table itext sharp

Apr 8 2018 1:56 AM
Report Itextsharp pdf
The application is for record workers. I need to make a pdf report for all the workers listed in the table

Exp ... 
ROW 1   Worker1   coming time   out time   work  overtime
ROW 2   Worker2   coming time out time work overtime
ROW 3   Worker3   coming time out time work overtime
ROW 4   Worker3   coming time out time work overtime  
ROW 5   Worker2   coming time out time work overtime  
ROW 6   Worker1 coming time out time work overtime
ROW 7   Worker3 coming time out time work overtime
ROW 8   Worker3 coming time out time work overtime  
 
I need report 
 
1   Worker1    work ours sum     overtime sum
2   Worker2   work ours sum overtime sum
2    Worker3   work ours sum overtime sum 
 
  1. //Creating iTextSharp Table from the DataTable data  
  2.             PdfPTable pdfTable = new PdfPTable(prijava_radnikaDataGridView.ColumnCount - 5);  
  3.             pdfTable.DefaultCell.Padding = 3;  
  4.             pdfTable.WidthPercentage = 100;  
  5.             pdfTable.HorizontalAlignment = Element.ALIGN_CENTER;  
  6.             pdfTable.DefaultCell.BorderWidth = 1;  
  7.             float[] widths = new float[] { 18f, 40f, 35f, 35f, 25f, 25f };  
  8.             pdfTable.SetWidths(widths);  
  9.             BaseFont bfCalibri = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);  
  10.             iTextSharp.text.Font calibri = new iTextSharp.text.Font(bfCalibri, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.WHITE);  
  11.             iTextSharp.text.Font calibri2 = new iTextSharp.text.Font(bfCalibri, 9);  
  12.   
  13.             //Adding Header row  
  14.             foreach (DataGridViewColumn column in prijava_radnikaDataGridView.Columns)  
  15.             {  
  16.   
  17.   
  18.                 if (column.Index == 0 || column.Index == 5 || column.Index == 6 || column.Index == 7 || column.Index == 10)  
  19.                 {  
  20.                 }  
  21.                 else  
  22.                 {  
  23.                     PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, calibri));  
  24.                     cell.BackgroundColor = new BaseColor(89, 135, 178);  
  25.                     cell.VerticalAlignment = Element.ALIGN_MIDDLE;  
  26.                     pdfTable.AddCell(cell);  
  27.                 }  
  28.             }  
  29.   
  30.             //Adding DataRow  
  31.             foreach (DataGridViewRow row in prijava_radnikaDataGridView.Rows)  
  32.             {  
  33.                 foreach (DataGridViewCell cell in row.Cells)  
  34.                 {  
  35.                     if (cell.ColumnIndex == 0 || cell.ColumnIndex == 5 || cell.ColumnIndex == 6 || cell.ColumnIndex == 7 || cell.ColumnIndex == 10)  
  36.   
  37.                     {  
  38.                     }  
  39.   
  40.                     else  
  41.                     {  
  42.   
  43.                         pdfTable.AddCell(new PdfPCell(new Phrase(cell.Value.ToString(), calibri2)));  
  44.   
  45.                     }  
  46.                 }  
  47.             }  
  48.   
  49.   
  50.             //Exporting to PDF  
  51.   
  52.             string folderPath = "C:\\PDFs\\";  
  53.             if (!Directory.Exists(folderPath))  
  54.             {  
  55.                 Directory.CreateDirectory(folderPath);  
  56.             }  
  57.             string file = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".pdf";  
  58.             using (FileStream stream = File.OpenWrite(file))  
  59.   
  60.             {  
  61.                 var attributes = File.GetAttributes(file);  
  62.                 File.SetAttributes(file, attributes | FileAttributes.Temporary);  
  63.                 iTextSharp.text.Font calibriTitle = new iTextSharp.text.Font(bfCalibri, 16);  
  64.                 iTextSharp.text.Font calibriSubTitle = new iTextSharp.text.Font(bfCalibri, 12);  
  65.                 iTextSharp.text.Font calibriTextBold = new iTextSharp.text.Font(bfCalibri, 12, iTextSharp.text.Font.BOLD);  
  66.                 Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);  
  67.                 PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);  
  68.                 pdfDoc.Open();  
  69.                 pdfDoc.Add(new Paragraph("Company name", calibriTitle));  
  70.               
  71.                 pdfDoc.Add(new Paragraph(" ", calibriTitle));  
  72.                 pdfDoc.Add(new Paragraph("Evidencija radnika: " + textBox1.Text, calibriSubTitle));  
  73.                 if (checkBox1.Checked == true)  
  74.                 {  
  75.                     pdfDoc.Add(new Paragraph("Od " + dateTimePicker1.Value.ToString("dd.MM.yyyy.") + " do " + dateTimePicker2.Value.ToString("dd.MM.yyyy."), calibriSubTitle));  
  76.                 }  
  77.                 pdfDoc.Add(new Paragraph("\n", calibriSubTitle));  
  78.                 pdfDoc.Add(pdfTable);  
  79.   
  80.   
  81.                 Paragraph par = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 40.0F, BaseColor.BLACK, Element.ALIGN_RIGHT, 1)));  
  82.                 pdfDoc.Add(par);  
  83.                 par = new Paragraph("Sati: " + sum.ToString() + "\t  Prekovremeni: " + overtimeSum.ToString() + "\n");  
  84.                 par.Alignment = Element.ALIGN_RIGHT;  
  85.                 pdfDoc.Add(par);  
  86.                 par = new Paragraph("UKUPNO: " + (sum + overtimeSum).ToString() + " h", calibriTextBold);  
  87.                 par.Alignment = Element.ALIGN_RIGHT;  
  88.                 pdfDoc.Add(par);  
  89.   
  90.                 writer.CloseStream = false;  
  91.                 pdfDoc.Close();  
  92.                 stream.Close();  
  93.                 System.Diagnostics.Process p = System.Diagnostics.Process.Start(stream.Name);  
  94.   
  95.             }  
  96.   
  97.         }