如何使文本适合printPreviewDialog文档C#

如何使文本适合printPreviewDialog文档C#,c#,winforms,printing,print-preview,C#,Winforms,Printing,Print Preview,我正试图得到一个长的描述,以适合我的printPreviewDialog文档。但是,当我在文本框中键入描述并尝试打印时,文本不会保留在文档上。导致此问题的代码行是最后一行 e、 Graphics.DrawString(“Description:”+description4Repair.Text,新字体(“Arial”,15,FontStyle.Regular),笔刷。黑色,新点(50493)) 所有代码: e.Graphics.DrawString("DEVICE INFORMAT

我正试图得到一个长的描述,以适合我的printPreviewDialog文档。但是,当我在文本框中键入描述并尝试打印时,文本不会保留在文档上。导致此问题的代码行是最后一行

e、 Graphics.DrawString(“Description:”+description4Repair.Text,新字体(“Arial”,15,FontStyle.Regular),笔刷。黑色,新点(50493))

所有代码:

       e.Graphics.DrawString("DEVICE INFORMATION:", new Font("Arial", 14, FontStyle.Regular), Brushes.Black, new Point(286, 373));


        e.Graphics.DrawString("Device Type:" + devices.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 403));

        if (devices.Visible == true & devices.Text == "Console")
        {
            e.Graphics.DrawString("Type of Console:" + consoleTextBox.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 433));
        }

        if (comboBox3.Visible == true) //iphone selection box is visible then show selected model on print document
        {
            e.Graphics.DrawString("Type of Phone:" + comboBox3.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 433));


        e.Graphics.DrawString("Service Type:" + serviceDesc.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 463));

        e.Graphics.DrawString("Description:" + description4Repair.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 493));

```

您需要使用的重载,它允许您在给定的矩形内布局文本。要确定矩形的大小,请使用。在额外变量中跟踪垂直位置。您的代码如下所示:

private int DrawWrapped(string text, Font f, Point location, Size maxSize, Graphics g)
{
    // how much space is needed
    var neededRect = g.MeasureString(text, f, maxSize.Width - location.X);
    var rect = new Rectangle(location, neededRect.ToSize());
    g.DrawString(text, f, Brushes.Black, rect, StringFormat.GenericDefault);
    return (int) Math.Ceiling(neededRect.Height);
}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    int y = 373; // keep track of where we are on the page vertically
    using (var arial14 = new Font("Arial", 14, FontStyle.Regular))
    {
        // add the used height to where we are
        y += DrawWrapped("DEVICE INFORMATION:", arial14, new Point(286, y), e.PageBounds.Size, e.Graphics);
        y += (2 * arial14.Height); // add some white space
    }
    using (var arial15 = new Font("Arial", 15, FontStyle.Regular))
    {
        y += DrawWrapped("Device Type:" + devices.Text, arial15, new Point(50, y), e.PageBounds.Size, e.Graphics);

        if (devices.Visible == true && devices.Text == "Console") // better use && me thinks 
        {
            y += DrawWrapped("Type of Console:" + consoleTextBox.Text, arial15, new Point(50, y ), e.PageBounds.Size, e.Graphics);
        }
        if (comboBox3.Visible == true) //iphone selection box is visible then show selected model on print document
        {
            y += DrawWrapped("Type of Phone:" + comboBox3.Text, arial15, new Point(50, y ), e.PageBounds.Size, e.Graphics);
            y += DrawWrapped("Service Type:" + serviceDesc.Text, arial15,  new Point(50, y ), e.PageBounds.Size, e.Graphics);
            y += DrawWrapped("Description:" + description4Repair.Text, arial15, new Point(50, y), e.PageBounds.Size, e.Graphics);
        }
    }
}
结果将是这样的:


您需要按照中的说明包装文本