Click here to Skip to main content
Licence CPOL
First Posted 23 Jul 2007
Views 34,427
Downloads 1,247
Bookmarked 45 times

LitePrint

By | 23 Jul 2007 | Article
Printing without the doc/view architecture

Introduction

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)
    return;

    //Step 2 : Start the Print
    prt.StartPrint();      

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

    //Step 4 : Start Page
    prt.StartPage(); 

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

    //Step 6 :  now end the page
    prt.EndPage();

    //Step 7 :  close the print document
    //          and release it in the spooler
    prt.EndPrint();  
}

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();
    lp->SetTextColor(RGB(0,0,0));
    if (hf) //if header
    {
        lp->LF();
        CSize sz;
        sz.cx = 100;
        sz.cy = 50;
        lp->PrintBitmap(IDB_BITMAP2, FORMAT_LEFT, &sz);
        lp->Line(PS_SOLID);
        lp->PrintText(lp->GetActiveFont(),
            _T("Report Document"),FORMAT_CENTER);
        CString nmb;
        nmb.Format(_T("Page %d"),sides);
        lp->PrintText(lp->GetActiveFont(),nmb,FORMAT_RIGHT);
        lp->Line(PS_SOLID,5,RGB(230,200,20));
        lp->LF();
    }
    else //if footer
    {
        CString>nmb;
        nmb.Format(_T("Page %d"),sides);
        lp->PrintText(lp->GetActiveFont(),nmb,FORMAT_CENTER);
        lp->LF(lp->GetActiveFont());
        lp->LF(lp->GetActiveFont());
    }
    lp->SetActiveFont(af);
    lp->SetTextColor(tc);
}

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;
vd.push_back(Col(300,FORMAT_CENTER));
vd.push_back(Col(100,FORMAT_LEFT));
vd.push_back(Col(300,FORMAT_CENTER));
...
prt.SetTableColumns(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!

History

  • 23 July, 2007 -- Original version posted

License

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

Member

Likes C++, templates, patterns, sarmas and loves Luka and Jelena

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
QuestionBug in multistring formatting. Fixed. PinmemberMember 86251595:25 5 Apr '12  
BugA memory Leak that solved. PinmemberPE32_6412:02 20 Jun '11  
GeneralWrap PinmemberPudjo21:01 7 Apr '11  
QuestionAny help on compiling with vc6? PinmemberPatrickM6:09 25 Feb '10  
QuestionWhat program on a screenshot you used for the virtual printer? PinmemberAlexander Shevchenko8:57 1 Sep '07  
QuestionCan I use your code freely? Pinmember_flix01_7:31 24 Aug '07  
AnswerRe: Can I use your code freely? Pinmember_flix01_2:25 26 Aug '07  
AnswerRe: Can I use your code freely? PinmemberIvan Markovic20:49 26 Aug '07  
GeneralRe: Can I use your code freely? Pinmember_flix01_7:08 28 Aug '07  
GeneralRe: Can I use your code freely? PinmemberIvan Markovic21:17 28 Aug '07  
GeneralNice work! Thanks. PinmemberBui Tan Duoc20:01 17 Aug '07  
GeneralProblems Compiling in VS6.0 PinmemberMike Copeland5:27 1 Aug '07  
Generalnice work:) PinmemberRoman Cherednik3:23 1 Aug '07  
Questionusage for Visual Studio 8.0 PinmemberSiHot19:46 30 Jul '07  
AnswerRe: usage for Visual Studio 8.0 PinmemberIvan Markovic21:05 30 Jul '07  
Sorry, I am using VS2005(8.0).
However you can create new MFC-based project in VS2003 and include all LitePrint files in it.
GeneralNice work PinmemberETA20:46 24 Jul '07  
Generalgreat! Pinmemberhamo200821:03 23 Jul '07  
GeneralLooks nice! PinmvpHans Dietrich8:39 23 Jul '07  
GeneralRe: Looks nice! PinmemberIvan Markovic20:56 23 Jul '07  

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.120529.1 | Last Updated 23 Jul 2007
Article Copyright 2007 by Ivan Markovic
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid