Receive a printout of a page without printing a document

This is somewhat similar to the question about Is there a better way to get the number of pages from PrintDocument than this?

But in my case, I have a web browser control with formatted html. At the moment, I have an option that calls ShowPrintPreviewDialog() so that the user can see how many pages will be printed.

In any case, to get the number of pages to be printed without running PrintPreview ?

I am trying to create a method that will call OnTextChange and automatically display a printout of the page

I am using the PrintPage event

 private void PrintDocumentOnPrintPage(object sender, PrintPageEventArgs e) { e.Graphics.DrawString(this.webBrowser1.DocumentText, this.webBrowser1.Font, Brushes.Black, 10, 25); } 
+7
c # printing print-preview
source share
1 answer

The bad news is always moving slowly in SO. You need to scratch the idea that it is practical.

Although this question was not asked, you should already have realized that your PrintPage event handler was not working. It always counts 1. This is because you never set the e.HasMorePages property to true, a property that creates more than one page.

To reliably set this property to true, you need to find out exactly how HTML is processed by the browser layout engine. And figure out how to break it into pages that don't cut, say, a line of text or an image in half. And find out how to do it in the exact way that the browser print engine does it. A feat that many programmers tried to do, which no one achieved. The browser automation object model simply does not have the required api.

The only sensible way is the one you already know. You must call ShowPrintPreviewDialog (). Which easily displays the number of pages in the preview dialog, looks like this in IE11:

enter image description here

In case you consider tracking this number in a dialog box: no, this will not work either. The dialog does not use any controls, it is one monolithic window.

+10
source share

All Articles