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

Printable ListView

By , 19 Sep 2005
 

Demo application

List Print Preview

Introduction

Some time during my development works I had the need of printing the contents of a ListView control. So I wrote down the code for a custom control, named PrintableListView, that solved the following two problems:

  • extend the print on multiple pages when the width of the list exceeds the width of the print page,
  • shrink, optionally, the list in a way that the resulting width fits on a single page.

Using the code

PrintableListView control is derived from System.Windows.Forms.ListView, so use it like the native ListView control. The additional functionalities are those for print and for print preview.

// call the print method for displaying the standard print dialog.
this.m_list.Print();


// call the print preview method for displaying
// the standard print preview dialog
this.m_list.PrintPreview();

To turn on the “Fit to page” option, use the FitToPage property of the control.

this.m_list.FitToPage = true;

To set the title that appears on the page header, use the Title property of the control.

Points of Interest

If you think about the ListView control, you will note that we have all the information we need to print it in the simplest way. We have the columns width, the fonts, the rows height and so on. All we have to do is to get all these information and play a little with the Graphics instance that represents the printing surface. What follows is the way I solved the problems listed in the Introduction.

Multi page print

This is quite simple. When we are printing the current column of a row, we check whether there is enough room to print it. If not, we mark that column as the first one for the next page. That’s it.

Fit to page

This point is just a little bit more complex compared to the previous one. If the total width of the list is greater than the width of the available space, we have to calculate a scaling factor, given by the list width divided by the available space. For simplicity, in my implementation, I converted all the measures in units of hundredths of an inch. To set the scale factor and unit of measure is sufficient to change the coordinate system of the Graphics object passed to the handler of the PrintPage event of the PrintDocument object.

private void OnPrintPage(object sender, PrintPageEventArgs e)
{
    …
    if (m_nStartCol==0 && m_bFitToPage && m_fListWidth>rectBody.Width)
    {
        // Calculate scale factor
        fScale = rectBody.Width / m_fListWidth;
    }
    …
    …
    // Setting scale factor and unit of measure
    g.ScaleTransform(fScale, fScale);
    g.PageUnit = GraphicsUnit.Inch;
    g.PageScale = 0.01f;
    …
}

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

Matteo D'Avena
Web Developer MerchandisingPlaza.com
Italy Italy
I am from Apricena, a small town in Puglia, Italy. I graduated at the University of Bari in 1998 in Computer Science. I worked for seven years in the field of business intelligence with one of the most important italian group producing software and services for the banking and financial sector.
 
For four year I had been a freelance software developer and consultant.
 
Now I am the leading project manager for MerchandisingPlaza.com!
 
Thanks very much.

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThanksmemberMember 1002138830-Apr-13 20:59 
Questionhow can i apply thismemberdemir12318-Apr-13 5:53 
QuestionlistviewmemberMember 844235920-Nov-12 20:34 
QuestionHELP PLEASE URGENT!!memberMember 844235919-Nov-12 18:54 
QuestionLicencesmember_Primrose1-Oct-12 5:16 
AnswerRe: LicencesmemberMatteo D'Avena2-Oct-12 1:59 
Questionsolvedmemberjacob waswa11-Aug-12 21:19 
QuestionURGENT HELP NEEDEDmemberjacob waswa11-Aug-12 20:33 
Questiongr8 Job ... thxmemberMember 261443618-Nov-11 20:14 
QuestionFitToPage bug [modified]memberwcm77713-Oct-11 6:36 
Questionprint with back color of the list viewmembermahmoud19907-Sep-11 1:32 
General{"InvalidArgument=Value of '2' is not valid for 'index'.\r\nParameter name: index"}memberg1xx3r3-Jun-11 2:51 
QuestionRight-to-leftmemberKassem Bagher6-Feb-11 13:14 
AnswerRe: Right-to-leftmemberPafia25-Feb-11 2:40 
Hi,
For Right-to-Left, change line 252 in "PrintableListView.cs" file (OnPrintPage methode) to below code:
 
for (int i = Columns.Count-1; i >= m_nStartCol; i--)
 
In addition, this is simplest way, for more functionality and efficiency learn GDI+ and change overloaded methode "OnPrintPage".
GeneralMy vote of 5memberjcarter12121211-Oct-10 12:21 
GeneralMy vote of 5memberDon_Hard11-Jul-10 2:36 
GeneralGood Articlemembergary_gsv15-Jun-10 17:46 
GeneralAnd with checkboxesmemberAdrian20993-Mar-10 7:48 
GeneralImages in ListView Print Previewmemberhellbinder5-Nov-09 7:49 
GeneralPrinting a ListView that contains groupsmemberMark F.23-Mar-09 9:32 
QuestionWhat's the license associated with your code?memberZaki Saadeh27-May-08 6:24 
Questionhow i can bind treeview With My Table in Databasememberwaleedmaklad9-Dec-07 9:35 
Questionhow can i bind my table with treeview In C#susswaleed9-Dec-07 9:32 
Questionhow to select printer?memberSuha18-Nov-07 12:32 
QuestionRight-to-leftmemberSohailB17-Nov-07 2:28 
GeneralThanks, butmemberbart_net_master20-Aug-07 3:17 
Generalhttp://puzzle.tangoing.info/susshttp://puzzle.tangoing.info/7-Dec-07 10:29 
GeneralGreat article...Thanks...memberAbhishek_Gajab14-Aug-07 2:42 
GeneralInvalidArgument=Value of '1' is not valid for 'index'.memberJordan Marrazzo4-Jul-07 17:59 
GeneralRe: InvalidArgument=Value of '1' is not valid for 'index'.memberJordan Marrazzo4-Jul-07 18:33 
QuestionHow to print selected pages using this Printable ListView controlmemberdurgamic10-Apr-07 1:08 
QuestionHow can i select required pagesmemberdurgamic10-Apr-07 1:05 
QuestionPrint 2 listviews on same page [modified]membercsfong3314-Mar-07 18:10 
GeneralListview printmemberGeorgiio7-Jan-07 10:20 
GeneralPrinting with diffent dongsmemberenlikil27-Oct-06 5:44 
GeneralAbout languagememberjabulino19-Mar-06 20:44 
QuestionColumn ordermemberColin Parker11-Oct-05 1:07 
AnswerRe: Column ordermemberMircea Puiu11-Oct-05 1:43 
GeneralRe: Column ordermemberColin Parker11-Oct-05 1:54 
GeneralRe: Column ordermemberMircea Puiu11-Oct-05 2:43 
Generalpippaguru...memberJonathanLivingstone19-Sep-05 20:41 
GeneralRe: pippaguru...memberMatteo D'Avena19-Sep-05 20:57 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 19 Sep 2005
Article Copyright 2005 by Matteo D'Avena
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid