Visual Studio 使用e.HasMorePages打印新页面

w46czmvw  于 2023-10-23  发布在  其他
关注(0)|答案(2)|浏览(113)

我有点困惑如何使用HasMorePages属性。我尝试通过YPosition方法打印更多的页面,但它会导致打印页面的无限循环。
这是我的代码:

private float YPosition()
        {
            return this.TopMargin + ((float)this.LinesCount * this.Font.GetHeight(this.Graphics) + (float)this.ImagesHeight);
        }

        private void TicketPrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.PageUnit = GraphicsUnit.Millimeter;
            this.Graphics = e.Graphics;
            foreach (Tuple<Object, LineTypes> tuple in this.Objects)
            {
                switch (tuple.Item2)
                {
                    case LineTypes.LINE:
                        this.Graphics.DrawString((String)tuple.Item1, this.Font, this.SolidBrush, this.LeftMargin, this.YPosition(), new StringFormat());
                        this.LinesCount++;
                        break;
                    case LineTypes.IMAGE:
                        Image Image = (Image)tuple.Item1;

                        // Center Image, using PaperSize
                        Graphics GraphicsImage = Graphics.FromImage(Image);
                        RectangleF RectangleF = e.MarginBounds;
                        RectangleF.Offset(-e.PageSettings.HardMarginX, -e.PageSettings.HardMarginY);

                        float InchX = RectangleF.X / 100f + (RectangleF.Width / 100f - (float)Image.Width / GraphicsImage.DpiX) / 2f;
                        Int32 MillimeterX = (Int32)Math.Ceiling(InchX / 0.039370);

                        this.Graphics.DrawImage(Image, new Point((int)this.LeftMargin + (MillimeterX / 2), (int)this.YPosition()));
                        double a = (double)Image.Height / 58.0 * 15.0;
                        this.ImagesHeight += (int)Math.Round(a) + 3;
                        break;
                }
                if ((YPosition() * 4) >= e.PageSettings.PrintableArea.Height)
                {
                    e.HasMorePages = true;
                    return;
                }
                else
                {
                    e.HasMorePages = false;
                }
            }
        }

YPosition表示页面中每行或图像的高度。
如果处理了所有对象,我如何防止打印和停止的无休止循环?

w8f9ii69

w8f9ii691#

你需要使用while循环和Enumerator来代替for循环。枚举器保存您正在处理的对象的状态,该对象作为成员存储在表单示例上。使用PrintDocument类的BeginPrintEndPrint事件初始化和清理枚举器。

// this is the variable that holds the enumerator instance 
// once printing has started
IEnumerator<Tuple<Object, LineTypes>> ObjectsEnumerator;

// this is the event raised by printdocument at the start of printing
private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
    this.ObjectsEnumerator = Objects.GetEnumerator();
}

因为我们现在有了一个枚举器,PrintPage实现将使用它。它调用MoveNext并将结果存储在HasMorePages中。代码的其余部分与您之前的代码类似,但请确保将页面上位置的计算保持在该方法的本地。不要(ab)使用示例变量。请注意,对Current的调用是while循环中的第一条语句。

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    int linesCount = 0;
    float yPosition = 10;
    // take the next item from the list by calling MoveNext
    // and assign the outcome to e.HasMorePages
    while(e.HasMorePages = this.ObjectsEnumerator.MoveNext())
    {
        var tuple = this.ObjectsEnumerator.Current;
        switch (tuple.Item2)
        {
            case LineTypes.LINE:
                // draw magic
                e.Graphics.DrawString(tuple.Item1.ToString(), font, solidBrush, new PointF(10,yPosition));
                yPosition += 300;
                linesCount++;
                break;
            case LineTypes.IMAGE:
                Image Image = (Image)tuple.Item1;
                // e.Graphics.DrawImage(....);
                // calculate new yPosition
                yPosition += Image.Height;
                break;
        }
        // if we reach the end of the page
        if (yPosition >= e.PageSettings.PrintableArea.Height)
        {
            //we break out of the while loop
            // e.HasMorePages is already set
            break;
        }
    }
}

使用此代码,您可以遍历集合,如果页面已满,则中断循环,如果没有更多的项目要打印,则停止迭代和打印。
如果打印完成,Endprint被调用,它的任务是清理枚举器。

private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
    if (this.ObjectsEnumerator != null)
    {
        this.ObjectsEnumerator.Dispose();
        this.ObjectsEnumerator = null;
    }
}
kiz8lqtg

kiz8lqtg2#

修复在C#中使用HasMorePages打印时的分页问题。关于这段代码的解释:您可以添加许多行或从数据库中读取,以查看打印时分页的结果,例如,我只手动向表中添加了两行,因为行数很少,所以没有分页。手动添加多行或将数据库中的信息插入网格视图。这是一个例子:

int currentPage = 1;
int rowsPerPage = 36; //Number of rows per page
int rowIndex = 0;

void pdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    try
    {
        int x = 5, y = 5; //Drawing start position
        int rowMargin = 0; //Spacing between lines

        dgvHesaab.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
        dgvHesaab.DefaultCellStyle.WrapMode = DataGridViewTriState.True;

        dgvHesaab.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        dgvHesaab.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing; //Text with more lines in a row.
        dgvHesaab.ColumnHeadersHeight = 40; //40=row height. Text with more lines in a row.

        dgvHesaab.RightToLeft = RightToLeft.Yes;

        int rowCount = dgvHesaab.Rows.Count; //Get the number of rows
        int pageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(rowCount) / Convert.ToDouble(rowsPerPage)));

        //Set header content of columns
        for (int j = this.dgvHesaab.Columns.Count - 1; j >= 0; j--)
        {
            Rectangle rect = new Rectangle(x, y, 132, dgvHesaab.Rows[0].Height); //132 = width in row
            StringFormat sf = new StringFormat();
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;
            e.Graphics.FillRectangle(Brushes.LightGray, rect);
            e.Graphics.DrawRectangle(Pens.Black, rect);
            if (this.dgvHesaab.Columns[j].HeaderText != null)
            {
                e.Graphics.DrawString(this.dgvHesaab.Columns[j].HeaderText, SystemFonts.DefaultFont, Brushes.Black, rect, sf);
            }
            x += rect.Width;
        }

        x = 5;
        y += dgvHesaab.Rows[0].Height + rowMargin;
        //Set the content of the lines
        while (rowIndex < dgvHesaab.Rows.Count)
        {
            DataGridViewRow row = dgvHesaab.Rows[rowIndex];
            if (row.Cells[0].Value != null)
            {
                for (int j = this.dgvHesaab.Columns.Count - 1; j >= 0; j--)
                {
                    DataGridViewCell cell = row.Cells[j];
                    Rectangle rect = new Rectangle(x, y, 132, dgvHesaab.Rows[0].Height); //132 = width in row
                    StringFormat sf = new StringFormat();
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Alignment = StringAlignment.Center;
                    e.Graphics.DrawRectangle(Pens.Black, rect);
                    if (cell.Value != null)
                    {
                        e.Graphics.DrawString(cell.Value.ToString(), new Font("Tahoma", 8, FontStyle.Bold), Brushes.Black, rect, sf);
                    }
                    x += rect.Width;
                }
                x = 5;
                y += dgvHesaab.Rows[0].Height + rowMargin;
            }
            rowIndex++;

            //Print a new page if necessary
            if (rowIndex % rowsPerPage == 0 || rowIndex == dgvHesaab.Rows.Count)
            {
                e.HasMorePages = (currentPage < pageCount);
                currentPage++;
                return;
            }
        }
    }
    catch (Exception)
    {

    }
}

   
private void btnAdd_Click(object sender, EventArgs e)
{
    try
    {
        //Begin , Adding a row to the gridView(dgvHesaab) 
        ///<summary>
        ///For example, the name of our Grid View is dgvHesaab
        ///Retrieving data from a database or manually inserting data for a row. 
        ///</summary>

        //add row 1
        dgvHesaab.Rows.Add(new object[]
        {
            "Row 1 : Item 1",
            "Row 1 : Item 2",
            "Row 1 : Item 3",
            "Row 1 : Item 4",
            "Row 1 : Item 5",
            "Row 1 : Item 6",
        });

        //add row 2
        dgvHesaab.Rows.Add(new object[]
        {
            "Row 2 : Item 1",
            "Row 2 : Item 2",
            "Row 2 : Item 3",
            "Row 2 : Item 4",
            "Row 2 : Item 5",
            "Row 2 : Item 6",
        });
        //End , Adding a row to the gridView(dgvHesaab)  

        dgvHesaab.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
        dgvHesaab.DefaultCellStyle.WrapMode = DataGridViewTriState.True;

        dgvHesaab.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        dgvHesaab.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing; //Text with more lines in a row.
        dgvHesaab.ColumnHeadersHeight = 40; //40=row height. Text with more lines in a row.

        dgvHesaab.RightToLeft = RightToLeft.Yes;

        //Begin Print
        PrintDialog pDialog = new PrintDialog();
        pDialog.AllowCurrentPage = false;
        pDialog.AllowPrintToFile = false;
        pDialog.AllowSelection = false;
        pDialog.AllowSomePages = false;
        pDialog.PrintToFile = false;
        pDialog.ShowHelp = false;
        pDialog.ShowNetwork = false;
        if (pDialog.ShowDialog() == DialogResult.OK)
        {
            System.Drawing.Printing.PrintDocument pdoc = new System.Drawing.Printing.PrintDocument();
            pdoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pdoc_PrintPage);
            pdoc.DocumentName = "Print Title";
            pdoc.PrinterSettings = pDialog.PrinterSettings;
            pdoc.DefaultPageSettings = pDialog.PrinterSettings.DefaultPageSettings;
            pdoc.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(100, 100, 100, 100);
            pdoc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169);
            pdoc.DefaultPageSettings.Landscape = false; //Landscape
            //pdoc.PrinterSettings.Copies = 3;
            PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
            printPreviewDialog1.Document = pdoc;
            pdoc.Print(); //No preview  
            //printPreviewDialog1.ShowDialog(); //printPreview
        }
        //End Print
    }
    catch (Exception)
    {

    }
}

相关问题