Click here to Skip to main content
Click here to Skip to main content

Quick Print

, 5 May 2006
Rate this:
Please Sign up or sign in to vote.
An easy way to print without the doc/view architecture.

Sample Image

Introduction

This printing class is based on the article by Rob A. Fraydl, "Creating a Printing Class". In fact, it's just an extension of the original class. What exactly has been extended? Well, first of all, I renamed the class to QPrint (stands for quick print). Big achievement, eh? OK, OK, this feature is not an extension at all, but anyway it's a start.

The second extension was to add the ability to manage big strings that extend beyond the right border of the physical paper size. Now, the big strings are not clipped at the end of the right border, but instead they continue to the next line.

Last but not the least, the new printing class supports tables as well as page orientation.

Background

As already said in the beginning, this piece of code did not start from scratch. You can always refer to the original printing class named CPrivatePrint. Please feel free to use and improve this code. Other than the additions I have made, I also corrected some bugs. I am not sure in some points if they were bugs, especially the part with the logical units, using the macros CALCF(x), CALCY(y), and CALCX(x). I deleted CALCX(x) completely, and the CALCY(x) partially, from the source code. So, if you meet "strange" results, please let me know. The others were safe point bugs that got corrected successfully.

I tested this class using the JawsPDF Creator (to save paper, time, patience, and finally money!) and different ink jet printers. It worked flawlessly.

Using the code

I chose this printing class as a base to build QuickPrint, because it was compact and easy to use. So, the only thing that you have to do is to declare the CQPrint prt and start using it. The following example shows the basic steps that you have to do:

