2017-08-24 4 views
0

私はprintPreviewDialogドキュメントに収まるように長い説明をしようとしています。ただし、テキストボックスに説明を入力して印刷しようとすると、テキストはドキュメントに残りません。printPreviewDialogドキュメントに収まるようにテキストを取得する方法C#

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

`` `

+0

https://stackoverflow.com/questions/9507842/drawstring-word-wrap-or-display-entire-text) –

答えて

0

はあなたがレイアウトすることができますDrawStringのオーバーロードを使用する必要があります。この問題を引き起こしているコードのText not staying on document行は最後の1 e.Graphics.DrawString("Description:" + description4Repair.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 493));

すべてのコードです指定された矩形内のテキスト長方形のサイズを決定するには、MeasureStringを使用します。余分な変数で縦の位置を追跡します。あなたのコードは次のようになります。

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

これは、結果がどのように見えるかです:で説明した[巾着ワードラップやテキスト全体を表示]として(あなたのテキストをラップする必要が

printed wrapped

関連する問題