DotNetBrowser Printing API allows you to decide whether web page should be printed using printer device or saved as PDF document.


To save web page as PDF document you must register your own implementation of PrintHandler where you override default print settings and tell Chromium engine to save web page as PDF document at the specified path on the local file system. For example:

C#

class MyPDFPrintHandler : PrintHandler
{
  Func<PrintSettings, PrintSettings> func;

  public MyPDFPrintHandler(Func<PrintSettings, PrintSettings> func)
  {
    this.func = func;
  }

  public PrintStatus OnPrint(PrintJob printJob)
  {
    PrintSettings printSettings = func(printJob.PrintSettings);
    printSettings.PrintToPDF = true;
    printSettings.Landscape = true;
    printSettings.PrintBackgrounds = true;
    printSettings.PageMargins = new PageMargins(20, 40, 40, 20);
    return PrintStatus.CONTINUE;
  }
}


browserView.Browser.PrintHandler = new MyPDFPrintHandler((printSettings) =>
{
  printSettings.PDFFilePath = dlg.FileName;
  return printSettings;
});
browserView.Browser.Print();


VB.NET

Class MyPDFPrintHandler
    Implements PrintHandler

    Dim func As Func(Of PrintSettings, PrintSettings)

    public Sub New(func As Func( Of PrintSettings, PrintSettings))
        Me.func = func
    End Sub

    Public Function OnPrint(printJob As PrintJob) As PrintStatus Implements PrintHandler.OnPrint
        Dim printSettings As PrintSettings = func(printJob.PrintSettings)
        printSettings.PrintToPDF = True
        printSettings.Landscape = True
        printSettings.PrintBackgrounds = True
        printSettings.PageMargins = New PageMargins(20, 40, 40, 20)
        Return PrintStatus.CONTINUE
    End Function
End Class


browserView.Browser.PrintHandler = New MyPDFPrintHandler(Function(printSettings)
    printSettings.PDFFilePath = dlg.FileName
    Return printSettings
End Function)



Once you register PrintHandler that saves web page as PDF document, you can initiate printing using one of the following ways:

  • By invoking the Browser.Print() method.
  • By invoking the window.print() JavaScript function.


Now, every time when printing is requested from .NET or JavaScript code, the PrintHandler will be used where you configure print settings to save web page as PDF document at specified file on the local file system.

C#

using DotNetBrowser;
using DotNetBrowser.Events;
using DotNetBrowser.WPF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace PrintToPDFSample
{
    class MyPDFPrintHandler : PrintHandler
    {
        Func<PrintSettings, PrintSettings> func;

        public MyPDFPrintHandler(Func<PrintSettings, PrintSettings> func)
        {
            this.func = func;
        }

        public PrintStatus OnPrint(PrintJob printJob)
        {
            PrintSettings printSettings = func(printJob.PrintSettings);
            printSettings.PrintToPDF = true;
            printSettings.Landscape = true;
            printSettings.PrintBackgrounds = true;
            printSettings.PageMargins = new PageMargins(20, 40, 40, 20);
            return PrintStatus.CONTINUE;
        }
    }

    public class WindowMain : Window
    {
        public WPFBrowserView browserView;

        private Button printButton;
        private Grid layout;

        public WindowMain()
        {
            layout = new Grid();
            ColumnDefinition gridCol1 = new ColumnDefinition();
            layout.ColumnDefinitions.Add(gridCol1);
            RowDefinition gridRow1 = new RowDefinition();
            gridRow1.Height = new GridLength(45);
            RowDefinition gridRow2 = new RowDefinition();

            layout.RowDefinitions.Add(gridRow1);
            layout.RowDefinitions.Add(gridRow2);

            Content = layout;

            printButton = new Button();
            printButton.Content = "Print PDF";
            printButton.Height = 23;
            printButton.Click += printButton_Click;
            Grid.SetRow(printButton, 0);
            Grid.SetColumn(printButton, 0);

            browserView = new WPFBrowserView(BrowserFactory.Create());
            Grid.SetRow(browserView, 1);
            Grid.SetColumn(browserView, 0);

            layout.Children.Add(printButton);
            layout.Children.Add(browserView);

            Width = 1024;
            Height = 768;

            this.Loaded += WindowMain_Loaded;
        }

        void WindowMain_Loaded(object sender, RoutedEventArgs e)
        {
            browserView.Browser.LoadURL("http://www.teamdev.com/services");
        }

        void printButton_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "document";
            dlg.DefaultExt = ".pdf";
            dlg.Filter = "PDF documents (.pdf)|*.pdf";
            dlg.Title = "Save page as PDF document";
            var res = dlg.ShowDialog();
            if (res.HasValue && res.Value)
            {
                browserView.Browser.PrintHandler = new MyPDFPrintHandler((printSettings) =>
                {
                    printSettings.PDFFilePath = dlg.FileName;
                    return printSettings;
                });
                browserView.Browser.Print();
            }
        }

        [STAThread]
        public static void Main()
        {
            Application app = new Application();

            WindowMain wnd = new WindowMain();
            app.Run(wnd);

            var browser = wnd.browserView.Browser;
            wnd.browserView.Dispose();
            browser.Dispose();
        }
    }
}


VB.NET

Imports System.Windows
Imports System.Windows.Controls
Imports DotNetBrowser
Imports DotNetBrowser.WPF
Imports Microsoft.Win32

Class MyPDFPrintHandler
    Implements PrintHandler

    Dim ReadOnly func As Func(Of PrintSettings, PrintSettings)

    Public Sub New(func As Func(Of PrintSettings, PrintSettings))
        Me.func = func
    End Sub

    Public Function OnPrint(printJob As PrintJob) As PrintStatus Implements PrintHandler.OnPrint
        Dim printSettings As PrintSettings = func(printJob.PrintSettings)
        printSettings.PrintToPDF = True
        printSettings.Landscape = True
        printSettings.PrintBackgrounds = True
        printSettings.PageMargins = New PageMargins(20, 40, 40, 20)
        return PrintStatus.CONTINUE
    End Function
End Class

Public Class WindowMain
    Inherits Window

    Public Dim browserView As WPFBrowserView

    Dim ReadOnly printButton As Button
    Dim ReadOnly layout As Grid

    Public Sub New()
        layout = new Grid()
        Dim gridCol1 = New ColumnDefinition()
        layout.ColumnDefinitions.Add(gridCol1)

        Dim gridRow1 = New RowDefinition()
        gridRow1.Height = new GridLength(45)
        Dim gridRow2 = New RowDefinition()
        layout.RowDefinitions.Add(gridRow1)
        layout.RowDefinitions.Add(gridRow2)

        Content = layout

        printButton = new Button()
        printButton.Content = "Print PDF"
        printButton.Height = 23
        AddHandler printButton.Click, AddressOf printButton_Click

        Grid.SetRow(printButton, 0)
        Grid.SetColumn(printButton, 0)

        browserView = new WPFBrowserView(BrowserFactory.Create())
        Grid.SetRow(browserView, 1)
        Grid.SetColumn(browserView, 0)

        layout.Children.Add(printButton)
        layout.Children.Add(browserView)

        Width = 1024
        Height = 768

        AddHandler Me.Loaded, AddressOf  WindowMain_Loaded
    End Sub

    Private Sub WindowMain_Loaded(sender As Object, e As RoutedEventArgs)
        browserView.Browser.LoadURL("http://www.teamdev.com/services")
    End Sub

    Private Sub printButton_Click(sender As Object, e As RoutedEventArgs)
        Dim dlg = New SaveFileDialog()
        dlg.FileName = "document"
        dlg.DefaultExt = ".pdf"
        dlg.Filter = "PDF documents (.pdf)|*.pdf"
        dlg.Title = "Save page as PDF document"
        Dim res = dlg.ShowDialog()
        If (res.HasValue And res.Value) Then

            browserView.Browser.PrintHandler = new MyPDFPrintHandler(Function(printSettings)

                printSettings.PDFFilePath = dlg.FileName
                return printSettings
            End Function)
            browserView.Browser.Print()
        End If
    End Sub