void TestDlg::OnPrintButton ()
{
CQPrint prt;
HPRIVATEFONT   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("Arial Greek",8,8);     
  
  //Step 4 : Start Page
  prt.StartPage(); 
  
  //Step 5 : The actual printing goes here
  prt.Print(hFont,"Set Font Arial Size = 8",FORMAT_NORMAL);   
  
  //Step 6 :  now end the page
  prt.EndPage();

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

You can see a live example (like the picture in this tutorial) in the following code:

 CQPrint  prt;
 HPRIVATEFONT   hFont,hFont1,hFont2;
 CSize  dim;
 CRect  margins;
  
 

  // initializing the class and printer selection
  if (prt.Dialog() == -1)
      return;

  // create the print document in the spooler
  prt.SetPageOrientation (DMORIENT_PORTRAIT);
  prt.StartPrint();


  // Adding the arial font to the class
  hFont = prt.AddFontToEnvironment("Arial Greek",8,8);
  hFont1 = prt.AddFontToEnvironment("Arial Greek",9,9);
  hFont2 = prt.AddFontToEnvironment("Arial Greek",10,10);

  prt.SetMargins(40,160,200,200);
  prt.SetDistance(5);  

  prt.GetDrawDimension(dim);
  prt.GetMargins(margins);
  prt.SetHPos(dim.cy-margins.top);
  // activate a header and footer line
  prt.SetActiveFont (hFont);
  prt.ActivateHF(HeaderFooter);



  // set margins and line spacing
  
  prt.StartPage();
  // start printing the lines
  
 
  prt.Print(hFont,"Set Font Arial Size = 8",FORMAT_NORMAL);    
  prt.Print(hFont,"My name is George Papaioannou",FORMAT_NORMAL);
  prt.Line (PS_SOLID);


  prt.Print(hFont1,"Set Font Arial Size = 9",FORMAT_NORMAL);    
  prt.Print(hFont1,"My name is  George Papaioannou",FORMAT_NORMAL);
  prt.Line (PS_SOLID);


  prt.Print(hFont2,"Set Font Arial Size = 10",FORMAT_NORMAL);  
  prt.Print(hFont2,"My name is  George Papaioannou",FORMAT_NORMAL);
  prt.Line (PS_SOLID);



  // ===================== TABLE 1 ==========================

  prt.lf(hFont2);
  prt.lf(hFont2);
  prt.Print(hFont2,"TABLE 1",FORMAT_CENTER);  
  prt.lf(hFont2);
  prt.lf(hFont2);


  int size[5];
  size[0]=800;
  size[1]=400;
  size[2]=400;
  size[3]=400;
  

  prt.SetTableColumns (3);
  prt.SetTableColumnsSize(size);
  prt.SetTableHorLine (TRUE);
  prt.SetTableVerLine (TRUE);
  prt.SetTableBorderSize (200);
  prt.SetTableJustify(FORMAT_CENTER);
  prt.SetTableBorderLine(5);
  //set the headers
  prt.SetFace (hFont,FACE_NORMALBOLD);
  prt.AddTableRecord (hFont,"NAME|GRADE|CITY",FORMAT_CENTER);
  //set the records
  prt.SetFace (hFont,FACE_NORMAL);
  prt.AddTableRecord (hFont,"George Papaioannou|Grade" 
      " A|Lives at Athens (Greece)",FORMAT_CENTER);
  prt.AddTableRecord (hFont,"Kostas Papadimitriou|" 
      "Grade B|Lives at Larissa (Greece)",FORMAT_CENTER);

  prt.lf(hFont2);
  prt.lf(hFont2);

  prt.Print(hFont2,"AppWizard has created this PrintJob" 
                   " application for you. This application "
                   "not only demonstrates the basics" 
                   " of using the Microsoft Foundation classes "
                   "but is also a starting point for" 
                   " writing your application.",FORMAT_NORMAL);

  // ===================== TABLE 2 ==========================
  prt.lf(hFont2);
  prt.lf(hFont2);
  prt.Print(hFont2,"TABLE 2",FORMAT_CENTER); 
  prt.lf(hFont2);
  prt.lf(hFont2);  


  size[0]=800;
  size[1]=400;
  size[2]=600;
  size[3]=600;
  
  prt.SetTableReset();
  prt.SetTableColumns (4);
  prt.SetTableColumnsSize(size);

  //set the headers
  prt.SetFace (hFont,FACE_NORMALBOLD);
  prt.AddTableRecord (hFont,"NAME|GRADE|CITY|ADDRESS", 
                      FORMAT_CENTER);
  //set the records
  prt.SetFace (hFont,FACE_NORMAL);
  prt.AddTableRecord (hFont,"George Papaioannou|Grade A|" 
      "Athens (Greece)|23 Octobriou",FORMAT_CENTER);
  prt.AddTableRecord (hFont,"Kostas Papadimitriou|Grade B|" 
      "Larissa (Greece)|Irron Polytechneiou",FORMAT_CENTER);


 // =================== TABLE 3 ==============================
  prt.lf(hFont2);
  prt.lf(hFont2);
  prt.Print(hFont2,"TABLE 3",FORMAT_CENTER);  
  prt.lf(hFont2);
  prt.lf(hFont2);

  size[0]=800;
  size[1]=400;
  size[2]=600;
  size[3]=600;
  
  prt.SetTableReset();
  prt.SetTableColumns (4);
  prt.SetTableColumnsSize(size);
  prt.SetTableHorLine (TRUE);  
  prt.SetTableBorderSize (200);
  prt.SetTableJustify(FORMAT_RIGHT);


  //set the headers
  prt.SetFace (hFont,FACE_NORMALBOLD);
  prt.AddTableRecord (hFont,"NAME|GRADE|CITY|ADDRESS",FORMAT_CENTER);
  //set the records
  prt.SetFace (hFont,FACE_NORMAL);
  prt.AddTableRecord (hFont,"George Papaioannou|Grade A|" 
      "Athens (Greece)|23 Octobriou",FORMAT_CENTER);
  prt.AddTableRecord (hFont,"Kostas Papadimitriou|Grade B|" 
      "Larissa (Greece)|Irron Polytechneiou",FORMAT_CENTER);

  // now end the page
  prt.EndPage();

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

You can call the following table methods:

CQPrint::SetTableColumns(int Cols)

SetTableColumns sets the table columns.

prt.SetTableColumns(3);

CQPrint::SetTableBorderSize(int Border)

SetTableBorderSize sets the table border space. Default is 50.

prt.SetTableBorderSize(50);

CQPrint::SetTableColumnsSize(int Cols,int size[])

SetTableColumnsSize sets the table column size. The maximum number of columns permitted is 20.

int size[4];
size[0]=800;
size[1]=400;
size[2]=600;
size[3]=600;

prt.SetTableColumnsSize(4,size);

CQPrint::SetTableHorLine(BOOL HLine)

SetTableHorLine sets the horizontal lines. Default is OFF (no lines).

prt.SetTableHorLine(TRUE);

CQPrint::SetTableVerLine(BOOL HLine)

SetTableVerLine sets the vertical lines. Default is OFF (no lines).

prt.SetTableVerLine(TRUE);

CQPrint::SetTableBorderLine(int Border)

SetTableBorderLine sets the width of the border line. Default is 0 (thin line).

prt.SetTableBorderLine(10);

CQPrint::SetTableJustify(int Justification)

SetTableJustify justifies the table. Default is FORMAT_NORMAL. You can also use FORMAT_CENTER and FORMAT_RIGHT.

prt.SetTableJustify(FORMAT_CENTER);

CQPrint::SetTableReset()

SetTableReset resets all the table parameters to the default state. Use this if you don't want to change values when using consecutive tables with different visual parameters.

prt.SetTableReset();

CQPrint::AddTableRecord(HPRIVATEFONT font, CString strText, UINT Format)

AddTableRecord inserts values to the current table. Use the | symbol to change the column in strText.

prt.AddTableRecord (hFont,"Column 1|Column 2|" 
           "Column 3|Column 4",FORMAT_CENTER);

If you want to change the page orientation, use SetPageOrientation before StartPrint. Use the values DMORIENT_PORTRAIT or DMORIENT_LANDSCAPE.

prt.SetPageOrientation (DMORIENT_PORTRAIT);
prt.StartPrint();

You can always use the "original" methods of the class like SetMargins(), Line(), lf() etc. More details about these functions can be found in the demo. You have to experiment a little before you use it. Don't forget that this class also supports bitmap printing with the InsertBitmap() method. I was wondering if there is someone who can extend this class to support other image formats using GDI+. Maybe the next version of QPrint will support print preview as well as image printing (JPG, GIF, PCX etc.), but it's a little bit complicated, since my wife Natasa is already negative to this perspective. So, don't count on this.

Don't forget that you can always use the callback function ActivateHF() that is used to print headers and footers. I have also included this feature in my demo. Anyway, there are more methods in QPrint that you can find in this short tutorial, so start playing with the demo to get an idea of how this stuff works, and then try to discover the more advanced capabilities that this class supports.

Points of Interest

Please, if you see problems with this class, especially with the (LOGPIXELSY, LOGPIXELSX) width and height of the letters, feel free to contact with me.

History

There is no history for the Quick Print (QPrint) class. This is the first and the only version. The base and the original code for this class was from Rob A. Fraydl and is called CPrivatePrint.

License

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

About the Author

sdancer75
Web Developer Paradox Interactive
Greece Greece
George Papaioannou, Greece
Application & Multimedia Programmer
 
He is a graduate of the Automation Department of Technological Education Institute of Thessaloniki (Greece), specialized in the Industrial Automation (SCADA, embedded Microcontrollers & PLCs) & Programmer / Analyzer of the Aristotelian University specialized in Telecommunication & Computer Networks. He has a great experience in Programming and Systems Analysis on a variety of computer languages like Assembly, C++, Cobol, Fortran, Pascal and Java. He has also worked in researching programs on appreciation and improvement algorithms for detection systems through a coordinated range of Satellite Systems (Global Positioning Systems). He is the Development Manager of his own company "Paradox Interactive", http://www.paradoxinteractive.gr

Comments and Discussions

 
GeneralIt seems QPrint ignore \r\n PinmemberAAron131329-Nov-10 17:45 
GeneralRe: It seems QPrint ignore \r\n Pinmembersdancer7529-Nov-10 22:10 
GeneralProgram crash PinmemberDGit21-Oct-10 6:49 
GeneralRe: Program crash Pinmemberxingrun198919-Dec-12 4:59 
GeneralRe: Program crash Pinmembersdancer7519-Dec-12 20:01 
GeneralRe: Program crash Pinmemberxingrun198919-Dec-12 20:44 
GeneralMy vote of 5 Pinmemberchobychen14-Jul-10 19:51 
QuestionHow do I print without any print dialog [modified] Pinmemberakira3222-Feb-10 16:29 
GeneralPage total PinmemberCanopenR6-Aug-09 3:21 
Questionproblem in [Insert Bitmap] , [Text utf-8 FONT] print [modified] Pinmembermohammadmot18-Jan-09 20:11 
AnswerRe: problem in [Insert Bitmap] , [Text utf-8 FONT] print Pinmembermohammadmot1-Feb-09 3:47 
GeneralProblem compiling QPrint in VS2005. Pinmemberldsdbomber2-Jan-09 0:16 
GeneralRe: Problem compiling QPrint in VS2005. Pinmembericryu28-Jan-09 12:45 
GeneralMultiple lines in a cell PinmemberMember 381507220-Nov-08 17:46 
Generalprinters on c#.net Pinmemberbalu1234530-Jul-08 7:47 
QuestionProblem of Print Preview. Pinmember"_$h@nky_"9-Jul-08 18:09 
GeneralFile saves... Pinmemberdubu8416-Dec-07 23:50 
Generalabout Quick Print version 2 Pinmemberrobbinzou31-May-07 14:51 
GeneralRe: about Quick Print version 2 Pinmembersdancer753-Jun-07 23:43 
QuestionSomebody with it an example of printing of a bitmap ? Pinmemberluckyluche10-May-07 23:29 
Questioncolumn's format? Pinmemberxiaoyu731110-May-07 20:08 
GeneralPrinting on A3 Paper and in color Pinmemberkristina_carter8-Jan-07 11:22 
GeneralRe: Printing on A3 Paper and in color Pinmembersdancer759-Jan-07 2:42 
GeneralRe: Printing on A3 Paper and in color Pinmemberkristina_carter11-Jan-07 15:13 
QuestionDon't show the printer properties dialog Pinmembersinesis14-Dec-06 0:01 

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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 5 May 2006
Article Copyright 2006 by sdancer75
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid