PrintPreviewDialog仅显示1个页面,其项目数少于打印数量

问题描述 投票:1回答:1

我想查看100个项目到PrintPreviewDialog。为此,我使用Stringbuilder构建String,然后设置它们的字体大小并启动​​PrintPreviewDialog。在这个,程序工作正常,但不是所有100项显示到PrintPreviewDialog,它只显示32项和1页。请告诉我如何将所有项目显示在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

c# wpf
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.