PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2005/2008/2010/2012/2013

How To Create a .NET PDF Viewer In 5 Minutes

Easy steps towards creating a PDF viewer application in Visual Studio 2005.
By Raju Sinha
IMPORTANT NOTE

With the release of Version 2.0, the code and the steps for creating a PDF viewer has changed. Please read the article How To Create A PDF Viewer in PDFOne .NET v2.x for updated information.

With absolutely no ado, here are the steps.

  1. Start Visual Studio 2005 and create a Visual C# Windows Application.
  2. Add a reference to our new PDFOne .NET v2.0 Preview component (Gnostice.PDFOne.dll)
  3. Resize Form1 to your liking.
  4. Add a Toolstrip component (named as toolStrip1 in the code snippets below) from the toolbox and add the following controls in the listed order on the strip:
    • TextBox - 1 No. (tb_Filename)
    • Button - 1 No.
    • Label - 4 Nos.
  5. Resize the text box so that it looks like the address bar of a browser.
  6. Set an icon image for the button.
  7. Change the Text property of the labels to First Page, Previous Page, Next Page, and Last Page.
  8. Double click Form1 to go to Code View. Extend the Form1 class by
    • adding private members of type PDFDocument and PDFViewer, and
    • modifying the default constructor to instantiate the private members.
    public partial class Form1 : Form
    {
        // New private members
        PDFDocument doc1;
        PDFViewer viewer1;
    
        public Form1()
        {
            InitializeComponent();
            // Initialize the private members
            doc1 = new PDFDocument();
            viewer1 = new PDFViewer();            
        }
    
    
  9. Add the following code for the button click event
    if (tb_Filename.Text == "") 
    {
        return;
    }
    
    // Close previous document if any
    if (doc1.GetPageCount() > 0)
    {
        doc1.Close();
        doc1.Dispose(); 
    }
    
    // Load PDF document
    try
    {
        doc1.Load(tb_Filename.Text);
    }
    catch(Exception err1)
    {   
       MessageBox.Show(
        "Sorry. There has been an error. Error Information: " 
        + err1.Message);
       return;    
    }    
    
    // Add viewer to Form1
    this.Controls.Add(viewer1);
    
    // Position the viewer just below the toolstrip
    viewer1.Location = new System.Drawing.Point(
        this.toolStrip1.Location.X, 
        this.toolStrip1.Location.Y 
        + this.toolStrip1.Height + 1);
    
    // Display document in viewer
    viewer1.Document = doc1;
    
    // Adjust viewer dimensions
    viewer1.Height = this.Height - 30;
    viewer1.Width = this.Width - 10;
    
    // Update form title bar
    this.Text = "PDF Viewer - " + tb_Filename.Text;
    
  10. Add the following code for the click event for the Next Page label.
    viewer1.NextPage();
    
    Add similar code similar for click events of the labels First Page, Previous Page, and Last page.

You can further improve the code by adding support for

  1. zoom (page magnification)
  2. page number display
  3. viewer resizing - in sync with window resizing
---o0O0o---

Downloads:

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved.