Print PDF Document Pages via PDFViewer in C#, VB.NET

As its read only characteristic, PDF enjoys great popularity among business field. Especially, when sign contracts or send invoices, almost all the business contacts and invoices must be printed. Thus, printing PDF document pages becomes an unavoidable process, which requires a well knowledge of print PDF document in a quick way.

This program guide aims at introducing a method to print PDF document via PDF Viewer component Spire.PDFViewer with C#, VB.NET.

This method enables you not only to print PDF document pages but also to open any PDF document on system via Spire.PDFViewer. That is to say, one method can open and print many PDF files. Please look at the procedure below.

Step 1: Create a new project

  • Create a new project in Visual Studio. Please note that this project needs a Form
  • Set the Target framework of this project to be .NET Framework 2 or above in Properties

Step 2: Add reference and Set up the Form

  • Add Spire.PDFViewer Form Dll as reference from your downloaded Spire.PDFViewer.
  • Add a toolScript and pdfDocumentViewer in the default Form "Form1".
  • Add two buttons and a ComboBox in Form1 from toolScript dropdown list.
  • Set the "Name", "Display Style", "Text" and "ToolTipText" of button1 in Properties to be "btnOpen", "Text", "Open" and "Open PDF document" and button2 to be "btnPrint", "Text", "Print" and "Print PDF document".
  • Set the Dock property of pdfDocumentViewer in order to view PDF file page in enough space.

Step 3: Print PDF Document Pages via PDF Viewer

Add below namespaces at the top of the method.

[C#]
using System.IO;
using Spire.PdfViewer.Forms;
[VB.NET]
Imports System.IO
Imports Spire.PdfViewer.Forms

Load a PDF document from system

[C#]
        private void Form1_Load(object sender, EventArgs e)
        {
            string pdfDoc = @"D:\e-iceblue\Spire.PDFViewer\Demos\Data\Spire.Office.pdf";
            if (File.Exists(pdfDoc))
            {
                this.pdfDocumentViewer1.LoadFromFile(pdfDoc);
            }

        }
[VB.NET]
	Private Sub Form1_Load(sender As Object, e As EventArgs)
	   Dim pdfDoc As String = "D:\e-iceblue\Spire.PDFViewer\Demos\Data\Spire.Office.pdf"
	        If File.Exists(pdfDoc) Then
	            Me.pdfDocumentViewer1.LoadFromFile(pdfDoc)
	        End If

	End Sub

Open the PDF document

[C#]
        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "PDF document (*.pdf)|*.pdf";
            DialogResult result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {

                string pdfFile = dialog.FileName;
                this.pdfDocumentViewer1.LoadFromFile(pdfFile);
            }
        }
[VB.NET]
	Private Sub btnOpen_Click(sender As Object, e As EventArgs)
		Dim dialog As New OpenFileDialog()
		dialog.Filter = "PDF document (*.pdf)|*.pdf"
		Dim result As DialogResult = dialog.ShowDialog()
	         If result = DialogResult.OK Then

		Dim pdfFile As String = dialog.FileName
		Me.pdfDocumentViewer1.LoadFromFile(pdfFile)
		End If
	End Sub

Print PDF document pages via PDF Viewer

[C#]
        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (this.pdfDocumentViewer1.PageCount > 0)
            {
                this.pdfDocumentViewer1.Print();
            }
        }


        private void pdfDocumentViewer1_PdfLoaded(object sender, EventArgs args)
        {
            this.comBoxPages.Items.Clear();
            int totalPage = this.pdfDocumentViewer1.PageCount;

            for (int i = 1; i <= totalPage; i++)
            {
                this.comBoxPages.Items.Add(i.ToString());
            }

            this.comBoxPages.SelectedIndex = 0;
        }

        private void pdfDocumentViewer1_PageNumberChanged(object sender, EventArgs args)
        {
            if (this.comBoxPages.Items.Count <= 0)
                return;
            if (this.pdfDocumentViewer1.CurrentPageNumber != this.comBoxPages.SelectedIndex + 1)
            {
                this.comBoxPages.SelectedIndex = this.pdfDocumentViewer1.CurrentPageNumber - 1;
            }
        }

        private void comBoxPages_SelectedIndexChanged(object sender, EventArgs e)
        {
            int soucePage = this.pdfDocumentViewer1.CurrentPageNumber;
            int targetPage = this.comBoxPages.SelectedIndex + 1;
            if (soucePage != targetPage)
            {
                this.pdfDocumentViewer1.GoToPage(targetPage);
            }
        }
[VB.NET]
	Private Sub btnPrint_Click(sender As Object, e As EventArgs)
		If Me.pdfDocumentViewer1.PageCount > 0 Then
			Me.pdfDocumentViewer1.Print()
		End If
	End Sub


	Private Sub pdfDocumentViewer1_PdfLoaded(sender As Object, args As EventArgs)
	         Me.comBoxPages.Items.Clear()
		Dim totalPage As Integer = Me.pdfDocumentViewer1.PageCount

		For i As Integer = 1 To totalPage
			Me.comBoxPages.Items.Add(i.ToString())
		Next

		Me.comBoxPages.SelectedIndex = 0
		End Sub

	Private Sub pdfDocumentViewer1_PageNumberChanged(sender As Object, args As EventArgs)
		If Me.comBoxPages.Items.Count <= 0 Then
			Return
		End If
		If Me.pdfDocumentViewer1.CurrentPageNumber <> Me.comBoxPages.SelectedIndex + 1 Then
			Me.comBoxPages.SelectedIndex = Me.pdfDocumentViewer1.CurrentPageNumber - 1
		End If
	End Sub

	Private Sub comBoxPages_SelectedIndexChanged(sender As Object, e As EventArgs)
		Dim soucePage As Integer = Me.pdfDocumentViewer1.CurrentPageNumber
		Dim targetPage As Integer = Me.comBoxPages.SelectedIndex + 1
		If soucePage <> targetPage Then
			Me.pdfDocumentViewer1.GoToPage(targetPage)
		End If
	End Sub

Step 4: Press F5 or debug the project

After debugging, you can preview the effect.

The First Page

The Fourth Page

When you click the "Open" button, there will be a dialog box appears and then you can open another PDF document from system. Also, you can select any page in the dropdown list of the ComboBox and click "Print" button to print it.