Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void GenerateLabelTable(List<Label> generatedLabels, Document doc)
- {
- doc.Open();
- iTextSharp.text.Font font = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 8.4f);
- iTextSharp.text.Font font2 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 8.4f, iTextSharp.text.Font.BOLD);
- iTextSharp.text.Font font3 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 11.5f, iTextSharp.text.Font.BOLD);
- float labelqrcode = 190;
- // Determine the number of rows and columns needed for the label grid
- int rows = (int)Math.Ceiling((double)generatedLabels.Count / 3);
- int cols = Math.Min(7, 3); // limit to a maximum of 7 columns
- int maxLabelsPerPage = 21; // set maximum number of labels per page
- //funcao gerar as tabelas e cells das etiquetas
- for (int i = 0; i < rows; i++)
- {
- PdfPTable table = new PdfPTable(cols);
- table.WidthPercentage = 100;
- // Create a new table within the main table.
- PdfPTable smallTable = new PdfPTable(1);
- // Set the width and height of the small table.
- smallTable.WidthPercentage = 30;
- //smallTable.AddCell("This is a cell in the small table.");
- for (int j = 0; j < cols; j++)
- {
- // Calculate the index of the current label in the labels list
- int index = i * 3 + j;
- if (index < generatedLabels.Count && index < (i + 1) * maxLabelsPerPage)
- {
- Label label = generatedLabels[index];
- // Create a new paragraph with the label text
- Paragraph p = new Paragraph(label.LabelText, font3);
- p.Font.Color = label.FontColor;
- p.SpacingBefore = -15f;
- //p.SpacingAfter = -5f;
- // Create a new paragraph with the label text
- Paragraph p2 = new Paragraph(label.LabelText2, font2);
- p2.Font.Color = label.FontColor;
- p2.SpacingBefore = -2f;
- p2.SpacingAfter = -2f;
- // Create a new paragraph with the label text
- Paragraph p3 = new Paragraph(label.LabelText3, font);
- p3.Font.Color = label.FontColor;
- p3.SpacingBefore = -1f;
- p3.SpacingAfter = -2f;
- // Create a new paragraph with the label text
- Paragraph p4 = new Paragraph(label.LabelText4, font2);
- p4.Font.Color = label.FontColor;
- p4.SpacingBefore = -1.5f;
- p4.SpacingAfter = -2f;
- // Create a new paragraph with the label text
- Paragraph p5 = new Paragraph(label.LabelText5, font);
- p5.Font.Color = label.FontColor;
- p5.SpacingBefore = -1f;
- p5.SpacingAfter = -1f;
- // Create a new paragraph with the label text
- Paragraph p6 = new Paragraph(label.LabelText6, font2);
- p6.Font.Color = label.FontColor;
- p6.SpacingBefore = -1f; //mexer aqui para mexer o qrcode para cima
- p6.SpacingAfter = -4f;
- // Create a new paragraph with the label text
- Paragraph p7 = new Paragraph(label.LabelText7, font);
- p7.Font.Color = label.FontColor;
- p7.SpacingBefore = 1f;
- p7.SpacingAfter = -1f;
- // Create a new paragraph with the label text
- Paragraph p8 = new Paragraph(label.LabelText8, font2);
- p8.Font.Color = label.FontColor;
- p8.SpacingBefore = -2f;
- p8.SpacingAfter = -1f;
- // Create a new paragraph with the label text
- Paragraph p9 = new Paragraph(label.LabelText9, font);
- p9.Font.Color = label.FontColor;
- p9.SpacingBefore = -17f;
- p9.SpacingAfter = -1f;
- // Create a new paragraph
- Paragraph p98 = new Paragraph();
- // Create a chunk containing the QR code and add it to the paragraph
- iTextSharp.text.Image qrImage = iTextSharp.text.Image.GetInstance(label.QRCodeImage, System.Drawing.Imaging.ImageFormat.Jpeg);
- //qrImage.ScaleToFit(60f, 60f);
- Chunk qrChunk = new Chunk(qrImage, (labelqrcode - 50), -10);
- qrImage.ScaleToFit(50f, 50f);
- qrChunk.Font.Color = label.FontColor;
- p98.Add(qrChunk);
- // smallTable.AddCell("qrimage");
- // Create a new cell and add the paragraph to it
- PdfPCell cell = new PdfPCell();
- cell.AddElement(p99);
- cell.AddElement(p);
- cell.AddElement(p2);
- cell.AddElement(p3);
- cell.AddElement(p4);
- cell.AddElement(p5);
- cell.AddElement(p6);
- cell.AddElement(p7);
- cell.AddElement(p8);
- cell.AddElement(p98);
- cell.AddElement(p9);
- // Position the small table in the bottom right corner of the main table.
- //PdfPCell cell1 = new PdfPCell(smallTable);
- //cell1.AddElement(p98);
- cell.FixedHeight = 120f;
- cell.HorizontalAlignment = Element.ALIGN_LEFT;
- cell.VerticalAlignment = Element.ALIGN_LEFT;
- table.AddCell(cell);
- //table.AddCell(cell1);
- }
- else
- {
- // Add an empty cell if there are no more labels
- PdfPCell emptyCell = new PdfPCell();
- emptyCell.FixedHeight = 120;
- emptyCell.HorizontalAlignment = Element.ALIGN_LEFT;
- emptyCell.VerticalAlignment = Element.ALIGN_LEFT;
- emptyCell.Border = 0;
- table.AddCell(emptyCell);
- }
- }
- // Add the current row of labels to the document
- doc.Add(table);
- }
- doc.Close();
- }
Advertisement
Add Comment
Please, Sign In to add comment