0

下面的代码用于打印订单。订单可以通过第一次 DrawString 调用写出可变数量的行。

文本

Bottom line1
Bottom line2

必须出现在页面的左下角。

下面的代码使用硬编码值 e.MarginBounds.Top + 460来打印它。如何删除这个硬编码值,以便在页面底部打印文本?

   var doc = new PrintDocument();
   doc.PrinterSettings.PrinterName = "PDFCreator";
   doc.PrintPage += new PrintPageEventHandler(ProvideContent);
   doc.Print();

    void ProvideContent(object sender, PrintPageEventArgs e)
    {
        e.Graphics.DrawString("string containing variable number of lines", new Font("Arial", 12),
            Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top);

        e.Graphics.DrawString("Bottom line1\r\nBottom line2", new Font("Courier", 10), Brushes.Black,
                    e.MarginBounds.Left, e.MarginBounds.Top + 460);
     }
4

1 回答 1

2

测量你的绳子,然后从底部向上移动那个高度?

    void ProvideContent(object sender, PrintPageEventArgs e)
    {
        e.Graphics.DrawString("string containing variable number of lines", new Font("Arial", 12),
            Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top);


        string bottom = "Bottom line1\r\nBottom line2";
        Font courier = new Font("Courier", 10);
        Size sz = TextRenderer.MeasureText(bottom, courier);
        e.Graphics.DrawString(bottom, courier, Brushes.Black,
                    e.MarginBounds.Left, e.MarginBounds.Bottom - sz.Height);
    }
于 2013-05-09T17:17:04.183 回答