End Class

Module PrintToPDFSample
    <STAThread>
    Sub Main()
        Dim app = New Application()

        Dim wnd = New WindowMain()
        app.Run(wnd)

        Dim browser = wnd.browserView.Browser
        wnd.browserView.Dispose()
        browser.Dispose()
    End Sub
End Module


This code sample demonstrates how to print the web page to the pdf file in offscreen mode:

C#

using System;
using System.IO;
using System.Threading;
using DotNetBrowser;

namespace OffscreenPrintToPDFSample
{
    class Program
    {
        [STAThread]
        static void Main()
        {
            Browser browser = BrowserFactory.Create(BrowserType.LIGHTWEIGHT);
            string pdfFilePath = Path.GetFullPath("sample.pdf");

            browser.PrintHandler = new MyPDFPrintHandler((printSettings) =>
            {
                printSettings.PDFFilePath = pdfFilePath;
                return printSettings;
            });

            ManualResetEvent waitEvent = new ManualResetEvent(false);
            browser.FinishLoadingFrameEvent += (s, e) =>
            {
                if (e.IsMainFrame)
                {
                    browser.Print();
                    Console.WriteLine("The web site was printed to the pdf file: "
                                      + pdfFilePath);
                    waitEvent.Set();
                }
            };
            browser.LoadURL("http://www.teamdev.com/services");

            Console.WriteLine("Start printing...");
            waitEvent.WaitOne();

            Console.WriteLine("Press any key to quit");
            Console.ReadKey();
            browser.Dispose();
        }

        internal class MyPDFPrintHandler : PrintHandler
        {
            private readonly Func<PrintSettings, PrintSettings> func;

            public MyPDFPrintHandler(Func<PrintSettings, PrintSettings> func)
            {
                this.func = func;
            }

            public PrintStatus OnPrint(PrintJob printJob)
            {
                PrintSettings printSettings = func(printJob.PrintSettings);
                printSettings.PrintToPDF = true;
                printSettings.Landscape = true;
                printSettings.PrintBackgrounds = true;
                printSettings.PageMargins = new PageMargins(20, 40, 40, 20);
                return PrintStatus.CONTINUE;
            }
        }
    }
}


VB.NET

Imports System.IO
Imports System.Threading
Imports DotNetBrowser

Module OffscreenPrintToPDFSample
    <STAThread>
    Sub Main()
        Dim browser As Browser = BrowserFactory.Create(BrowserType.LIGHTWEIGHT)
        Dim pdfFilePath As String = Path.GetFullPath("sample.pdf")

        browser.PrintHandler = New MyPDFPrintHandler(Function(printSettings)
            printSettings.PDFFilePath = pdfFilePath
            Return printSettings
        End Function)

        Dim waitEvent = New ManualResetEvent(False)
        AddHandler browser.FinishLoadingFrameEvent, Sub(s, e)
            If (e.IsMainFrame) Then
                browser.Print()
                Console.WriteLine("The web site was printed to the pdf file: " + pdfFilePath)
                waitEvent.Set()
            End If
        End Sub
        browser.LoadURL("http://www.teamdev.com/services")

        Console.WriteLine("Start printing...")
        waitEvent.WaitOne()

        Console.WriteLine("Press any key to quit")
        Console.ReadKey()
        browser.Dispose()
    End Sub
End Module

Friend Class MyPDFPrintHandler
    Implements PrintHandler

    Dim ReadOnly func As Func(Of PrintSettings, PrintSettings)

    Public Sub New(func As Func(Of PrintSettings, PrintSettings))
        Me.func = func
    End Sub

    Public Function OnPrint(printJob As PrintJob) As PrintStatus Implements PrintHandler.OnPrint
        Dim printSettings As PrintSettings = func(printJob.PrintSettings)
        printSettings.PrintToPDF = True
        printSettings.Landscape = True
        printSettings.PrintBackgrounds = True
        printSettings.PageMargins = New PageMargins(20, 40, 40, 20)
        return PrintStatus.CONTINUE
    End Function
End Class