Click here to Skip to main content
15,881,089 members
Articles / Programming Languages / Visual Basic
Article

Print Formatted Text To A Printer

Rate me:
Please Sign up or sign in to vote.
2.38/5 (12 votes)
15 Oct 2003CPOL2 min read 53.6K   1.1K   15   3
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
MCAD for .Net
MCSD for .Net
MCDBA for Sql Server 2000
B.S. Computer Science and Mathematics

Comments and Discussions

 
GeneralRe: Try this instead Pin
Jeffrey Scott Flesher24-Dec-03 20:17
Jeffrey Scott Flesher24-Dec-03 20:17 
GeneralTry this instead Pin
Jeffrey Scott Flesher16-Dec-03 14:28
Jeffrey Scott Flesher16-Dec-03 14:28 
GeneralRe: Try this instead Pin
Rod DeValcourt16-Dec-03 14:54
Rod DeValcourt16-Dec-03 14:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.