无法扩大第二行的第一行高度

时间:2018-12-15 12:03:32

标签: c# wpf

我需要一次打印所有库存物品的条形码。对于打印,我已经通过单击按钮选择了所有项目,并使用DrawStringDrawImage函数进行了打印,该功能可以正常工作,但是如果项目名称太长,则问题是我的剪辑宽度为100px和文本调用了wrap和print函数,但是在打印第二行时,以Y点开头的项无法识别哪个会造成问题。如果我正确地确定了第一行的高度,那么我可以相应地设置第二行的Y坐标,这个问题可以解决,但是我不知道该怎么做。如您所见,结果如下图所示。

*问题出在这段代码中:

        if (numbarcode < NBbarcode_perLine)
            startX += 150;
        else
        {
            //MessageBox.Show(rectangleHeight.ToString());
            startX = 5;
            startY += 150; // space between 2 lines
            numbarcode = 0;
        }

enter image description here 代码:

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

            int NBbarcode_perLine = 5;
            int numbarcode = 0;

            int barcodePerPage = 35;
            int totalcodebar = listTobePrint.Count;

            for (int i = 0; i < barcodePerPage; i++)
            {

                String code = listTobePrint[countbarcode].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("Rs." + price, printFont, System.Drawing.Brushes.Black,
                    x4, y4, new StringFormat());

                // Measure string.
                SizeF stringSize = new SizeF();
                stringSize = e.Graphics.MeasureString(name, printFont);
                int width = (int)stringSize.Width;
                int height = (int)stringSize.Height;


                //MessageBox.Show(width.ToString() + ", Height: "+height);

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

                RectangleF rectangle = new RectangleF(x5, y5, 100, 0);
                int rectangleWidht = (int)rectangle.Width;
                int rectangleHeight = (int)rectangle.Height;

                //MessageBox.Show(rectangleHeight.ToString());

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

                numbarcode++;
                countbarcode++;


                if (numbarcode < NBbarcode_perLine)
                    startX += 150;
                else
                {
                    //MessageBox.Show(rectangleHeight.ToString());
                    startX = 5;
                    startY += 150; // space between 2 lines
                    numbarcode = 0;
                }


                if (countbarcode == totalcodebar) break;

                if (i == barcodePerPage - 1)
                {
                    e.HasMorePages = true;
                    return;
                }

            }
            countbarcode = 0;
            e.HasMorePages = false;
            db.DBClose();
        }

1 个答案:

答案 0 :(得分:0)

第一个解决方案是: 减少一行条形码,并增加两行之间的间距:(因为您有在页面底部放置剪切线的风险)

    int barcodePerPage = 30;//instead of 35

您会增加两行之间的间距

            if (numbarcode < NBbarcode_perLine)
                startX += 150;
            else
            {
                //MessageBox.Show(rectangleHeight.ToString());
                startX = 5;
                startY += 200; // ***** space between 2 lines increased
                numbarcode = 0;
            }

该解决方案的优势在于每页具有相同数量的条形码

另一种解决方案是计算下一个35个条形码的字符数,然后根据您选择的项目的字符数,选择第一种方式打印35个条形码,或者如果字符太长,则选择第二种方式打印30个条形码。如果你不知道那样做,我可以帮助你 该解决方案每页可能有不同数量的条形码

另一种解决方案是为该商品选择一种较小的字体(例如,arial 8),您可以赢得空间

相关问题