Click here to Skip to main content
13,050,840 members (57,843 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 22 Nov 1999

A line printer class

, 22 Nov 1999
Rate this:
Please Sign up or sign in to vote.
A handy way to write to a line printer


Windows is really fun, mostly. But one of the things I miss about UNIX (or the IBM 1401) is the handy way it writes to a line printer. No sissy graphics, no obsequious "Page n of m" ( unless you want to write a lot of extra code), just thousands of sturdy fixed width lines of ASCII characters. Gayle Manufacturing Company (The GM in all the names and labels below) is the steel fabricating company I work for. Most of the routine reports we generate are in line printer format, nothing fancy - just the numbers. Did you ever wonder why there isn't some easy way to simply iterate through your data and print a line at a time (nostalgic sigh)?? Maybe it's just been too obvious and that's why I missed it.

I'm immersed in a project that is a redesign of a system that presently runs on UNIX. Most of the reports are fine the way they are so I want a simple way to re-create them from Windows apps. I need a line printer!! I want printf() to go to 'stdout' so I can pipe it to 'lp'. Is that so wrong? The idea is to loop throught the data in a query and print a line or two for each row of data with the odd total here and there.

Ah, well. Things have changed and there isn't a line printer so I guess I had to make my own. I designed a COM class that acts sort of like a line printer. It's written in VC++ 5 with MFC4.2, ATL and STL. Since it is a dual inteface COM component, it may be used from C++ or VBA. It makes it trivially easy to create line printer style reports and it runs under MTS. It is another example of a way to print without using a CView class. It compiles under UNICODE. The error checking is rather minimal and some of the code is probably BFI (Brute Force and Ignorance, but if I'm ignorant how can I tell?).

The COM Interface has the following methods

write(LINE_TYPE, BSTR text)  // to send lines to the print arrays
reset(RESET_TYPE)            // to clear the print arrays
print(PRINT_TYPE)            // to format the print arrays onto the default printer
These properties
font_size ( GMP_FONT_CPI)       //to set the fixed width font size(10, 12, 15 cpi)
orientation ( GMP_ORIENTATION)  //portrait or landscape
title(BSTR)                     //prints on the lower right corner of each page
print_heading(bool)             // set to false for really plain printing
page_breaks(bool)               //if false don't allow forced page breaks
punch_margin(double )           // Sets the width of the margin for 3 hole punching
and these enumerations
[helpstring("enum line types")]
typedef enum

[helpstring("enum reset types")]
typedef enum

[helpstring("enum print types")]
typedef enum

[helpstring("enum orientation")]
typedef enum

[helpstring("enum font cpi selection")]
typedef enum

It works like this Use the usual means of getting a pointer to an IGMPrintEZ instance. I like the #import method so

IGMPrintEZPtr p_prt;
HRESULT hr = p_prt.CreateInstance(__uuidof(GMPrintEZ));

The printer object starts out empty but just to be sure I can reset it

p_prt->reset(GMP_RS_ALL);    // clears all the data from the arrays

Write the heading lines to the heading line table

// the text is a BSTR so use any means of providing one
BSTR text = SysAllocString(L"HeadingLine 1");
CString str_blankline = "";
p_prt->write(GMP_LT_HEAD, text);
// a blank line
p_prt->write(GMP_LT_HEAD, str_blankline.AllocSysString());
        SysAllocString(L"Column 1        Column 2  Column3"));

You can write heading lines any time during the process. There is a separate array for the heading and the body lines. The driving program could write the heading lines AFTER processing all the body lines so that totals appear on each page heading. This opens a number of possibilities. In fact the title, orientation, and font can all be determined after the body lines have been written to the body line array, and set just prior to actually printing.

The other operation is to write the body lines

while(! data.IsEOF())
  CString data;
  data.Format("%25s %10d", data.item, data.quantity);
  p_prt->write(lt, data.AllocSysString());

Before we print we set the title, the format to landscape and the font to 12 characters per inch

p_prt->title = _T("Test Job");
 p_prt->orientation = GMP_LANDSCAPE;
 p_prt->font_size = GMP_FONT_12;

And then we print


The print object iterates through the BODY line array and prints a line for each entry in the array. The heading and footer are separated from the body by horizontal lines. The date and time print in the lower left, the page number in the center and the title in the lower right.

The main elements of the program are a pair of vectors ( vector), one for the heading lines and one for the body lines, and a print loop. Here's what the print loop code looks like

bool GMLinePrintpage_loop()
// loop thru the body lines and print to the page
        CPrintDialog pd(FALSE);
        DOCINFO di;     // must have DOCINFO for CDCStartDoc(DOCINFO)
        m_line = 0;
        m_max_lines = 20;
        m_last_body_line = m_max_lines;
        m_line_height = LINEHEIGHT_10;

                // if the body lines don't contain anything just return now
                if(0 == body_lines->size())
                return true;

        memset(di, 0, sizeof(DOCINFO)); // make a clean start
        di.cbSize = sizeof(DOCINFO);
        di.lpszDocName = m_title;

        // lazy way of getting the default printer
        // just get all the printer defaults - no  display
        // so this COM object can run from MTS


        DEVMODE *dm = pd.GetDevMode();

        // set orientation
        // print landscape or portrait?
        dm->dmOrientation = m_orientation + 1;
        // signify te presence of orientation data
        dm->dmFields |= DM_ORIENTATION;

        // set punch margin
        case GMP_PORTRAIT
                m_top_offset = 0;
                m_left_offset = INCH * m_punch_margin;
        case GMP_LANDSCAPE
                m_top_offset = INCH * m_punch_margin;
                m_left_offset = 0;

        // create the printer device context by getting values from the
        // printdialog and the dm structure
        CDC dc;
        if(! dc.CreateDC(pd.GetDriverName(), pd.GetDeviceName(),
                pd.GetPortName(), dm))
                AfxMessageBox(_T("Can't create DC in print_loop"));
                return false;


        // obtain the page dimensions from the Device Context
        m_page_height = dc.GetDeviceCaps(VERTSIZE) * MM_TO_INCH;
        m_page_width = dc.GetDeviceCaps(HORZSIZE) * MM_TO_INCH;

        CFont *oldfont;

        // select font and set line height
        case GMP_FONT_12
                oldfont = dc.SelectObject(C12);
                m_line_height = LINEHEIGHT_12;
                m_cpi = 12;
        case GMP_FONT_15
                oldfont = dc.SelectObject(C15);
                m_line_height = LINEHEIGHT_15;
                m_cpi = 15;
        case GMP_FONT_10
                oldfont = dc.SelectObject(C10);
                m_line_height = LINEHEIGHT_10;
                m_cpi = 10;

        // compute the lines per page
        m_max_lines = -(( m_page_height - m_top_offset) /

        // compute the last body line
                m_last_body_line = m_max_lines - FOOTER_LINES -
                head_lines->size() - HEADER_SEPARATOR;
                m_last_body_line = m_max_lines;

        // the print loop
        // I like to use the Standard Library collections when I can.
        vectoriterator itext;
        m_page = 0;
        for(itext = body_lines->begin();
                itext < body_lines->end();
                // look for a pagebreak
                if("@@FF" == itext->Left(4))
                        m_line = 0;
                        // otherwise just ignore the page_break token
                        if (m_line >= m_last_body_line)
                        m_line = 0;
                        if (m_line == 0)

                                (m_line++ * m_line_height) - m_top_offset,


        return true;
                return false;

There is no user interface to this object (that's a feature!!) so there is no print preview or printer dialog. This makes it it possible to install this COM class on MTS and run the printer from a process on a remote system. This article was contributed by Richard Warg .

There are many applications where I don't really want to use the usual windows page print. Instead I want output to go directly to the printer using standard print i/o. It's actually a topic that is hard to find in any of the books on Windows, at least I've never found anything on it. But to my surprise I recently learned that standard (DOS/UNIX) printing is alive and well underneath windows. All we need to do is open a printer port and print to it.

If the printer is directly attached to the computer it's trivial. The method for obtaining a printer port when the printer is on the network isn't hard either.

The example below shows how I use the Windows NET USE command to re-direct LPT1 to a shared printer on an NT Server. The same technique applies for Novell networks with a slightly different syntax.

Try this out by creating a new MFC Form-based project. Put a button on the form and attach this code to it. You can actually print with only 3 lines of code:

FILE *fp = fopen("LPT1", "w");
fprintf(fp,"What's up, Doc?\n");

Instant print gratification!!

While the program is open it hogs the printer port. In my shop that isn't a problem but be aware of the effect on your windows spooled output.

                        THE CODE

// the headers for the conventional i/o routines

using namespace std;      // makes string and ofstream
                        // work without std:: qualifier

void CLineprtView::OnButton1()
        // I could have used a CString instead of the buff[]
        // but I wanted to show how this is used with lightweight
        // ATL code and STD library

        char buff[MAX_BUFF_SIZE];

        // My printer is located on another server so I must re-direct the
        // printer port.  If the printer is directly attached this extra step
        // is not needed.
        // on my network the printer is published as \\GREEN\hp5annex
        // All those back-slashes escape the backslash in the path name

        if (PRINTER_IS_REMOTE)
                system("NET USE LPT1 /d");  // free up the port
                system("net use lpt1 \\\\green\\hp5annex");

        // old fashioned file handle with
        // old fashioned open of the printer port
        FILE *ptr = fopen("LPT1","w");

        // laser printer setup string

        // old fashioned print
        fprintf(ptr,"Who of late doth make a thimble.\n");
        fprintf(ptr,"Is a lower bunk a status symbol??\n");

        // old fashioned close

        // now the same thing with stream io
        ofstream optr("LPT1", ios::out);

        string str_text = "Hey Doc, Ain't this a print test from windows\n";
        str_text += "with more lines to follow?\n";

        optr << str_text << endl;
        optr << "Quiet, wabbit. I'm conversing with my muse!!\n";

        optr << "That's all folks." << "\f" << flush;     // add a formfeed

        // the printer connection is still open so close it

        // drop the network link
        if (PRINTER_IS_REMOTE)
                system("net use lpt1 /d");

In practice I get printer path information from the registry on each machine, so the real live code is a little busier than this example, but not much.


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

Richard Warg
United States United States
Dick Warg is very old, 60 in fact.
Dick thinks FORTH is still cool but he mostly writes dreadful C++ which ends up looking a lot like FORTH anyway when he gets done with it. He hopes to finish his current project real soon, or at least before his Social Security kicks in.

Favorite saying: "Who wrote this garbage? Oh, I did."

You may also be interested in...

Comments and Discussions

GeneralHere's to you, old man!! Pin
WREY28-Oct-03 10:46
memberWREY28-Oct-03 10:46 
GeneralRe: Here's to you, old man!! Pin
Fastfootskater11-Jul-05 20:07
memberFastfootskater11-Jul-05 20:07 
GeneralRe: Here's to you, old man!! Pin
Rajeev Verma4-Dec-07 20:36
memberRajeev Verma4-Dec-07 20:36 
GeneralRe: Here's to you, old man!! Pin
"_$h@nky_"2-Jul-08 2:53
member"_$h@nky_"2-Jul-08 2:53 
Generaldoes it work for windows CE Pin
keen7-May-03 23:26
memberkeen7-May-03 23:26 
Generala lost art Pin
Marc Clifton25-Feb-03 0:49
memberMarc Clifton25-Feb-03 0:49 
Generalquestion about CPrintDialog and DEVMODE Structure Pin
lavocat2-Dec-02 0:26
memberlavocat2-Dec-02 0:26 
GeneralWin95 not working with sizes Pin
Antonis24-Jul-02 19:43
sussAntonis24-Jul-02 19:43 
GeneralNot so good... Pin
GeoffSW8-Mar-02 11:14
memberGeoffSW8-Mar-02 11:14 
GeneralWell, line printing is for geeks :-) Pin
Anonymous7-Sep-02 15:51
sussAnonymous7-Sep-02 15:51 
GeneralWell, well Pin
Anonymous23-Aug-01 10:38
memberAnonymous23-Aug-01 10:38 
GeneralRe: Well, well Pin
Blake Miller7-Jun-04 5:27
memberBlake Miller7-Jun-04 5:27 
Generalline printer Pin
t.sha24-Jul-01 4:22
membert.sha24-Jul-01 4:22 
GeneralRe: line printer Pin
Anonymous14-Mar-02 12:45
memberAnonymous14-Mar-02 12:45 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 23 Nov 1999
Article Copyright 1999 by Richard Warg
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid