C# WinForms: Breaking up a PrintDocument into pages?

Posted on 2012-09-09
Last Modified: 2012-09-10
I have the code for the attached file printing fine (the word 'test'), but I need to find out how to parse my text file document into individual pages.

How does that work?

As you can see in the demo which I have working, this line:

e.Graphics.DrawString("test",new Font(FontFamily.GenericSerif,12),

prints one word.  I have a multi-page text file.  Must I iterate through the lines and pass each line into DrawString()?  Is there an easier way?

Also, if I am to iterate, then I presume I must also determine if I am near a page break.  And this is starting to sound like a Printer Device Context object, which I hope I do not need to deal with.

Please let me know.

Question by:newbieweb
    LVL 96

    Accepted Solution

    When working with the PrintDocument, you handle the Print event for each page printed.  If you need multiple pages, then set e.HasMorePages = true, until you are done.
    LVL 40

    Assisted Solution

    by:Jacques Bourgeois (James Burger)
    Printing with the PrintDocument is a pain, so do it only as a last resource. That is why report generators are so popular. Personnally, I usually use Microsoft Word when I need to control printing, because it takes care of a lot of stuff such as margins and page breaks automatically.

    With the PrintDocument have to know where the cursor is and move it by giving new coordinates for each line that you want to print.

    You have to take care of where you are in the page, and are the one that tells the system that a new page need to be printed, as TheLearnedOne told you.

    Then, the Print event will be fired for the new page, and it is up to you to continue from where you where in the data after the previous page.

    Author Closing Comment

    Thanks.  I had already discovered:

     e.HasMorePages = true

    is key.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    6 Experts available now in Live!

    Get 1:1 Help Now