Click here to Skip to main content
12,693,094 members (31,528 online)
Click here to Skip to main content
Add your own
alternative version


49 bookmarked


, 23 Jul 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Printing without the doc/view architecture


This set of classes enables printing without using doc/view architecture. This allows you to print directly from your dialog or from any part of your MFC-based application. The code was based on A. Fraydl's "Creating a Printing Class" and George Papaioannou's "Quick Print." Improvements include:

  • UNICODE support
  • Unlimited number of fonts and table columns
  • Each table column can now use different alignment
  • Columns can be sized to fit between page margins
  • Better bitmap printing
  • Loading bitmaps from external file
  • Setting/restoring font for document's header and footer

Using the code

The only thing that you have to do is declare the CLitePrint object and start using it. This generic code shows typical usage:

Void CLitePrintDemoDlg::OnPrintButton ()
    CLitePrint prt;
    HLTFONT hFont;

    // Step 1 : call the CPrintDialog
    if (prt.Dialog() == -1)

    //Step 2 : Start the Print

    //Step 3 : Create a printing font
    hFont = prt.AddFontToEnvironment(_T("Arial"),8,8);     

    //Step 4 : Start Page

    //Step 5 : The actual printing goes here
    prt.PrintText(hFont,_T("Example Text"),FORMAT_NORMAL);   

    //Step 6 :  now end the page

    //Step 7 :  close the print document
    //          and release it in the spooler

An external, user-defined header/footer function is called each time the printing reaches the beginning or the end of a page. An example that prints a logo image, lines and page numbers is given bellow:

void FHeadFoot(CLitePrint* lp,int sides, bool hf)
    HLTFONT af = lp->SetHFFont();
    COLORREF tc = lp->GetTextColor();
    if (hf) //if header
        CSize sz; = 100; = 50;
        lp->PrintBitmap(IDB_BITMAP2, FORMAT_LEFT, &sz);
            _T("Report Document"),FORMAT_CENTER);
        CString nmb;
        nmb.Format(_T("Page %d"),sides);
    else //if footer
        nmb.Format(_T("Page %d"),sides);

To use tables, one has to define the column widths and alignment. Some helper code has been developed for this purpose. Just fill in the vector with the column's data and pass it to the SetTableColumns() function. Optional parameters will force the columns to occupy complete space between the margins:

vColData vd;
//prt.SetTableColumns(vd,TRUE); //fit width

Points of interest

More details about all of the functions can be found in the demo project. Note that print preview is not developed yet. Please, if you see any problems with these classes, feel free to send questions. Good luck!


  • 23 July, 2007 -- Original version posted


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ivan Markovic
Software Developer
Yugoslavia Yugoslavia
Likes C++, templates, patterns, sarmas and loves Luka and Jelena

You may also be interested in...

Comments and Discussions

QuestionBug in multistring formatting. Fixed. Pin
Member 86251595-Apr-12 6:25
memberMember 86251595-Apr-12 6:25 
BugA memory Leak that solved. Pin
PE32_6420-Jun-11 13:02
memberPE32_6420-Jun-11 13:02 
GeneralWrap Pin
Pudjo7-Apr-11 22:01
memberPudjo7-Apr-11 22:01 
QuestionAny help on compiling with vc6? Pin
PatrickM25-Feb-10 7:09
memberPatrickM25-Feb-10 7:09 
QuestionWhat program on a screenshot you used for the virtual printer? Pin
Alexander Shevchenko1-Sep-07 9:57
memberAlexander Shevchenko1-Sep-07 9:57 
QuestionCan I use your code freely? Pin
_flix01_24-Aug-07 8:31
member_flix01_24-Aug-07 8:31 
AnswerRe: Can I use your code freely? Pin
_flix01_26-Aug-07 3:25
member_flix01_26-Aug-07 3:25 
AnswerRe: Can I use your code freely? Pin
Ivan Markovic26-Aug-07 21:49
memberIvan Markovic26-Aug-07 21:49 
GeneralRe: Can I use your code freely? Pin
_flix01_28-Aug-07 8:08
member_flix01_28-Aug-07 8:08 
GeneralRe: Can I use your code freely? Pin
Ivan Markovic28-Aug-07 22:17
memberIvan Markovic28-Aug-07 22:17 
GeneralNice work! Thanks. Pin
Bui Tan Duoc17-Aug-07 21:01
memberBui Tan Duoc17-Aug-07 21:01 
GeneralProblems Compiling in VS6.0 Pin
Mike Copeland1-Aug-07 6:27
memberMike Copeland1-Aug-07 6:27 
Generalnice work:) Pin
Roman Cherednik1-Aug-07 4:23
memberRoman Cherednik1-Aug-07 4:23 
Questionusage for Visual Studio 8.0 Pin
SiHot30-Jul-07 20:46
memberSiHot30-Jul-07 20:46 
AnswerRe: usage for Visual Studio 8.0 Pin
Ivan Markovic30-Jul-07 22:05
memberIvan Markovic30-Jul-07 22:05 
GeneralNice work Pin
ETA24-Jul-07 21:46
memberETA24-Jul-07 21:46 
Generalgreat! Pin
hamo200823-Jul-07 22:03
memberhamo200823-Jul-07 22:03 
GeneralLooks nice! Pin
Hans Dietrich23-Jul-07 9:39
mvpHans Dietrich23-Jul-07 9:39 
GeneralRe: Looks nice! Pin
Ivan Markovic23-Jul-07 21:56
memberIvan Markovic23-Jul-07 21:56 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 23 Jul 2007
Article Copyright 2007 by Ivan Markovic
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid