Graphics, Multimedia, and Printing Recipes—Recipe 8 16

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:Articles
C# Articles

Graphics, Multimedia, Printing

© 2006 Jones, MacDonald, Rajan

Recipe 8-16. Print Wrapped Text

Problem

You need to parse a large block of text into distinct lines that fit on one page.


Solution

Use the Graphics.DrawString method overload that accepts a bounding rectangle.


How It Works

Often, you will need to break a large block of text into separate lines that can be printed individually on a page. The .NET Framework can perform this task automatically, provided you use a version of the Graphics.DrawString method that accepts a bounding rectangle. You specify a rectangle that represents where you want the text to be displayed. The text is then wrapped automatically to fit within those confines.


The Code

The following code demonstrates this approach, using the bounding rectangle that represents the printable portion of the page. It prints a large block of text from a textbox on the form.


using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
 
namespace Apress.VisualCSharpRecipes.Chapter08
{
  public partial class Recipe08_16 : Form
  {
    public Recipe08_16()
    {
      InitializeComponent();
    }
 
    private void cmdPrint_Click(object sender, EventArgs e)
    {
      // Create the document and attach an event handler.
      string text = "Windows Server 2003 builds on the core strengths " +
        "of the Windows family of operating systems--security, " +
        "manageability, reliability, availability, and scalability. " +
        "Windows Server 2003 provides an application environment to " +
        "build, deploy, manage, and run XML Web services. " +
        "Additionally, advances in Windows Server 2003 provide many " +
        "benefits for developing applications.";
      PrintDocument doc = new ParagraphDocument(text);
      doc.PrintPage += this.Doc_PrintPage;
 
      // Allow the user to choose a printer and specify other settings.
      PrintDialog dlgSettings = new PrintDialog();
      dlgSettings.Document = doc;
 
      // If the user clicked OK, print the document.
      if (dlgSettings.ShowDialog() == DialogResult.OK)
      {
        doc.Print();
      }
    }
 
    private void Doc_PrintPage(object sender, PrintPageEventArgs e)
    {
      // Retrieve the document that sent this event.
      ParagraphDocument doc = (ParagraphDocument)sender;
 
      // Define the font and text.
      using (Font font = new Font("Arial", 15))
      {
        e.Graphics.DrawString(doc.Text, font, Brushes.Black,
           e.MarginBounds, StringFormat.GenericDefault);
      }
    }
  }
 
  public class ParagraphDocument : PrintDocument
  {
    private string text;
    public string Text
    {
      get { return text; }
      set { text = value; }
    }
 
    public ParagraphDocument(string text)
    {
      this.Text = text;
    }
  }
}


Figure 8-10 shows the wrapped text.


Image:5890f0810.jpg

Figure 8-10. The printed document with wrapping


Previous_Page_.gif Next_Page_.gif


Personal tools