Zohaib
Zohaib

Reputation: 199

PrintPreviewDialog only shows 1 page having lesser number of items than being print

I want to view 100 items into PrintPreviewDialog. For this, i've build String using Stringbuilder and afterwards set their font size and launch PrintPreviewDialog. At this, program works fine but not all 100 items showing into PrintPreviewDialog, its just shows 32 items and 1 page only. Kindly tell me how to show all items into 1 page or in multiple page.

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

Event:

 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);
}

Output: enter image description here

Upvotes: 1

Views: 55

Answers (1)

Related Questions