string.Format没有与固定宽度对齐

时间:2014-08-13 04:30:21

标签: c# printing receipt

这是我打印订单收据的功能,打印工作但我的问题是打印没有对齐记录。我已经在底部附加了输出的屏幕截图,请告诉我我做了什么,这样输出就好了。

private void PrintReceipt(object sender, PrintPageEventArgs e)
    {
        //Graphics g = this.CreateGraphics();
        Graphics graphic = e.Graphics;
        Font font = new Font("Courier", 8);
        Font Heading = new Font("Courier", 10);
        float fontHeight = font.GetHeight();
        int startX = 10;
        int startY = 10;
        int offset = 40;

        int receipt_w = 400;
        //int receipt_h = 0;

        string company_name = "Welcome to Arslan Medicose";
        string company_address = "Kohenoor One Jaranwala road faisalbad";
        string company_phone = "Phone:+9233898937";

        string developer_str = "Developed by: virk";
        string developer_contact = "Phone:+923338989937";

        string underLine = "-------------------------------------------------------------------------------------------------------";


        SizeF size = graphic.MeasureString(company_name, Heading);
        graphic.DrawString(company_name, Heading, new SolidBrush(Color.Black), (receipt_w - size.Width) / 2, startY);

        //offset = offset + (int)fontHeight + 5;
        size = graphic.MeasureString(company_address, Heading);
        graphic.DrawString(company_address, font, new SolidBrush(Color.Black), (receipt_w - size.Width) / 2, startY + offset);

        offset = offset + (int)fontHeight + 5;

        size = graphic.MeasureString(company_phone, Heading);
        graphic.DrawString(company_phone, font, new SolidBrush(Color.Black), (receipt_w - size.Width) / 2, startY + offset);

        offset = offset + (int)fontHeight + 5;

        graphic.DrawString(underLine, font, new SolidBrush(Color.Black), startX, startY + offset);

        offset = offset + (int)fontHeight + 5;

        int total_rows = cart_pro_gv.RowCount;
        int recs = 1;

        foreach (DataGridViewRow row in cart_pro_gv.Rows) {

            if (recs < total_rows)
            {
                string pn = (null != row.Cells[1].Value) ? row.Cells[1].Value.ToString().Trim() : String.Empty;
                string pq = (null != row.Cells[2].Value) ? row.Cells[2].Value.ToString().Trim() : String.Empty;
                string pp = (null != row.Cells[3].Value) ? row.Cells[3].Value.ToString().Trim() : String.Empty;
                string ppt = (null != row.Cells[4].Value) ? row.Cells[4].Value.ToString().Trim() : String.Empty;


                string pline = string.Format("{0,-40}|{1,-15}|{2,-15}|{3,-15}", pn, pq, pp, ppt);
                //string pline = string.Format("{0}|{1}|{2}|{3}", pn.PadRight(30), pq.PadRight(30), pp.PadRight(30), ppt.PadRight(30));

                /*int padding = 3;
                int maxCol0width = pn.Length;
                int maxCol1width = pq.Length;
                int maxCol2width = pp.Length;
                int maxCol3width = ppt.Length;

                string fmt0 = "{0,-" + (maxCol0width + padding) + "}";
                string fmt1 = "{1,-" + (maxCol1width + padding) + "}";
                string fmt2 = "{2,-" + (maxCol2width + padding) + "}";
                string fmt3 = "{3,-" + (maxCol3width + padding) + "}";

                string fmt = fmt0 + fmt1 + fmt2 + fmt3;

                string pline = string.Format(fmt, pn, pq, pp, ppt);*/


                graphic.DrawString(pline, font, Brushes.Black, startX, startY + offset);
                offset = offset + (int)fontHeight + 5;
            }
            recs++;
        }

       // offset = offset + (int)fontHeight + 5;

        graphic.DrawString(underLine, font, Brushes.Black , startX, startY + offset);

        offset = offset + 10;
        string total_paying = "Net Total: " + cart_total_label.Text;
        graphic.DrawString(total_paying, font, new SolidBrush(Color.Black), startX, startY + offset);
    }

这是我得到的输出:

enter image description here

0 个答案:

没有答案