Jump to content
  • Advertisement
Sign in to follow this  
Big Sassy

[.net] Having trouble printing range of pages with PrintDialog

This topic is 4833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I'm embarrased that I'm having so much trouble with something so simple, but here it goes. In my code I have a PrintDialog that allows the user to select a page range to print (the AllowSomePages property is set to true), but when a range is selected (ex: pages 2-3) in the dialog it still print all the pages in the document. I've been following the API documentation as well as articles/tutorials online but I still can't get it to work. Multiple copies print fine. Print to File works fine. Just the page range doesn't work. I've tried manually setting the ToPage and FromPage members in the PrintDocument's PrinterSettings and DefaultPrinterSettings members as well as in the PrintDialog's PrinterSettings member. I've also set the MinimumPages and MaximumPages in each one with no success. Here's some test code I've been trying to solve the problem with (...well the latest version anyway). The code is on a WinForm with one button (Button2). I really don't know what I'm doing wrong. Any help at solving my problem would be very appreciated.
Public PageCount As Integer
WithEvents Doc As New PrintDocument

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim Diag As New PrintDialog

    Diag.Document = Doc

    Diag.AllowSomePages = True
    Diag.PrinterSettings.MinimumPage = 1
    Diag.PrinterSettings.MaximumPage = 3
    Diag.PrinterSettings.FromPage = 1
    Diag.PrinterSettings.ToPage = 3

    If Diag.ShowDialog() = DialogResult.OK Then
        PageCount = 1
        Doc.Print()
    End If
End Sub

Private Sub PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Doc.PrintPage

    Dim f As New Font(FontFamily.GenericSerif, 18, FontStyle.Regular)
    e.Graphics.DrawString("Page #" & PageCount, f, Brushes.Black, 10, 10)

    PageCount += 1
    If PageCount <= 3 Then
        e.HasMorePages = True
    End If

End Sub

Share this post


Link to post
Share on other sites
Advertisement
Just setting the To and From page doesn't automatically restrict the printed pages to that. You still need to check to see if what is being printed in on one of the selected pages and either draw or not draw the content.

Share this post


Link to post
Share on other sites
I've tried putting code inside the PrintPage method checking whether the page being printed is in range or not, and drawing or not drawing accordingly. However, when I don't draw it just causes the printer to spit out a blank page instead of just skipping the page.

Share this post


Link to post
Share on other sites
Oh, I understand what you are saying now. You're right. I was under the impression that the .NET framework would automatically print only the pages in the range. I'll have to write code to print only the selected pages. Thanks for your help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!