Click here to Skip to main content
16,013,930 members
Articles / Programming Languages / C#
Article

QedPrinter - Quick & Dirty Printer: Printing from C#, using WordPad as the print "engine"

Rate me:
Please Sign up or sign in to vote.
3.26/5 (7 votes)
11 Jul 2005CPOL3 min read 42.7K   1.6K   17  
This article describes a class to quickly implement printing with C# (i.e. yet another .NET printing utility), using the ubiquous Windows program WordPad.exe as its print engine.

Sample Image

Introduction

It seems to be always true that one thing leads to another; this component is at two or three removes from the actual application I'm writing to interface with my employer's customer base. In the course of developing and testing the application, I realized that I wanted to be able to print "stuff" now. After several hours of on-line searching, while I'd come across several interesting articles and classes implementing the PrintDocument class (from which I intend to someday learn how to use that class), I'd not found a simple way to print simple text without investing time I don't have at the moment in learning to use the PrintDocument class.

So, since it was looking as though I'd have to write my own class/component to do what I wanted, I stopped looking for a ready-made one and did just that. I've found The Code Project to be a great resource and have been intending for some time to get around to writing and submitting articles on some classes/components (both original and extensions of others' work) I've written; QedPrinter seems to me a good case to use as my first.

Note: QedPrinter contains no form; the above image is of the demo project superimposed on an instance of WordPad which it spawned via QedPrinter.

Using the code

As I've written it, the interface to QedPrinter is strictly via static class methods and properties. These class methods construct, manipulate and dispose the actual instance of the class. Thus, as written, in an application making use of QedPrinter, there can exist at most one instance at any one time.

QedPrinter makes use of a RichTextBox as its internal buffer. An application making use of QedPrinter may modify the internal buffer in one of two ways:

  1. by appending string values to its .Text property, or
  2. by setting its .Rtf property to that of a RichTextBox the application has constructed/populated.

Using option 2, any graphics and text formatting supported by the RichTextBox class are supported by QedPrinter.

To use QedPrinter as is, add a reference in your project to the class library 'TDHQedPrinter.dll'. The namespace used in that library is:

C#
using TDHControls.TDHQedPrinter;

The members of QedPrinter's interface are:

  • _Start() - This method constructs the instance of QedPrinter (if it doesn't already exist), returning true if the instance was successfully constructed (or already existed), or false upon failure to construct it.
  • _Stop() - This method disposes the instance, returning true upon success, or false upon failure.
  • _Clear() - This method initializes the internal buffer of the QedPrinter instance. An overload of this method accepts a string value to which the internal buffer is initialized.
  • _Print() - This method directly prints, via WordPad, the contents of the internal buffer of the QedPrinter instance.
  • _PrintPreview() - This method opens, in WordPad, the contents of the internal buffer of the QedPrinter instance.
  • _Started - This boolean property returns a value indicating whether the instance exists (i.e. has been started).
  • _AppendLine - This accessor accepts a string assignment, which is appended (as a new line) to the .Text of the internal buffer of the QedPrinter instance.
  • _AppendText - This accessor accepts a string assignment, which is appended to the .Text of the internal buffer of the QedPrinter instance.
  • _RichTextBox - This accessor accepts or returns a RichTextBox (i.e. the internal buffer). The {set} accessor sets the the internal buffer's .Rtf property to that of the assignment. The {get} accessor returns the RichTextBox internal buffer, the properties of which can then be accessed.

Sample code making use of QedPrinter:

C#
using TDHControls.TDHQedPrinter;

private void SomeMethod()
{
    System.Windows.Forms.RichTextBox aRichTextBox = new RichTextBox();
    //
    // code to manipulate the contents of aRichTextBox 
    //

    QedPrinter._Start();
    SomeOtherMethod(aRichTextBox);

    aRichTextBox = new RichTextBox(); // Oops! I didn't mean to do that.
    aRichTextBox.Rtf = QedPrinter._RichTextBox.Rtf;
    // Whew! QedPrinter's buffer to the rescue!

    YetSomeOtherMethod();

    // This will set the aRichTextBox.Text property to the current value
    // of the .Text property of the QedPrinter internal buffer
    aRichTextBox.Text = QedPrinter._RichTextBox.Text;

    // This will reset aRichTextBox to whatever YetSomeOtherMethod() did to 
    // the QedPrinter internal buffer
    aRichTextBox.Rtf = QedPrinter._RichTextBox.Rtf;

    QedPrinter._Stop();
}

private void SomeOtherMethod(System.Windows.Forms.RichTextBox 
                                             parmRichTextBox;)
{
    // Put something into QedPrinter
    QedPrinter._RichTextBox = parmRichTextBox;
    QedPrinter._Print(); // Let's just print it
}

private void YetSomeOtherMethod()
{
    if (QedPrinter._Started)
    {
        QedPrinter._Clear("Hey!  I'm a brash new Method()");
        QedPrinter._AppendLine = "I don't care" + 
            " about whatever might have come before!";
        QedPrinter._AppendLine = "";
    }
    else
    {
        QedPrinter._Start();
    }

    QedPrinter._AppendLine = "This will be on a new line.";
    QedPrinter._AppendText = "\r\n" + "So will this.";
    QedPrinter._AppendText = "This, however," + 
                   " will be all run-on with the previous line.";
    QedPrinter._AppendText = "It will no doubt be an ugly mess.";
    QedPrinter._AppendText = "Andgettingworsebytheminute.";

    QedPrinter._AppendLine = "But enough of this foolishness!";

    QedPrinter._PrintPreview();    // Let's open/view the result with WordPad
}

Behind the Scenes

The QedPrinter._Print() and QedPrinter._PrintPreview() methods work thusly:

  1. Spawn a new thread, which:
  2. Creates a temporary file, using the RichTextBox's .SaveFile() method.
  3. Spawns a new process to start WordPad, passing it this temporary file.
  4. Deletes the temporary file after the WordPad process terminates.

History

  • 2005 July: creation of QedPrinter and submission to The Code Project.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --