Click here to Skip to main content
15,850,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Code is down

What I have tried:

  private void EtiketeButton_Click(object sender, EventArgs e)
        {
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(" ");
            }
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);
                    //WorkPdfThread w = new WorkPdfThread(file, tempFiles);
                    //Thread newThread = new Thread(w.DoWork);
                    //newThread.Start();
                }
            }
        }
Posted
Comments
jsc42 23-Mar-23 6:04am    
I am guessing that your problem may be with the lines 'if (ItemsDataGridView.Rows.Count % 2 == 1) { pdfTable.AddCell(" "); }' which only add a cell if there are an odd number of rows. I suspect that you are looking for more than 2 rows e.g. 'if (ItemsDataGridView.Rows.Count > 2) { /* etc */}'. But, as I do not know anything about the PdfTable object, I could be completely wrong.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900