Hello
I create labels from app c#
Table is for 3 labels and count rows
If have 1or to row don't create document, error is doc no have pages
If have 3 or more rows work
PdfPTable pdfTable = new PdfPTable(3);
pdfTable.DefaultCell.Padding = 3;
pdfTable.WidthPercentage = 100;
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
pdfTable.DefaultCell.BorderWidth = 1;
BaseFont bfCalibri = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
iTextSharp.text.Font calibri = new iTextSharp.text.Font(bfCalibri, 12);
iTextSharp.text.Font calibri6 = new iTextSharp.text.Font(bfCalibri, 10);
//Adding DataRow
foreach (DataGridViewRow row in ItemsDataGridView.Rows)
{
Barcode128 code128 = new Barcode128();
code128.CodeType = Barcode.CODE128_RAW;
code128.ChecksumText = true;
code128.GenerateChecksum = true;
code128.Code = Barcode128.GetRawText(row.Cells[6].Value.ToString(), false, Barcode128.Barcode128CodeSet.AUTO);
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White));
iTextSharp.text.Image barCode = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Png);
PdfPCell pdfCell = new PdfPCell();
PdfPTable tmpTable = new PdfPTable(1);
tmpTable.WidthPercentage = 100;
PdfPCell tmpCell = new PdfPCell(barCode);
tmpTable.AddCell(new Paragraph("NIVELACIJA BROJ: " + RedniBrojListaTextBox.Text.ToString().ToUpper() + "/" + DateTime.Now.Year + " ", calibri6));
barCode.ScaleAbsolute(100, 40);
tmpCell.FixedHeight = 60;
tmpCell.HorizontalAlignment = Element.ALIGN_CENTER;
tmpCell.VerticalAlignment = Element.ALIGN_MIDDLE;
tmpCell.BorderWidth = 0;
tmpTable.AddCell(tmpCell);
tmpTable.DefaultCell.BorderWidth = 0;
tmpTable.DefaultCell.VerticalAlignment = Element.ALIGN_TOP;
tmpTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
tmpTable.AddCell(new Paragraph("ŠIFRA: " + row.Cells[2].Value.ToString().ToUpper(), calibri6));
tmpTable.AddCell(new Paragraph(row.Cells[3].Value.ToString().ToUpper(), calibri6));
tmpTable.AddCell(new Paragraph("CIJENA [SP]: " + row.Cells[6].Value.ToString().ToUpper() + " KM", calibri));
pdfCell.AddElement(tmpTable);
pdfTable.AddCell(pdfCell);
}
if (ItemsDataGridView.Rows.Count % 2 == 1)
{
pdfTable.AddCell(" ");
}
//else if (ItemsDataGridView.Rows.Count % 1 == 2)
//{
// pdfTable.AddCell("");
// pdfTable.AddCell("");
//}
//else if (ItemsDataGridView.Rows.Count % 3 == 0)
//{
// pdfTable.AddCell("");
// pdfTable.AddCell("");
// pdfTable.AddCell("");
//}
//
var tempFiles = new System.CodeDom.Compiler.TempFileCollection();
{
string file = tempFiles.AddExtension("pdf");
using (FileStream stream = File.OpenWrite(file))
{
var attributes = File.GetAttributes(file);
File.SetAttributes(file, attributes | FileAttributes.Temporary);
iTextSharp.text.Font calibriTitle = new iTextSharp.text.Font(bfCalibri, 18);
iTextSharp.text.Font calibriSubTitle = new iTextSharp.text.Font(bfCalibri, 14);
iTextSharp.text.Font calibriTextBold = new iTextSharp.text.Font(bfCalibri, 14, iTextSharp.text.Font.BOLD);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
pdfDoc.Add(pdfTable);
writer.CloseStream = false;
pdfDoc.Close();
stream.Close();
System.Diagnostics.Process p = System.Diagnostics.Process.Start(stream.Name);
}
}
}
Goran BibicPosted Mar 27, 2023, 5:35 AM
Yes I have, posted code have problem
If have 1 or two rows can't print labels
Write document no pages
Tuhin PaulPosted Mar 26, 2023, 3:35 AM
you are creating a PDF document with labels using iTextSharp library in C#.
If there is only one or two rows in the DataGridView, an error will occur because a document with no pages cannot be created. Therefore, you have added some code to handle these scenarios by adding an empty cell to the table. After creating the PDF document, you are opening it using the default PDF viewer on the user's system.
If you have any specific questions about the code or the process, please let me know.