Print Dialog Example for WPF

Nevron NOV UserInterface for WPF ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WPF UserInterface examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Print Dialog Example for WPF Description

This example demonstrates how to create and use the print dialog provided by NOV.


Print Dialog Example

C# Print Dialog Example Code for Nevron UserInterface for WPF
Language: C#

using Nevron.Nov;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NPrintDialogExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			NButton printButton = new NButton("Print...");
			printButton.HorizontalPlacement = ENHorizontalPlacement.Left;
			printButton.VerticalPlacement = ENVerticalPlacement.Top;
			printButton.Click += OnPrintButtonClick;

			return printButton;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();

			// print range mode
			m_PrintRangeModeComboBox = new NComboBox();
			m_PrintRangeModeComboBox.Items.FillFromEnum();
			m_PrintRangeModeComboBox.SelectedIndex = 0;
			stack.Add(new NPairBox("Print Range Mode:", m_PrintRangeModeComboBox, true));

			// enable current page
			m_EnableCurrentPageCheckBox = new NCheckBox();
			stack.Add(new NPairBox("Enable Current Page:", m_EnableCurrentPageCheckBox, true));

			// enable selection
			m_EnableSelectionCheckBox = new NCheckBox();
			stack.Add(new NPairBox("Enable Selection:", m_EnableSelectionCheckBox, true));

			// enable custom page range
			m_EnableCustomPageRangeCheckBox = new NCheckBox();
			stack.Add(new NPairBox("Enable Custom Page Range:", m_EnableCustomPageRangeCheckBox, true));

			// collate
			m_CollateCheckBox = new NCheckBox();
			stack.Add(new NPairBox("Collate:", m_CollateCheckBox, true));

			// number of copies
			m_NumberOfCopiesUpDown = new NNumericUpDown();
			m_NumberOfCopiesUpDown.DecimalPlaces = 0;
			m_NumberOfCopiesUpDown.Step = 1;
			m_NumberOfCopiesUpDown.Minimum = 1;
			m_NumberOfCopiesUpDown.Maximum = 100;
			stack.Add(new NPairBox("Number of Copies:", m_NumberOfCopiesUpDown, true));

			return new NUniSizeBoxGroup(stack);
		}
		public string GetExampleDescription()
		{
			return @"
				

This example demonstrates how to create and use the print dialog provided by NOV.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Common Dialogs", "https://www.nevron.com/docs/openvision/ui-common-dialogs.html"), ("Top Level Windows", "https://www.nevron.com/docs/openvision/ui-top-level-windows.html"), }; } #endregion #region Implementation #endregion #region Event Handlers private void OnPrintButtonClick(NEventArgs args) { NPrintDocument printDocument = new NPrintDocument(); printDocument.DocumentName = "Test Document 1"; printDocument.BeginPrint += OnBeginPrint; printDocument.PrintPage += OnPrintPage; printDocument.EndPrint += OnEndPrint; NPrintDialog printDialog = new NPrintDialog(); printDialog.PrintRangeMode = (ENPrintRangeMode)m_PrintRangeModeComboBox.SelectedItem.Tag; printDialog.EnableCustomPageRange = m_EnableCustomPageRangeCheckBox.Checked; printDialog.EnableCurrentPage = m_EnableCurrentPageCheckBox.Checked; printDialog.EnableSelection = m_EnableSelectionCheckBox.Checked; printDialog.CustomPageRange = new NRangeI(1, 100); printDialog.NumberOfCopies = (int)m_NumberOfCopiesUpDown.Value; printDialog.Collate = m_CollateCheckBox.Checked; printDialog.PrintDocument = printDocument; printDialog.Closed += OnPrintDialogClosed; printDialog.RequestShow(); } private void OnBeginPrint(NPrintDocument sender, NBeginPrintEventArgs e) { } private void OnEndPrint(NPrintDocument sender, NEndPrintEventArgs e) { } private void OnPrintPage(NPrintDocument sender, NPrintPageEventArgs e) { NSize pageSizeDIP = new NSize(this.Width, this.Height); try { NMargins pageMargins = NMargins.Zero; NRegion clip = NRegion.FromRectangle(new NRectangle(0, 0, e.PrintableArea.Width, e.PrintableArea.Height)); NMatrix transform = new NMatrix(e.PrintableArea.X, e.PrintableArea.Y); NPaintVisitor visitor = new NPaintVisitor(e.Graphics, 300, transform, clip); // forward traverse the display tree m_PrintRangeModeComboBox.DisplayWindow.VisitDisplaySubtree(visitor); e.HasMorePages = false; } catch (Exception x) { NMessageBox.Show(x.Message, "Exception", ENMessageBoxButtons.OK, ENMessageBoxIcon.Error); } } private void OnPrintDialogClosed(NPrintDialogResult result) { if (result.Result == ENCommonDialogResult.Error) { NMessageBox.Show("Error Message: " + result.ErrorException.Message, "Print Dialog Error", ENMessageBoxButtons.OK, ENMessageBoxIcon.Error); } } #endregion #region Fields NComboBox m_PrintRangeModeComboBox; NCheckBox m_EnableCustomPageRangeCheckBox; NCheckBox m_EnableCurrentPageCheckBox; NCheckBox m_EnableSelectionCheckBox; NCheckBox m_CollateCheckBox; NNumericUpDown m_NumberOfCopiesUpDown; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Print Dialog Example for WPF Solution

VB.NET Print Dialog Example Code for Nevron UserInterface for WPF
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Wpf
Imports System

Class MainWindow

#Region "Constructors"

	Public Sub New()
		' This call is required by the designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.

		' Set form title
		Title = "NOV WPF - NPrintDialogExample"

		' Create the NOV example and its controls
		Dim exampleContent As NWidget = CreateExampleContent()
		Dim exampleControls As NWidget = CreateExampleControls()
		If Not exampleControls Is Nothing Then
			exampleControls.PreferredWidth = 300
		End If

		' Place the example and its controls in a pair box
		Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
		pairBox.FillMode = ENStackFillMode.First
		pairBox.FitMode = ENStackFitMode.First
		pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

		' Host the example in the form
		Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
		Content = host
	End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            Dim printButton As NButton = New NButton("Print...")
            printButton.HorizontalPlacement = ENHorizontalPlacement.Left
            printButton.VerticalPlacement = ENVerticalPlacement.Top
            printButton.Click += AddressOf OnPrintButtonClick

            Return printButton
        End Function
        Public Function CreateExampleControls() As NWidget
            Dim stack As NStackPanel = New NStackPanel()

            ' print range mode
            m_PrintRangeModeComboBox = New NComboBox()
            m_PrintRangeModeComboBox.Items.FillFromEnum(Of ENPrintRangeMode)()
            m_PrintRangeModeComboBox.SelectedIndex = 0
            stack.Add(New NPairBox("Print Range Mode:", m_PrintRangeModeComboBox, True))

            ' enable current page
            m_EnableCurrentPageCheckBox = New NCheckBox()
            stack.Add(New NPairBox("Enable Current Page:", m_EnableCurrentPageCheckBox, True))

            ' enable selection
            m_EnableSelectionCheckBox = New NCheckBox()
            stack.Add(New NPairBox("Enable Selection:", m_EnableSelectionCheckBox, True))

            ' enable custom page range
            m_EnableCustomPageRangeCheckBox = New NCheckBox()
            stack.Add(New NPairBox("Enable Custom Page Range:", m_EnableCustomPageRangeCheckBox, True))

            ' collate
            m_CollateCheckBox = New NCheckBox()
            stack.Add(New NPairBox("Collate:", m_CollateCheckBox, True))

            ' number of copies
            m_NumberOfCopiesUpDown = New NNumericUpDown()
            m_NumberOfCopiesUpDown.DecimalPlaces = 0
            m_NumberOfCopiesUpDown.Step = 1
            m_NumberOfCopiesUpDown.Minimum = 1
            m_NumberOfCopiesUpDown.Maximum = 100
            stack.Add(New NPairBox("Number of Copies:", m_NumberOfCopiesUpDown, True))

            Return New NUniSizeBoxGroup(stack)
        End Function
        Public Function GetExampleDescription() As String
            Return "
				

This example demonstrates how to create and use the print dialog provided by NOV.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Common Dialogs", "https://www.nevron.com/docs/openvision/ui-common-dialogs.html"), ("Top Level Windows", "https://www.nevron.com/docs/openvision/ui-top-level-windows.html")} End Function #End Region #Region "Implementation" #End Region #Region "Event Handlers" Private Sub OnPrintButtonClick(args As NEventArgs) Dim printDocument As NPrintDocument = New NPrintDocument() printDocument.DocumentName = "Test Document 1" printDocument.BeginPrint += AddressOf OnBeginPrint printDocument.PrintPage += AddressOf OnPrintPage printDocument.EndPrint += AddressOf OnEndPrint Dim printDialog As NPrintDialog = New NPrintDialog() printDialog.PrintRangeMode = CType(m_PrintRangeModeComboBox.SelectedItem.Tag, ENPrintRangeMode) printDialog.EnableCustomPageRange = m_EnableCustomPageRangeCheckBox.Checked printDialog.EnableCurrentPage = m_EnableCurrentPageCheckBox.Checked printDialog.EnableSelection = m_EnableSelectionCheckBox.Checked printDialog.CustomPageRange = New NRangeI(1, 100) printDialog.NumberOfCopies = CInt(m_NumberOfCopiesUpDown.Value) printDialog.Collate = m_CollateCheckBox.Checked printDialog.PrintDocument = printDocument printDialog.Closed += AddressOf OnPrintDialogClosed printDialog.RequestShow() End Sub Private Sub OnBeginPrint(sender As NPrintDocument, e As NBeginPrintEventArgs) End Sub Private Sub OnEndPrint(sender As NPrintDocument, e As NEndPrintEventArgs) End Sub Private Sub OnPrintPage(sender As NPrintDocument, e As NPrintPageEventArgs) Dim pageSizeDIP As NSize = New NSize(Me.Width, Me.Height) Try Dim pageMargins = NMargins.Zero Dim clip As NRegion = NRegion.FromRectangle(New NRectangle(0, 0, e.PrintableArea.Width, e.PrintableArea.Height)) Dim transform As NMatrix = New NMatrix(e.PrintableArea.X, e.PrintableArea.Y) Dim visitor As NPaintVisitor = New NPaintVisitor(e.Graphics, 300, transform, clip) ' forward traverse the display tree m_PrintRangeModeComboBox.DisplayWindow.VisitDisplaySubtree(visitor) e.HasMorePages = False Catch x As Exception NMessageBox.Show(x.Message, "Exception", ENMessageBoxButtons.OK, ENMessageBoxIcon.Error) End Try End Sub Private Sub OnPrintDialogClosed(result As NPrintDialogResult) If result.Result Is ENCommonDialogResult.Error Then Call NMessageBox.Show("Error Message: " & result.ErrorException.Message.ToString(), "Print Dialog Error", ENMessageBoxButtons.OK, ENMessageBoxIcon.Error) End If End Sub #End Region #Region "Fields" Private m_PrintRangeModeComboBox As NComboBox Private m_EnableCustomPageRangeCheckBox As NCheckBox Private m_EnableCurrentPageCheckBox As NCheckBox Private m_EnableSelectionCheckBox As NCheckBox Private m_CollateCheckBox As NCheckBox Private m_NumberOfCopiesUpDown As NNumericUpDown #End Region #Region "Static Methods" #End Region #Region "Constants" #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) Print Dialog Example for WPF Solution

Related Documentation Links