PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2008 VS 2005 CLR 2.0

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 Developer Tools
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.

XtremePDFConverter VCL

A Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents.

PDFOne .NET

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

XtremeDocumentStudio .NET

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

PDFOne (for Java™)

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

XtremeFontEngine (for Java)

Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts

Our Office Productivity Applications
Free PDF Reader

A free, fast, and portable application for viewing, printing and converting PDF documents.

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

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).