PrintPreviewDialog仅显示一页的项目比正在打印的页面少

时间:2018-09-02 09:28:42

标签: c# wpf

我想在PrintPreviewDialog中查看100个项目。为此,我使用Stringbuilder构建String,然后设置其字体大小并启动​​PrintPreviewDialog。此时,程序可以正常工作,但不能将全部100个项目显示到PrintPreviewDialog中,它仅显示32个项目和1个页面。请告诉我如何将所有项目显示为一页或多页。

    for (int i = 0; i < 100; i++)
    {
        Item item = new Item()
        {
            Code = i.ToString(),
            Name = "name",
            UnitPrice = "10",
            Quantity = "2",
            SubTotal = "1"
        };
        list.Add(item);
    }

    const int FIRST_COL_PAD = 20;
    const int SECOND_COL_PAD = 7;
    const int THIRD_COL_PAD = 20;

    var sb = new StringBuilder();
    sb.AppendLine("Start of receipt");
    sb.AppendLine("================");

    foreach (var item in list)
    {
        sb.Append(item.Code.PadRight(FIRST_COL_PAD));

        var breakDown = int.Parse(item.UnitPrice) > 0 ? item.UnitPrice + "x" + 2 : string.Empty;
        sb.Append(breakDown.PadRight(SECOND_COL_PAD));

        sb.AppendLine(string.Format("{0:0.00} A", item.SubTotal).PadLeft(THIRD_COL_PAD));
    }

    sb.AppendLine("================");
    printText = new PrintText(sb.ToString(), new Font("Monospace Please...", 20));

    System.Windows.Forms.PrintDialog pd = new System.Windows.Forms.PrintDialog();
    pdoc = new PrintDocument();
    PrinterSettings ps = new PrinterSettings();
pd.Document = pdoc;
 pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);

            DialogResult result = pd.ShowDialog();
            if (result == DialogResult.OK)
            {
                PrintPreviewDialog pp = new PrintPreviewDialog();
                pp.Document = pdoc;
                result = pp.ShowDialog();
                if (result == DialogResult.OK)
                {
                    pdoc.Print();
                }
            }

事件:

 void pdoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics graphics = e.Graphics;
            var layoutArea = new SizeF(widht, 0);
            SizeF stringSize = graphics.MeasureString(printText.Text, printText.Font, layoutArea, printText.StringFormat);

            RectangleF rectf = new RectangleF(new PointF(), new SizeF(widht, stringSize.Height));
            graphics.DrawString(printText.Text, printText.Font, System.Drawing.Brushes.Black, rectf, printText.StringFormat);
}

输出: enter image description here