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);
}
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With