Click here to Skip to main content
Licence 
First Posted 15 Oct 2003
Views 42,977
Bookmarked 14 times

Print Formatted Text To A Printer

By | 15 Oct 2003 | Article
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

About the Author

Rod DeValcourt

Web Developer

United States United States

Member

MCAD for .Net
MCSD for .Net
MCDBA for Sql Server 2000
B.S. Computer Science and Mathematics
 


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralRe: Try this instead PinmemberJeffrey Scott Flesher20:17 24 Dec '03  
GeneralTry this instead PinmemberJeffrey Scott Flesher14:28 16 Dec '03  
GeneralRe: Try this instead PinmemberRod DeValcourt14:54 16 Dec '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 16 Oct 2003
Article Copyright 2003 by Rod DeValcourt
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid