Guest User

Untitled

a guest
May 22nd, 2023
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. static void GenerateLabelTable(List<Label> generatedLabels, Document doc)
  2. {
  3. doc.Open();
  4.  
  5.  
  6.  
  7. iTextSharp.text.Font font = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 8.4f);
  8. iTextSharp.text.Font font2 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 8.4f, iTextSharp.text.Font.BOLD);
  9. iTextSharp.text.Font font3 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 11.5f, iTextSharp.text.Font.BOLD);
  10.  
  11.  
  12.  
  13. float labelqrcode = 190;
  14.  
  15.  
  16.  
  17. // Determine the number of rows and columns needed for the label grid
  18. int rows = (int)Math.Ceiling((double)generatedLabels.Count / 3);
  19. int cols = Math.Min(7, 3); // limit to a maximum of 7 columns
  20. int maxLabelsPerPage = 21; // set maximum number of labels per page
  21.  
  22. //funcao gerar as tabelas e cells das etiquetas
  23. for (int i = 0; i < rows; i++)
  24. {
  25. PdfPTable table = new PdfPTable(cols);
  26. table.WidthPercentage = 100;
  27.  
  28. // Create a new table within the main table.
  29. PdfPTable smallTable = new PdfPTable(1);
  30.  
  31. // Set the width and height of the small table.
  32. smallTable.WidthPercentage = 30;
  33.  
  34.  
  35. //smallTable.AddCell("This is a cell in the small table.");
  36.  
  37. for (int j = 0; j < cols; j++)
  38. {
  39. // Calculate the index of the current label in the labels list
  40. int index = i * 3 + j;
  41.  
  42. if (index < generatedLabels.Count && index < (i + 1) * maxLabelsPerPage)
  43. {
  44. Label label = generatedLabels[index];
  45. // Create a new paragraph with the label text
  46. Paragraph p = new Paragraph(label.LabelText, font3);
  47. p.Font.Color = label.FontColor;
  48. p.SpacingBefore = -15f;
  49. //p.SpacingAfter = -5f;
  50.  
  51. // Create a new paragraph with the label text
  52. Paragraph p2 = new Paragraph(label.LabelText2, font2);
  53. p2.Font.Color = label.FontColor;
  54. p2.SpacingBefore = -2f;
  55. p2.SpacingAfter = -2f;
  56. // Create a new paragraph with the label text
  57. Paragraph p3 = new Paragraph(label.LabelText3, font);
  58. p3.Font.Color = label.FontColor;
  59. p3.SpacingBefore = -1f;
  60. p3.SpacingAfter = -2f;
  61. // Create a new paragraph with the label text
  62. Paragraph p4 = new Paragraph(label.LabelText4, font2);
  63. p4.Font.Color = label.FontColor;
  64. p4.SpacingBefore = -1.5f;
  65. p4.SpacingAfter = -2f;
  66. // Create a new paragraph with the label text
  67. Paragraph p5 = new Paragraph(label.LabelText5, font);
  68. p5.Font.Color = label.FontColor;
  69. p5.SpacingBefore = -1f;
  70. p5.SpacingAfter = -1f;
  71. // Create a new paragraph with the label text
  72. Paragraph p6 = new Paragraph(label.LabelText6, font2);
  73. p6.Font.Color = label.FontColor;
  74. p6.SpacingBefore = -1f; //mexer aqui para mexer o qrcode para cima
  75. p6.SpacingAfter = -4f;
  76. // Create a new paragraph with the label text
  77. Paragraph p7 = new Paragraph(label.LabelText7, font);
  78. p7.Font.Color = label.FontColor;
  79. p7.SpacingBefore = 1f;
  80. p7.SpacingAfter = -1f;
  81. // Create a new paragraph with the label text
  82. Paragraph p8 = new Paragraph(label.LabelText8, font2);
  83. p8.Font.Color = label.FontColor;
  84. p8.SpacingBefore = -2f;
  85. p8.SpacingAfter = -1f;
  86.  
  87. // Create a new paragraph with the label text
  88. Paragraph p9 = new Paragraph(label.LabelText9, font);
  89.  
  90. p9.Font.Color = label.FontColor;
  91. p9.SpacingBefore = -17f;
  92. p9.SpacingAfter = -1f;
  93.  
  94. // Create a new paragraph
  95. Paragraph p98 = new Paragraph();
  96. // Create a chunk containing the QR code and add it to the paragraph
  97. iTextSharp.text.Image qrImage = iTextSharp.text.Image.GetInstance(label.QRCodeImage, System.Drawing.Imaging.ImageFormat.Jpeg);
  98. //qrImage.ScaleToFit(60f, 60f);
  99. Chunk qrChunk = new Chunk(qrImage, (labelqrcode - 50), -10);
  100. qrImage.ScaleToFit(50f, 50f);
  101. qrChunk.Font.Color = label.FontColor;
  102. p98.Add(qrChunk);
  103. // smallTable.AddCell("qrimage");
  104.  
  105. // Create a new cell and add the paragraph to it
  106. PdfPCell cell = new PdfPCell();
  107. cell.AddElement(p99);
  108. cell.AddElement(p);
  109. cell.AddElement(p2);
  110. cell.AddElement(p3);
  111. cell.AddElement(p4);
  112. cell.AddElement(p5);
  113. cell.AddElement(p6);
  114. cell.AddElement(p7);
  115. cell.AddElement(p8);
  116. cell.AddElement(p98);
  117. cell.AddElement(p9);
  118.  
  119. // Position the small table in the bottom right corner of the main table.
  120. //PdfPCell cell1 = new PdfPCell(smallTable);
  121. //cell1.AddElement(p98);
  122.  
  123. cell.FixedHeight = 120f;
  124. cell.HorizontalAlignment = Element.ALIGN_LEFT;
  125. cell.VerticalAlignment = Element.ALIGN_LEFT;
  126. table.AddCell(cell);
  127. //table.AddCell(cell1);
  128.  
  129. }
  130. else
  131. {
  132. // Add an empty cell if there are no more labels
  133. PdfPCell emptyCell = new PdfPCell();
  134. emptyCell.FixedHeight = 120;
  135. emptyCell.HorizontalAlignment = Element.ALIGN_LEFT;
  136. emptyCell.VerticalAlignment = Element.ALIGN_LEFT;
  137. emptyCell.Border = 0;
  138. table.AddCell(emptyCell);
  139. }
  140. }
  141. // Add the current row of labels to the document
  142. doc.Add(table);
  143. }
  144. doc.Close();
  145. }
Advertisement
Add Comment
Please, Sign In to add comment