Goran Bibic

Goran Bibic

  • 482
  • 2.9k
  • 197.8k

Draw multiple rectangle with Itextsharp pdf C#

Apr 14 2020 3:57 AM
  1. private void PrintButton_Click(object sender, EventArgs e)  
  2.         {  
  3.             PdfPTable pdfTable = new PdfPTable(3);  
  4.             pdfTable.DefaultCell.Padding = 3;  
  5.             pdfTable.WidthPercentage = 100;  
  6.             pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;  
  7.             pdfTable.DefaultCell.BorderWidth = 1;  
  8.             BaseFont bfCalibri = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);  
  9.             iTextSharp.text.Font calibri = new iTextSharp.text.Font(bfCalibri, 12);  
  10.             iTextSharp.text.Font calibri6 = new iTextSharp.text.Font(bfCalibri, 10);  
  11.   
  12.             //Adding DataRow  
  13.             foreach (DataGridViewRow row in ListDataGridView.Rows)  
  14.             {  
  15.                 PdfPCell pdfCell = new PdfPCell();  
  16.                 PdfPTable tmpTable = new PdfPTable(1);  
  17.                 tmpTable.WidthPercentage = 100;                 
  18.                 PdfPCell tmpCell = new PdfPCell();  
  19.                  tmpCell.FixedHeight = 60;  
  20.                 tmpCell.HorizontalAlignment = Element.ALIGN_CENTER;  
  21.                 tmpCell.VerticalAlignment = Element.ALIGN_MIDDLE;  
  22.                 tmpCell.BorderWidth = 1;  
  23.                 //tmpTable.AddCell(tmpCell);  
  24.                 tmpTable.DefaultCell.BorderWidth = 0;  
  25.                 tmpTable.DefaultCell.VerticalAlignment = Element.ALIGN_TOP;  
  26.                 tmpTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;  
  27.                 tmpTable.AddCell(new Paragraph(row.Cells[4].Value.ToString().ToUpper(), calibri));  
  28.                 pdfCell.AddElement(tmpTable);  
  29.                 pdfTable.AddCell(pdfCell);  
  30.             }  
  31.             
  32.                      var tempFiles = new System.CodeDom.Compiler.TempFileCollection();  
  33.             {  
  34.                 string file = tempFiles.AddExtension("pdf");  
  35.                 using (FileStream stream = File.OpenWrite(file))  
  36.                 {  
  37.                     var attributes = File.GetAttributes(file);  
  38.                     File.SetAttributes(file, attributes | FileAttributes.Temporary);  
  39.                     iTextSharp.text.Font calibriTitle = new iTextSharp.text.Font(bfCalibri, 18);  
  40.                     iTextSharp.text.Font calibriSubTitle = new iTextSharp.text.Font(bfCalibri, 14);  
  41.                     iTextSharp.text.Font calibriTextBold = new iTextSharp.text.Font(bfCalibri, 14, iTextSharp.text.Font.BOLD);  
  42.                     Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);  
  43.                     PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);  
  44.                     pdfDoc.Open();  
  45.                     pdfDoc.Add(pdfTable);  
  46.                     writer.CloseStream = false;  
  47.                     pdfDoc.Close();  
  48.                     stream.Close();  
  49.                     System.Diagnostics.Process p = System.Diagnostics.Process.Start(stream.Name);  
  50.                      
  51.                 }  
  52.             }  
  53.         } 
 Result is
 
 
 
I have 3 row in my table.
 
I need next:
1) To be just selected row on print
 
 
2) To make some textbox and put inside some number exp 6 and to be on pdf 6 selected row boxex added
 

Answers (1)