7

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);               
 }
Community
  • 1
  • 1
huMpty duMpty
  • 13,481
  • 12
  • 52
  • 88
  • I think you have your answer in the [answer to the linked question](http://stackoverflow.com/a/11456713/1552016): it's a static method you can call in you text change event handler, and it won't display any dialogs at all. – qqbenq Jul 08 '14 at 09:50
  • @qqbenq: if use `StandardPrintController` it works fine but not the `PreviewPrintController`, So the answer doesn't help – huMpty duMpty Jul 08 '14 at 12:54

1 Answers1

10

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.

Hans Passant
  • 873,011
  • 131
  • 1,552
  • 2,371