c#new line打印出一个String

时间:2015-08-11 14:25:28

标签: c# string printing

如何打印整个字符串? 我想知道我怎么能用New Line打印出这个String?

感谢您的回答

    private void drucken_Click(object sender, EventArgs e)
    {

        PrintDialog printDialog = new PrintDialog();
        PrintDocument printDocument = new PrintDocument();
        printDialog.Document = printDocument;

        printDocument.PrintPage += new PrintPageEventHandler (PrintReceiptPage);

        DialogResult result = printDialog.ShowDialog();

        if (result == DialogResult.OK)
        {
            printDocument.Print();
        }           
    }

    private void PrintReceiptPage(object sender, PrintPageEventArgs e)
    {
        string message = "Haalloodhsak dfsdfsfdsfsdfdssdddddddddddddddsf  Tetsn dfgdfgdfgdfg dfgdfgdfgdfg dfgdfgdfgdfggdfdfg gdgdgdffgddfgdfgdfggdf dfgdfgdfgdfggdf";
        int y;
        // Print receipt
        Font myFont = new Font("Times New Roman", 15, FontStyle.Bold);
         y = e.MarginBounds.Y;
       e.Graphics.DrawString(message , myFont, Brushes.Red,   e.MarginBounds.X, y);
    }

2 个答案:

答案 0 :(得分:1)

将PrintReceiptPage方法更改为以下内容:

private void PrintReceiptPage(object sender, PrintPageEventArgs e)
{
    string message = "Haalloodhsak dfsdfsfdsfsdfdssdddddddddddddddsf  Tetsn dfgdfgdfgdfg dfgdfgdfgdfg dfgdfgdfgdfggdfdfg gdgdgdffgddfgdfgdfggdf dfgdfgdfgdfggdf";
    int yMargin;
    // Print receipt
    Font myFont = new Font("Times New Roman", 15, FontStyle.Bold);
    yMargin = e.MarginBounds.Y;

    // Create rectangle for drawing. 
    float x = 150.0F;
    float y = 150.0F;
    float width = 200.0F;
    float height = 50.0F;
    RectangleF drawRect = new RectangleF(x, yMargin, width, height);

    e.Graphics.DrawString(message, myFont, Brushes.Red, drawRect);
}

答案 1 :(得分:0)

我认为没有办法自动包装文本。您可以做的一件事是测量字符串的一部分,如果有足够的空间,则添加字符。如果没有足够的空间,请打印并向下移动一点。

要测量字符串,请使用e.Graphics.MeasureString(string,Font)。希望这会有所帮助。