条码生成完美,但是当我打印2条或更多条码时,它会将每个条码打印在另一页上

问题描述 投票:0回答:1

我正在一个产品中注册了ProductCode的项目中,如果用户生成单个产品的2个或更多条形码,那么它会生成pdf,而每行只有1个条形码,我想使它出现,单页

Document doc = new Document(new iTextSharp.text.Rectangle(25, 11), 5, 8, 1, 1);
            int count = int.Parse(metroTextBox11.Text);
                PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/codes.pdf", FileMode.Create));
                doc.Open();
                DataTable dt = new DataTable();
                dt.Columns.Add("ID");
                dt.Columns.Add("Price");
                dt.Columns.Add("Des");
                for (int i = 0; i < count; i++)
                {
                    DataRow row = dt.NewRow();
                    row["ID"] = barcode;
                    row["Price"] = price;
                    row["Des"] = description;
                    dt.Rows.Add(row);
                }
                System.Drawing.Image img1 = null;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i != 0)
                        doc.NewPage();
                    PdfContentByte cb1 = writer.DirectContent;
                    BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_BOLDITALIC, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                    cb1.SetFontAndSize(bf, 2.0f);
                    cb1.BeginText();
                    cb1.SetTextMatrix(1.2f, 8.5f);
                    cb1.ShowText("Aaraiz");
                    cb1.EndText();
                    PdfContentByte cb2 = writer.DirectContent;
                    BaseFont bf1 = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                    cb2.SetFontAndSize(bf1, 1.0f);
                    cb2.BeginText();
                    cb2.SetTextMatrix(1.2f, 7.0f);
                    cb2.ShowText("Description: "+dt.Rows[i]["Price"].ToString());
                    cb2.EndText();
                    PdfContentByte cb = writer.DirectContentUnder;
                    Barcode128 bc = new Barcode128();
                    bc.TextAlignment = Element.ALIGN_CENTER;
                    bc.Font = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                    bc.Code = dt.Rows[i]["ID"].ToString();
                    bc.AltText = "Code: " + dt.Rows[i]["ID"].ToString() + "  Price: " + dt.Rows[i]["Price"].ToString();
                    bc.StartStopText = false;
                    bc.CodeType = iTextSharp.text.pdf.Barcode128.EAN13;
                    bc.Extended = true;
                    iTextSharp.text.Image img = bc.CreateImageWithBarcode(cb, iTextSharp.text.BaseColor.BLACK, iTextSharp.text.BaseColor.BLACK);
                    cb.SetTextMatrix(1.5f, 3.0f);
                    img.ScaleToFit(60, 5);
                    img.SetAbsolutePosition(1.5f, 1);
                    cb.AddImage(img);


                }
                doc.Close();
                System.Diagnostics.Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/codes.pdf");
            }
            catch(Exception error)
            {
                MessageBox.Show(error.ToString());
            }
            finally
            {
                doc.Close();
            }
c# winforms itext
1个回答
0
投票

这里有一些代码可以帮助您前进

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Data;
using System.IO;

namespace iTextBarcodes_58190058
{
    class Program
    {
        static void Main(string[] args)
        {
            doit();
        }

        private static void doit()
        {
            string savepath = "M:\\StackOverflowQuestionsAndAnswers\\iTextBarcodes_58190058\\iTextBarcodes_58190058\\bin\\Debug\\codes.pdf";
            string barcode = "blahlblahblah";
            string price = "0.95";
            string description = "more blah blah";
            //Document doc = new Document(new iTextSharp.text.Rectangle(25, 11), 5, 8, 1, 1);
            Document doc = new Document();
            int count = 20;
            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(savepath, FileMode.Create));
            doc.Open();
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Price");
            dt.Columns.Add("Des");
            for (int i = 0; i < count; i++)
            {
                DataRow row = dt.NewRow();
                row["ID"] = barcode  + " " + i;
                row["Price"] = (double.Parse(price) + i).ToString();
                row["Des"] = description + " " + i;
                dt.Rows.Add(row);
            }

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                doc.Add(new Paragraph("Aaraiz"));
                doc.Add(new Paragraph("Description: " + dt.Rows[i]["Price"].ToString()));

                PdfContentByte cb = writer.DirectContent;
                Barcode128 bc = new Barcode128();
                bc.TextAlignment = Element.ALIGN_CENTER;
                bc.Font = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                bc.Code = dt.Rows[i]["ID"].ToString();
                bc.AltText = "Code: " + dt.Rows[i]["ID"].ToString() + "  Price: " + dt.Rows[i]["Price"].ToString();
                bc.StartStopText = false;
                bc.CodeType = iTextSharp.text.pdf.Barcode128.EAN13;
                bc.Extended = true;
                iTextSharp.text.Image img = bc.CreateImageWithBarcode(cb, iTextSharp.text.BaseColor.BLACK, iTextSharp.text.BaseColor.BLACK);
                cb.SetTextMatrix(1.5f, 3.0f);
                img.ScaleToFit(120, 25);
                doc.Add(img);


            }
            doc.Close();
            System.Diagnostics.Process.Start(savepath);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.