Goran Bibic

Goran Bibic

  • 480
  • 2.9k
  • 200.7k

Print multiple pages pdf itextsharp C#

Apr 29 2021 6:56 AM
My code is export data from data grid to one page, need to export multiple rows to each page
  1. private void PrintButton_Click(object sender, EventArgs e)      
  2. {      
  3.        
  4.     //Creating iTextSharp Table from the DataTable data      
  5.     //Console.WriteLine(PoreziDataGridView.ColumnCount); test za itext sharp      
  6.     PdfPTable pdfTable = new PdfPTable(ListDataGridView.ColumnCount);      
  7.     pdfTable.DefaultCell.Padding = 3;      
  8.     pdfTable.WidthPercentage = 100;      
  9.     pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;      
  10.     pdfTable.DefaultCell.BorderWidth = 1;      
  11.     pdfTable.DefaultCell.BorderWidth = 1;      
  12.   
  13.     float[] sirina = new float[] { 0f, 20f, 20f, 60f, 25f, 25f };      
  14.     pdfTable.SetWidths(sirina);      
  15.     BaseFont bfCalibri = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);      
  16.     iTextSharp.text.Font calibri = new iTextSharp.text.Font(bfCalibri, 8);      
  17.     iTextSharp.text.Font calibri2 = new iTextSharp.text.Font(bfCalibri, 8, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.WHITE);      
  18.     iTextSharp.text.Font calibri3 = new iTextSharp.text.Font(bfCalibri, 12);      
  19.   
  20.     //Adding Header row      
  21.     foreach (DataGridViewColumn column in ListDataGridView.Columns)      
  22.     {      
  23.         PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, calibri2));      
  24.         cell.HorizontalAlignment = Element.ALIGN_CENTER;      
  25.         cell.VerticalAlignment = Element.ALIGN_CENTER;      
  26.         cell.BackgroundColor = new iTextSharp.text.BaseColor(243, 70, 5);      
  27.         pdfTable.AddCell(cell);      
  28.     }      
  29.   
  30.     //Adding DataRow      
  31.     foreach (DataGridViewRow row in ListDataGridView.Rows)      
  32.     {      
  33.   
  34.         foreach (DataGridViewCell cell in row.Cells)      
  35.         {      
  36.             PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString(), calibri));      
  37.             pdfTable.AddCell(cell2);      
  38.         }      
  39.     }     
  40.     byte[] Memorandum = null;      
  41.   
  42.     using (SqlConnection openCon21 = new SqlConnection(Con))      
  43.     using (SqlCommand cmd21 = new SqlCommand())      
  44.     {      
  45.         cmd21.CommandText = "select memorandum from podaci_o_korisniku";      
  46.         cmd21.Connection = openCon21;      
  47.         openCon21.Open();      
  48.   
  49.         using (SqlDataReader reader21 = cmd21.ExecuteReader())      
  50.         {      
  51.             if (reader21.Read() && !reader21.IsDBNull(0))      
  52.             {      
  53.                 Memorandum = (byte[])reader21[0];      
  54.             }      
  55.             else      
  56.             {      
  57.                 Memorandum = null;      
  58.             }      
  59.         }      
  60.     }      
  61.   
  62.     if (Memorandum == null)      
  63.     {      
  64.         MessageBox.Show("Ucitajte zaglavlje kako biste odštampali dokument!" + "\n" + "Zaglavlje možete ucitati u:" + "\n" + "Šifarnici /1500 Osnovni podaci / 1501 Podaci o korisniku ""Informacija", MessageBoxButtons.OK, MessageBoxIcon.Information);      
  65.     }      
  66.     else      
  67.     {      
  68.         iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Memorandum);      
  69.   
  70.         //Exporting to PDF      
  71.   
  72.         string folderPath = "C:\\Program Files\\BSS\\PDFs\\";      
  73.         if (!Directory.Exists(folderPath))      
  74.         {      
  75.             Directory.CreateDirectory(folderPath);      
  76.         }      
  77.         using (FileStream stream = new FileStream(folderPath + "Porezi.pdf", FileMode.Create))      
  78.         {      
  79.                   
  80.           iTextSharp.text.Font calibriSubTitle = new iTextSharp.text.Font(bfCalibri, 10);      
  81.                  
  82.             iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(PageSize.A4, 20f, 15f, 10f, 30f);      
  83.                  
  84.             PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);      
  85.             pdfDoc.Open();      
  86.   
  87.             logo.ScaleAbsolute(550, 110);      
  88.             logo.SetAbsolutePosition(25, 715);      
  89.             pdfDoc.Add(logo);      
  90.   
  91.                  
  92.             pdfDoc.Add(new ListItem(" "));      
  93.             pdfDoc.Add(new ListItem("Porezi ", calibrinaslov));      
  94.             pdfDoc.Add(new ListItem(" "));      
  95.               
  96.             PdfContentByte cb = writer.DirectContent;      
  97.             PdfContentByte cb1 = writer.DirectContent;      
  98.             cb.SetColorStroke(new BaseColor(41, 128, 185));      
  99.   
  100.             cb1.SetLineWidth(2);      
  101.             cb1.RoundRectangle(20f, 711f, 560f, 120f, 10f);       
  102.             cb1.Stroke();      
  103.                  
  104.             pdfDoc.Add(pdfTable);      
  105.   
  106.             cb.SetColorStroke(new BaseColor(41, 128, 185));      
  107.             cb.SetLineWidth(0.5);      
  108.             cb.MoveTo(20f, 20f); // linija donja slogan firme      
  109.             cb.LineTo(585f, 20f);      
  110.             cb.Stroke(); 
  111.             pdfDoc.Close();      
  112.             stream.Close();      
  113.             System.Diagnostics.Process.Start(folderPath + "Porezi.pdf");      
  114.         }      
  115.     }      
  116. } 

Answers (2)