I am trying to print webpage which is loaded in 'DotNetBrowser.Browser', as PDF document and save locally using DotNetBrowser PrintHandler. This works fine in a windows form application with WinFormsBrowserView but in case of console application, the PrintHandler does not print the entire page i.e. only some parts of the page is printed and saved as pdf.
Asked
Active
Viewed 439 times
2
-
Could you please add the code that demonstrates how the print handler is implemented in your application? – Anna Dolbina Jan 06 '17 at 17:33
-
public void PrintPDF(string sFileName) { browser.PrintHandler = new MyPDFPrintHandler((printSettings) => { printSettings.PrintToPDF = true; printSettings.PDFFilePath = sFileName; return printSettings; }); browser.Print(); } – SR_P Jan 07 '17 at 05:27
-
partial class MyPDFPrintHandler : PrintHandler { Func
func; public MyPDFPrintHandler(Func – SR_P Jan 07 '17 at 05:30func) { this.func = func; } public PrintStatus OnPrint(PrintJob printJob) { PrintSettings printSettings = func(printJob.PrintSettings); printSettings.PrintToPDF = true; printSettings.Landscape = true; printSettings.PrintBackgrounds = true; return PrintStatus.CONTINUE; } } -
The webpage has a huge table in it with 50 rows and paging, but the saved pdf shows only the header and footer of the table with no rows while running in console application. But the same webpage prints properly while running in Winform application – SR_P Jan 07 '17 at 05:35
2 Answers
1
Finally i was able to print PDF in console application by setting the initial size to the browser.
browserView.Browser.SetSize(1280, 1024)
This is what i was missing in my code. Since the browser is not shown, setting the initial size solved the problem of incomplete pdf printing.

SR_P
- 79
- 6
0
Here is the sample code that demonstrates how to load the website and print it to PDF:
using DotNetBrowser;
using DotNetBrowser.Events;
using System;
using System.Threading;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
using (var browser = BrowserFactory.Create())
{
ManualResetEvent waitEvent = new ManualResetEvent(false);
browser.FinishLoadingFrameEvent += delegate(object sender, FinishLoadingEventArgs e)
{
// Wait until main document of the web page is loaded completely.
if (e.IsMainFrame)
{
waitEvent.Set();
}
};
browser.LoadURL("https://www.teamdev.com/dotnetbrowser");
waitEvent.WaitOne();
PrintPDF(browser, System.IO.Path.GetFullPath(@"test.pdf"));
}
}
public static void PrintPDF(Browser browser, string sFileName) {
ManualResetEvent waitEvent = new ManualResetEvent(false);
var handler = new MyPDFPrintHandler((printJob) => {
var printSettings = printJob.PrintSettings;
printSettings.PrintToPDF = true;
printSettings.PDFFilePath = sFileName;
printJob.PrintJobEvent += (s, e) =>
{
System.Diagnostics.Debug.WriteLine("Printing done: " + e.Success);
waitEvent.Set();
};
return printSettings;
});
browser.PrintHandler = handler;
browser.Print();
waitEvent.WaitOne();
}
class MyPDFPrintHandler : PrintHandler
{
Func<PrintJob, PrintSettings> func;
public MyPDFPrintHandler(Func<PrintJob, PrintSettings> func)
{
this.func = func;
}
public PrintStatus OnPrint(PrintJob printJob)
{
PrintSettings printSettings = func(printJob);
printSettings.PrintToPDF = true;
printSettings.Landscape = true;
printSettings.PrintBackgrounds = true;
return PrintStatus.CONTINUE;
}
}
}
}
As you can see, there is a synchronous printing - the method does not return until the web page is printed completely.
Could you please test this code with your website and let me know the results?

Anna Dolbina
- 1
- 1
- 8
- 9
-
I'v tried your above code but it still gives me incomplete print. I just notices that if i user BrowserType.LIGHTWEIGHT in winformapplication, print handler saves incomplete pdf, just as in case of console application. Would like to add that I'v used BrowserType.HEAVYWEIGHT in the console application. – SR_P Jan 10 '17 at 10:51
-
Is it possible to provide the URL of the page for which this issue is reproducible? – Anna Dolbina Jan 11 '17 at 12:45