Tuesday, 12 June 2012 08:48

Open PDF Document via PDFViewer in C#, VB.NET

Written by  support iceblue
Rate this item
(2 votes)

In people's daily life, we can open a PDF document by right clicking the open option as well as using C#, VB.NET or other programming languages. Both methods are available as long as you have a PDF Document, but for PDF itself, it has no viewing function, thus, we need to use PDF Viewer to help us view it. This article is designed to open a PDF Document with C#, VB.NET via PDF Viewer by two methods.

Spire. PDFViewer is designed for viewing PDF files from .NET application. It does NOT require Adobe Read or any other 3rd party software/library installed on system. By using Spire.PDFViewer, we can do this job easily. Please just follow the below procedure.

Step 1: Create a new project

  • Freely Download Spire.PDFViewer
  • Create a new project in Visual Studio and add a toolScript in Form1
  • Set its target Framework to be .NET Framework 4
  • Add Spire.PdfViewer. Forms as reference in Project. And add using at the top of the method. Please see below:
[C#]
using System.IO; 
using Spire.PdfViewer.Forms;
[VB.NET]
Imports Sytem. IO
Imports Spire.PdfViewer.Forms

Step 2: Open a PDF Document with C#, VB.NET via Spire.PDFViewer

Method one: This method is to directly load a PDF file from system, then open it.

[C#]
        public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
         
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string pdfDoc = @"D:\michelle\e-iceblue\Spire.Office.pdf";
            if (File.Exists(pdfDoc))
            {
                this.pdfDocumentViewer1.LoadFromFile(pdfDoc);
            }

        }
    }
 }
[VB.NET]
Public Partial Class Form1
		Inherits Form

		Public Sub New()

			InitializeComponent()
		End Sub

		Private Sub Form1_Load(sender As Object, e As EventArgs)

		End Sub

		Private Sub toolStripButton1_Click(sender As Object, e As EventArgs)
			Dim pdfDoc As String = "D:\michelle\e-iceblue\Spire.Office.pdf"
			If File.Exists(pdfDoc) Then
				Me.pdfDocumentViewer1.LoadFromFile(pdfDoc)
			End If

		End Sub
	End Class
End Namespace

Method Two: This method allows you to choose the PDF file you want to open in a dialog box from your computer.

[C#]
public partial class Form1 : Form
 {
       
        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
         
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "PDF document (*.pdf)|*.pdf";
            DialogResult result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                try
                {
                    string pdfFile = dialog.FileName;
                    this.pdfDocumentViewer1.LoadFromFile(pdfFile);
                }
                catch (Exception exe)
                {
                    MessageBox.Show(exe.Message, "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }

        }
   }
[VB.NET]
Public Partial Class Form1
	Inherits Form

	Public Sub New()

		InitializeComponent()
	End Sub

	Private Sub Form1_Load(sender As Object, e As EventArgs)

	End Sub

	Private Sub toolStripButton1_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
			Try
				Dim pdfFile As String = dialog.FileName
				Me.pdfDocumentViewer1.LoadFromFile(pdfFile)
			Catch exe As Exception
				MessageBox.Show(exe.Message, "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.[Error])

			End Try
		End If

	End Sub
End Class

Step 3: Launch the file

Press F5, you can see Form1 display itself as picture below:

Then click "open" in the Form. When you use method one, you can see the PDF document content shows in the Form1. Also you can set the size of the form according to your own preference. When you use method two, you can choose the PDF Document by yourself in a dialog box. And then preview it in Form1.

Note: I set the default name of toolScript to be "open".

Effective Screenshot:

Additional Info

  • tutorial_title: Open PDF Document via PDFViewer
Last modified on Thursday, 17 July 2014 01:37