Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get print page count without printing the document

This is somewhat similar to question about Is there a better way to get the page count from a PrintDocument than this?

But in my case I have a web-browser control with formatted html. At the moment I have option which calls ShowPrintPreviewDialog() so user can see how many pages going to be printed.

Is there anyway to get the no of pages which going to be printed, without launching the PrintPreview?

I am trying to create a method which will call OnTextChange and display print-page count automatically?

I have use PrintPage event

private void PrintDocumentOnPrintPage(object sender, PrintPageEventArgs e)
 {
     e.Graphics.DrawString(this.webBrowser1.DocumentText, this.webBrowser1.Font, Brushes.Black, 10, 25);               
 }
like image 576
huMpty duMpty Avatar asked Jul 08 '14 09:07

huMpty duMpty


People also ask

How can I find out how many pages my printer has printed?

Right-click the icon for your product, and then select Properties from the drop-down menu. Click the General tab, and then click Printing Preferences. Click the Basics tab, and then click About. Locate the page count at the bottom of the About This Driver screen.


1 Answers

Bad news always travels slow at SO. You'll need to scratch the idea that this is practical.

Although unstated in the question, you should have already figured out by now that your PrintPage event handler doesn't work. It always produces a count of 1. That's because you never set the e.HasMorePages property to true, the property that causes more than one page to be generated.

To reliably set that property to true, you need to figure out exactly how the HTML gets rendered by the browser layout engine. And figure out exactly how to break it up into pages that don't cut, say, a line of text or an image in two. And figure out how to this is in the exact same way that the browser printing engine does this. A feat that's been attempted by many a programmer, accomplished by none. The browser's automation object model just doesn't have the needed api.

The only reasonable way is the one you already know. You have to call ShowPrintPreviewDialog(). Which readily displays the page count in the preview dialog, looks like this in IE11:

enter image description here

In case you'd consider snooping that number off the dialog: no, that cannot work either. The dialog doesn't use any controls, it is one monolithic window.

like image 88
Hans Passant Avatar answered Oct 23 '22 22:10

Hans Passant