Goran Bibic

Goran Bibic

  • 474
  • 2.9k
  • 201.2k

Print 1 label from datagrid C#

Mar 23 2023 9:20 AM

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);

        }
    }
}

 


Answers (2)