How To Use Spire PDFViewer

Introduction

Sometimes when you work on developing a huge application, you may need a PDFViewer control to display an ebook or simply to extract the content of a PDF file.

The first choice that comes to mind as a .NET developer is to use the famous Adobe Reader Active X control. This solution is good and works well but requires Adobe Reader, which is installed on a target PC in order to work. This may be a problem especially when you don‘t want your app users to install a 3rd party application so that your app can work.

There are so many other solutions, where you can search the internet for PDF viewers and you‘ll find plenty, but most of them are hard to use (with coding of course), as it requires so many changes on the end-user's machine.

Background

What we need is an easy-to-use PDFViewer, which gives us full options, as shown below.

  • Able to load PDF files easily, like calling a method with file path as parameter, or by using Streams.
  • Supports all PDF formats, or at least, supports the most commonly-used ones.
  • Able to open encrypted PDF files.
  • Able to load all the content of the PDF file (read all texts, images ..etc without glitches).

I searched the internet and found a simple library that can do all the magic and it is called Free Spire.PDFViewer for .NET. You can get it from here. This library is free to download and use, the free version is limited to 10 pages of PDF, when viewing. To exceed the limitation, you‘ll need to purchase the Commercial Edition of Spire.PDFViewer for .NET.

All the stuff,which we need are listed as features of this library-- it supports most PDF formats, it's easy to use, and can open encrypted  files. Read more about the features here.

To download, you need to register at e-iceclup. After registering (so easy), a link will be sent to your email, once the account is confirmed you can download the library. The library comes in .msi file format, installation is very easy and it will ask you to install  additional things for VS, which is optional and cool.

After installation completes, you can use the library directly and of course, browse the amazing demos and tutorials that are included (in the setup folder) with fully explained and simple code lines in C# and VS.

Using the code

Demos and tutorials are included in the setup folder (after installation, usually at C:\Program Files (x86)\e-iceblue\Spire.PdfViewer-FE\Demos\, or simply visit the website), with fully explained and simple code lines in C# and VS.



This one is the hello world (C:\Program Files (x86)\e-iceblue\Spire.PdfViewer-FE\Demos\CS\WinForms\02_PdfViewer\HelloWorld), simple and easy.

  1. using System;  
  2. using System.IO;  
  3. using System.Windows.Forms;  
  4. using Spire.PdfViewer.Forms;  
  5.   
  6.   
  7. namespace PdfViewer_HelloWorld  
  8. {  
  9.     public partial class Form1 : Form  
  10.     {  
  11.         public Form1()  
  12.         {  
  13.             InitializeComponent();  
  14.         }  
  15.   
  16.         private void Form1_Load(object sender, EventArgs e)  
  17.         {  
  18.             string pdfDoc = @"..\..\..\..\..\..\Data\Le_Petit_Prince_French.pdf";  
  19.             if (File.Exists(pdfDoc))  
  20.             {  
  21.                 this.pdfViewer1.LoadFromFile(pdfDoc);  
  22.             }  
  23.   
  24.         }  
  25.   
  26.         
  27.     }  
  28. }  
After examining the code, we just need to do a few things in order to use the library.
  1. Adding a reference to the libraries we need to use, the libraries are: Spire.License.dll, Spire.Pdf.dll and Spire.PdfViewer.Forms.dll. These can be found at „C:\Program Files (x86)\e-iceblue\Spire.PdfViewer-FE\Bin“, choose between NET2.0, NET3.5, NET4.0 or NET4.0 ClientProfile, which depends on your Application needs.



  2. In the forms designer, we just add the Spire.PdfViewer.Forms.PdfViewer control, which is simple and easy.

  3. We add a simple code, as shown above, to open the file which we need and when running the app, the control will do all the magic.

Well, to export a PDF file as an image, this may be a little bit harder, but it's simple enough, check out the code given below.

  1. private void ExportImage()  
  2. {  
  3.     // Check the control to see how many pages we have, we cannot export if no page is loaded  
  4.     if(this.pdfDocumentViewer1.PageCount > 0)  
  5.     {  
  6.   
  7.         // Open the save dialog  
  8.         SaveFileDialog dialog=new SaveFileDialog ();  
  9.         dialog.Filter="JPG Format(*.jpg)|*.jpg|BMP Format(*.bmp)|*.bmp|PNG Format(*.png)|*.png|GIF Format(*.gif)|*.gif";  
  10.         // Show the dialog  
  11.         if (dialog.ShowDialog()==DialogResult.OK)  
  12.         {  
  13.             string fileName=dialog.FileName;  
  14.             int currentPage=this.pdfDocumentViewer1.CurrentPageNumber;  
  15.             // We just call this method with the page index (0 based)  
  16.             Image image=this.pdfDocumentViewer1.SaveAsImage(currentPage-1);  
  17.             // Save it !!  
  18.             image.Save(fileName);  
  19.   
  20.         }  
  21.     }  
  22.   
  23. }  
Points of Interest

There are a lot of solutions to include a PDF Viewer in your project, but choosing the best one for your application is a hard task. I chose this library because I found it the best for my applications, such as Emulators Organizer.
Ebook Download
View all
Learn
View all