Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Print Formatted Text To A Printer

0.00/5 (No votes)
15 Oct 2003 2  
Prints Formatted Text To A Printer. When you have a need for a report from a text field in a database thats small, say 2000 characters...how do you print it?

Introduction

I have run across a need to print reports from my Sql Server 2000 database tables.  In other words, print a report to show what is in your database table at a functional print level, no graphics, just database fields.

I ran across some great examples for printing objects, but didn't really find one for a specific field that holds text data, say 2000 characters max.  So I wrote a quick program that splits out each word based on a string of text and a variable max number of characters per line.

I placed the split out lines into an System.Collections.ArrayList and then looped it in the printing routine.

I don't have a need per se to print reports with fancy graphics, just functional printouts with a printer friendly font so don't expect the greatest print routine, just an example to point you in the right direction. 

Assumptions made:

Print Line Limit: 72 based on Courier New, 12 Pt font, HP Deskjet 3650 printer.  The code allows you to change this :)

10/17/2003 code update - you'll have to copy the last if statement in the following code snippet to handle lines of text in SplitUpText that have a length < nMaxCount.

public ArrayList SplitUpText(string strText,int nMaxCount)

{

    int nLength = 0;

    int nWhereAt = 0;

    ArrayList al = new ArrayList();

    char nChar;

    char[] characters = strText.ToCharArray();

    string strCurrentLine = "";

    string strTrimmedText = "";

    string strCurrentWord = "";

    int nWordLength = 0;

    nLength = characters.Length;

    while (nWhereAt < nLength)

    {

        nChar = characters[nWhereAt];

        if ((nChar == 32) || (nChar == '\r'))

        {

            strTrimmedText = strCurrentWord.Trim();

            nWordLength = strTrimmedText.Length;

            // add to string here

            if ((strCurrentLine.Length + nWordLength + 1) > nMaxCount)

            {

                al.Add(strCurrentLine);

                strCurrentLine = strTrimmedText + " ";

            } // if ((strCurrentLine.Length + nWordLength + 1) > nMaxCount)

            else

            {

                strCurrentLine = strCurrentLine + strTrimmedText + " ";

            } // else (strCurrentLine.Length + nWordLength + 1) < nMaxCount

            strCurrentWord = "";

            } // if ((nChar == 32) || (nChar == '\r'))

            else

            {

            if ((nChar != 10) && (nChar != 13))

            {

                strCurrentWord = strCurrentWord + nChar;

            } // if ((nChar != 10) && (nChar != 13))

        } // else nChar != 32 && nChar == '\r'

        nWhereAt++;

    } // while (nWhereAt < nLength)

    if (strCurrentLine.Length > 0)

    {

        strTrimmedText = strCurrentLine.Trim();

        al.Add(strTrimmedText);

    } // if (strCurrentLine.Length > 0)

    if (strCurrentWord.Length > 0)

    {

        strTrimmedText = strCurrentWord.Trim();

        al.Add(strTrimmedText);

    } // if (strCurrentWord.Length > 0)

    return al;

} // public ArrayList SplitUpText(string strText,int nMaxCount)

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here