1 Replies - 413 Views - Last Post: 15 December 2019 - 05:07 PM Rate Topic: -----

#1 Nicomendox   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-March 16

How can I get PrintDocument pageCount without printpreviewdialog ?

Posted 13 December 2019 - 06:19 PM

I just need to get Printdocument PageCount. I like to get it only with Button_click with in a messagebox. I can get result only if I use with calling PrintPreviewDialog.showDialog windows.
Is This A Good Question/Topic? 0
  • +

Replies To: How can I get PrintDocument pageCount without printpreviewdialog ?

#2 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1538
  • View blog
  • Posts: 3,869
  • Joined: 01-February 13

Re: How can I get PrintDocument pageCount without printpreviewdialog ?

Posted 15 December 2019 - 05:07 PM

@ Nicomendox,
You can use the PreviewPrintController Class to do this. If you already have your PrintDocument created and have it's PrintPage event all setup to print your pages, you can do the following...

First, save a backup of the PrintDocument's PrintController in a local variable. Then assign a new instance of a PreviewPrintController to the PrintDocument's PrintController property. Now you can call the PrintDocument's Print method. Then grab the number of PreviewPageInfo classes that where created by calling the GetPreviewPageInfo method of the PreviewPrintController. There should be 1 for each page. Each PreviewPageInfo class holds an image of one page. Last, assign the backup PrintController back to the PrintDocument's PrintController property.

In this example, I have already created my PrintDocument (PrintDocument1) and set up it's PrintPage event. Then I just used this in a Button Click event to show a MessageBox to tell how many pages there are to print.
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim DefaultCtrlr As PrintController = PrintDocument1.PrintController 'Save a backup of PrintDocument1's default PrintController

        Dim PrvPrntCtrlr As New PreviewPrintController 'Create a New instance of a PreviewPrintController class
        PrintDocument1.PrintController = PrvPrntCtrlr 'Assign the new PreviewPrintController to PrintDocument1's PrintController property

        PrintDocument1.Print() 'Call the Print method of PrintDocument1. This call will block until all the pages are printed to images in the PreviewPrintController class

        'Now you can call the GetPreviewPageInfo method of the PreviewPrintController to get an array of PreviewPageInfo classes. There will be one for each page.
        MessageBox.Show(PrvPrntCtrlr.GetPreviewPageInfo.Length.ToString & " Page(s) To Print.")

        PrintDocument1.PrintController = DefaultCtrlr 'Re-Assign the default PrintController to PrintDocument1's PrintController property
    End Sub



I should also add that this will not show any dialog windows or messagebox. However, it operates just like a PreviewDialog as in it has to run the Print method to actually print the pages to images. You can use them if you like but, if you just want the number of pages to print, then you can just get the number of the image array. I explained that in the code comments.

This post has been edited by IronRazer: 16 December 2019 - 01:26 PM
Reason for edit:: Commented Code and added more info,

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1