【问题标题】:c# new line printing out a Stringc#换行打印出一个字符串
【发布时间】:2015-08-11 14:25:28
【问题描述】:

如何打印出整个字符串? 我想知道如何用换行符打印出这个字符串?

谢谢你的回答

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

【问题讨论】:

标签: c# string printing


【解决方案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);
}

【讨论】:

    【解决方案2】:

    我认为没有任何方法可以自动换行。您可以做的一件事是测量字符串的一部分,并在有足够空间的情况下添加字符。如果没有足够的空间,打印并向下移动一点。

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多