在任何纸张尺寸上逐行打印多个条形码

时间:2018-11-17 16:07:10

标签: c# wpf

我想逐行打印多个库存项目的多个条形码。我写了一些代码,根据X,Y坐标进行了一些基本设置,效果很好。但是问题是,如下面的图片

所示,可打印条形码用完了纸

enter image description here

如上图所示,

最后一个条形码切开了它们的右侧,没有完整的宽度,如果我们有100个项目,那么我的代码仅打印5个条形码,其余的被跳过。请告诉您如何逐行打印100个项目的所有条形码。

 pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler(this.PrintBarcodeEvent_PrintPage);

                System.Windows.Forms.PrintDialog pdd = new System.Windows.Forms.PrintDialog();
                pdd.Document = pd;

                System.Windows.Forms.DialogResult result = pdd.ShowDialog();

                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    System.Windows.Forms.PrintPreviewDialog pp = new System.Windows.Forms.PrintPreviewDialog();
                    pp.Document = pd;
                    result = pp.ShowDialog();

                    //pd.Print();
                }

EventHandler:

 // The PrintPage event is raised for each page to be printed. 
        private void PrintBarcodeEvent_PrintPage(object sender, PrintPageEventArgs e)
        {
            int startX = 5;
            int startY = 5;

            Database db = new Database();
            db.DBOpen();

            for (int i = 0; i < listTobePrint.Count; i++)
            {
                String code = listTobePrint[i].Code;
                String name = db.GetByValue(Database.TABLE_ITEMS, Database.CODE_ITEMS, code, 2);
                String price = db.GetByValueForInt(Database.TABLE_ITEMS, Database.CODE_ITEMS, code, 8);

                Font printFont = new Font("Arial", 10.0f);

                e.Graphics.DrawString("Phulkari by VIRSA", printFont, System.Drawing.Brushes.Black,
                  startX, startY, new StringFormat());

                int x2 = startX + 3;
                int y2 = startY + 15;

                e.Graphics.DrawImage(Util.ImageWpfToGDI(Util.GenerateBarcode(code)), x2, y2, 100, 50);

                int x3 = startX;
                int y3 = y2 + 50;

                e.Graphics.DrawString(code, printFont, System.Drawing.Brushes.Black,
                    x3, y3, new StringFormat());

                int x4 = startX;
                int y4 = y3 + 15;

                e.Graphics.DrawString(name, printFont, System.Drawing.Brushes.Black,
                   x4, y4, new StringFormat());

                int x5 = startX;
                int y5 = y4 + 15;

                e.Graphics.DrawString("Rs." + price, printFont, System.Drawing.Brushes.Black,
                   x5, y5, new StringFormat());

                startX += 150;
                //startY += 100;
            }
            db.DBClose();
        }

注意:在上面的代码中,Util.ImageWpfToGDI(Util.GenerateBarcode(code)), x2, y2, 100, 50这一段代码将生成条形码。

1 个答案:

答案 0 :(得分:0)

在此解决方案中,您可以调整每行的条形码数量:

    private void PrintBarcodeEvent_PrintPage(object sender, PrintPageEventArgs e)
    {
        int startX = 5; int startY = 5;

        int NBbarcode_perLine = 5;
        int numbarcode = 0;

        Font printFont = new Font("Arial", 10.0f); // initialize just one time before loop

        Database db = new Database(); db.DBOpen();

        for (int i = 0; i < listTobePrint.Count; i++)
        {
            String code = listTobePrint[i].Code;
            String name = db.GetByValue(Database.TABLE_ITEMS, Database.CODE_ITEMS, code, 2);
            String price = db.GetByValueForInt(Database.TABLE_ITEMS, Database.CODE_ITEMS, code, 8);

            e.Graphics.DrawString("Phulkari by VIRSA", printFont, System.Drawing.Brushes.Black, startX, startY, new StringFormat());

            int x2 = startX + 3;
            int y2 = startY + 15;

            e.Graphics.DrawImage(Util.ImageWpfToGDI(Util.GenerateBarcode(code)), x2, y2, 100, 50);

            int x3 = startX;
            int y3 = y2 + 50;

            e.Graphics.DrawString(code, printFont, System.Drawing.Brushes.Black, x3, y3, new StringFormat());

            int x4 = startX;
            int y4 = y3 + 15;

            e.Graphics.DrawString(name, printFont, System.Drawing.Brushes.Black, x4, y4, new StringFormat());

            int x5 = startX;
            int y5 = y4 + 15;

            e.Graphics.DrawString("Rs." + price, printFont, System.Drawing.Brushes.Black, x5, y5, new StringFormat());

            numbarcode++;
            if (numbarcode < NBbarcode_perLine)
                startX += 150;
            else
            {
                startX = 5;
                startY += 100; // space between 2 barcode in vertical (upper left). you have to adjust)
                numbarcode = 0;
            }

        }
        db.DBClose();
    }