메뉴 닫기

Printable ListView

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 Graphicsobject 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;
    …
}

39 Comments

  1. how g

    Hey I know this is off topic but I was wondering if you knew of any widgets I could add
    to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would
    have some experience with something like this. Please let me know if you
    run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  2. http://tinyurl.com/cbd-oil-that-works-2020

    Cbd oil that works 2020
    Hey there I am so grateful I found your blog, I really found you
    by error, while I was researching on Bing for something else,
    Regardless I am here now and would just like to say cheers for a fantastic post and a all round interesting blog (I also love the
    theme/design), I don’t have time to look over it all
    at the minute but I have saved it and also added your RSS feeds, so when I
    have time I will be back to read much more, Please do keep up the superb work.

    best cbd oil for pain http://tinyurl.com/cbd-oil-that-works-2020 cbd oil that
    works 2020 http://tinyurl.com/cbd-oil-that-works-2020

  3. Pinkie Rehnborg

    I just want to tell you that I’m newbie to blogging and seriously liked this web site. Most likely I’m likely to bookmark your site . You definitely come with remarkable posts. Thanks a lot for sharing with us your blog.

  4. buy viagra online no prescription

    The next time I review a blog, I really hope that it doesn’t dissatisfy me as high as this one. I indicate, I recognize it was my option to check out, yet I actually assumed youd have something fascinating to state. All I listen to is a number of yawping concerning something that you can repair if you werent too busy seeking attention.

  5. security screen mesh

    Do you mind if I quote a few of your posts as long as I provide credit and sources back to your website? My blog is in the very same niche as yours and my users would truly benefit from a lot of the information you present here. Please let me know if this alright with you. Thanks!

  6. cbd oil for sale from florida

    An outstanding share, I just provided this onto a coworker who was doing a little analysis on this. And also he as a matter of fact purchased me breakfast since I found it for him. smile. So let me reword that: Thnx for the reward! However yeah Thnkx for investing the moment to discuss this, I feel highly concerning it and enjoy learning more on this topic. If possible, as you become knowledge, would you mind upgrading your blog with even more information? It is highly valuable for me. Large thumb up for this blog post!

  7. cbd flower for sale florida

    This is the best blog for any individual that wants to find out about this subject. You recognize so much its virtually difficult to argue with you (not that I in fact would want?HaHa). You absolutely placed a new spin on a topic thats been covered for many years. Wonderful things, simply wonderful!

  8. contraindications 4 carprofen and cbd oil for dogs

    An impressive share, I just provided this onto an associate that was doing a little analysis on this. And he in fact purchased me morning meal since I located it for him. smile. So let me reword that: Thnx for the treat! However yeah Thnkx for investing the moment to review this, I really feel highly regarding it and love reading more on this topic. When possible, as you end up being expertise, would you mind updating your blog with even more information? It is highly valuable for me. Large thumb up for this blog post!

  9. Winnie Decourley

    Youre so awesome! I do not suppose Ive check out anything such as this before. So nice to find someone with some original thoughts on this subject. realy thanks for beginning this up. this web site is something that is needed online, a person with a little creativity. beneficial work for bringing something brand-new to the net!

  10. Prince Keagy

    Can I simply say what a relief to find a person that in fact knows what theyre speaking about online. You absolutely know exactly how to bring a problem to light and make it important. More individuals need to read this and also comprehend this side of the story. I cant believe youre not a lot more preferred because you absolutely have the present.

  11. here.

    I?m amazed, I need to state. Really seldom do I come across a blog site that?s both educative and also enjoyable, as well as let me tell you, you have actually hit the nail on the head. Your concept is exceptional; the concern is something that inadequate people are talking intelligently around. I am extremely delighted that I came across this in my look for something connecting to this.

  12. official statement

    I?m satisfied, I have to claim. Really rarely do I run into a blog that?s both informative and also amusing, as well as let me tell you, you have struck the nail on the head. Your idea is superior; the problem is something that insufficient people are talking intelligently about. I am very delighted that I came across this in my look for something relating to this.

  13. more..

    Youre so trendy! I do not expect Ive read anything similar to this before. So good to locate someone with some original ideas on this subject. realy thank you for beginning this up. this site is something that is needed on the web, somebody with a little creativity. helpful work for bringing something new to the internet!

  14. Google

    Good post. I learn something totally new and challenging on sites I stumbleupon every day. It will always be interesting to read content from other writers and use a little something from their websites.

  15. try this web-site

    After study a few of the article on your web site now, as well as I genuinely like your means of blog writing. I bookmarked it to my bookmark internet site list and also will be checking back quickly. Pls take a look at my internet site also as well as let me know what you think.

댓글 남기기

이메일은 공개되지 않습니다.