Simple Wrapper for Printing Text in .net

tinstaafl 1 Tallied Votes 580 Views Share

Here's a simple wrapper for printing text. Built as a class library, this can be used in any .net application. This has automatic word wrapping. I figured that using the new constructor to accept different parameters would easily allow for printing different documents with different settings. I included Name, Font, and Landscape, but the structure is quite simple and can easily be adapted to include or exclude any property or setting.

Usage is quite simple, build a string using linebreaks if needed(C#-'\n', VB-vbNewLine), declare a new PrintDoc, then call the Print() method.

//C#
PrintDoc pd = new PrintDoc(MyString);
pd.Print();

'VB
Dim pd as New PrintDoc(MyString)
pd.Print()

Options include adding a Document name to the top of the printout, using a different font, or using Landscape orientation. The options are included as overloads, Landscape is optional in each overload, when declaring the new PrintDoc.

Hope someone can find this useful.

Jx_Man commented: Great snippet +14
using System;
using System.Drawing;
using System.Drawing.Printing;
namespace PrintDocClass
{
    public class PrintDoc
    {
        private PrintDocument pd1 =  new PrintDocument();
        private Font _pdfont = new Font("Microsoft Sans Serif",8.25f);
        private string _PrintString = "";
        private string _Name = "";
        private bool _Landscape = false;
        public PrintDoc(string PrintString, bool Landscape = false)
        { 
            _PrintString = PrintString;
            _Landscape = Landscape;
        }
        public PrintDoc(string PrintString, string DocName, bool Landscape = false)
        {
            _PrintString = PrintString;
            _Name = DocName;
            _Landscape = Landscape;
        }
        public PrintDoc(string PrintString, string DocName, Font PrintFont, bool Landscape = false)
        {
            _PrintString = PrintString;
            _Name = DocName;
            _pdfont = PrintFont;
            _Landscape = Landscape;
        }
        public void Print()
        {
            pd1.DefaultPageSettings.Landscape = _Landscape;
            pd1.PrintPage += new PrintPageEventHandler(pd1_PrintPage);
            if (_Name != "")
                _PrintString = _Name + "\n\n" + _PrintString;
            pd1.Print();
        }
        private void pd1_PrintPage(object sender, PrintPageEventArgs e)
        {

            int charactersOnPage = 0;
            int linesPerPage = 0;
            // Sets the value of charactersOnPage to the number of characters  
            // of stringToPrint that will fit within the bounds of the page.
            
            e.Graphics.MeasureString(_PrintString,_pdfont ,
                e.MarginBounds.Size, StringFormat.GenericTypographic,
                out charactersOnPage, out linesPerPage);

            // Draws the string within the bounds of the page
            e.Graphics.DrawString(_PrintString, _pdfont, Brushes.Black,
                e.MarginBounds, StringFormat.GenericTypographic);

            // Remove the portion of the string that has been printed.
            _PrintString = _PrintString.Substring(charactersOnPage);

            // Check to see if more pages are to be printed.
            e.HasMorePages = (_PrintString.Length > 0);
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.