系统绘图;e.HasMorePages问题

本文关键字:HasMorePages 问题 绘图 系统 | 更新日期: 2023-09-27 18:23:52

我花了几天时间试图解决C#中的这个大问题:我正在尝试打印21篇文章(账单)票证格式,但纸卷有限制,占用了几页的分隔打印,但我无法从一页跳到打印文章#17,并用#18继续到另一页,请帮助。。

private void DrawItems(System.Drawing.Printing.PrintPageEventArgs e)
        {
            int linesprinted = 0;
            int linesperpage = 17;
            int numberitems = items.Count; //21
            //numberitems / linespage = 1.23 = 2 Pages True :)
            if (linesprinted <= linesperpage)
            {
                linesprinted++;
                e.HasMorePages = false;
            }
            else {
                linesprinted=0;
                e.HasMorePages = true;
            }
//print items
            OrderItem ordIt = new OrderItem('?');
            gfx.DrawString("C/P   DESCRIPCION                  TOTAL", new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
            DrawEspacio();
            gfx.DrawString(DottedLine(), new Font(fontName, 9, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
            count++;
            foreach (string item in items)
            {
                String ItemCantidad = ordIt.GetItemCantidad(item);
                String ItemPrice = ordIt.GetItemPrice(item);
                Int16 not_equal = 0;

                gfx.DrawString(ItemCantidad + "  x", new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
                line = ordIt.GetItemUnitPrice(item);
                line = AlignRightText(line.Length) + line;
                gfx.DrawString("                 " + line, printFont, myBrush, leftMargin, YPosition(), new StringFormat());
                string name = ordIt.GetItemName(item);
                leftMargin = 0;
                if (name.Length > maxCharDescription)
                {
                    int currentChar = 0;
                    int itemLenght = name.Length;
                    while (itemLenght > maxCharDescription)
                    {
                        line = ordIt.GetItemName(item);
                        gfx.DrawString("         " + line.Substring(currentChar, maxCharDescription), new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
                        count++;
                        not_equal++;
                        if (not_equal == 1)
                        {
                            gfx.DrawString(ItemPrice, new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
                        }
                        currentChar += maxCharDescription;
                        itemLenght -= maxCharDescription;
                    }
                    line = ordIt.GetItemName(item);
                    gfx.DrawString("         " + line.Substring(currentChar, line.Length - currentChar), new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
                    count++;
                    gfx.DrawString("-----", new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
                    count++;
                }
                else
                {
                    gfx.DrawString("         " + ordIt.GetItemName(item), new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
                    count++;
                    gfx.DrawString(ItemPrice, new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
                    count++;
                    gfx.DrawString("-----", new Font(fontName, fontSize, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
                    count++;
                }
            } //end foreach

            leftMargin = 0;
            gfx.DrawString(DottedLine(), new Font(fontName, 9, FontStyle.Regular), myBrush, leftMargin, YPosition(), new StringFormat());
            DrawEspacio();
        } //end function

系统绘图;e.HasMorePages问题

我认为你做得不对。应该是这样的:

    private void MyPrintDocument_PrintPage(object sender,
        System.Drawing.Printing.PrintPageEventArgs e)
    {
        bool more = DrawItems(e.Graphics);
        if (more == true)
            e.HasMorePages = true;
    }

因此,在PrintDocumentPrint事件之后,您调用方法来绘制项目,并且它跟踪方法外部变量中最后绘制的项目,因此当再次调用它时,可以知道从哪里开始。当涉及到应该转到下一页的项目时,它返回true。