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

How To Create A PDF Viewer in PDFOne .NET v2.x

Create a PDF viewer application in 5 minutes flat.
By V. Subhash

This is an updated version of an older article. That article was written for PDFOne .NET v2.0 Preview, where the viewer was part of Gnostice.PDFOne assembly.

When version 2.0 was finally released, the PDF viewer component was provided in a separate assembly named Gnostice.PDFOne.Windows.PDFViewer. Hence, a newer version of the article was due. PDFViewer is a .NET PDF viewer component that you can use to display PDF documents to end-users in Win-forms applications.

All right and here we go. Let us build a Winforms PDF viewer application.

  1. Start Visual Studio. (I have used VS 2005 for this article.)
  2. Create a new Windows application in C# or Visual Basic.
  3. Add a toolstrip component from the Toolbox to the form. (In C#, this will named as toolStrip1. In VB.NET, it will be ToolStrip1.) Ensure that the strip is right below the title bar and its Dock property is set to Top.
  4. Add a PDFViewer component to the form. (It is a good idea to create a tab in the IDE's Toolbox for PDFOne .NET components. The help file has a "Getting Started" topic that shows how to do this.) In C#, the instance will be named pdfViewer1. In VB.NET, it will be named PdfViewer1.
  5. Ensure that the viewer is below the tool strip and viewer's Dock property is set to Fill.
  6. Add the following components using the toolstrip's design-time split button menu options.
    1. label - 1 no.
    2. text box - 1 no.
    3. buttons - 5 nos.
  7. Set the text property of the label to "File:".
  8. Set the name property of the text box to tb_Filename. Resize it so that it looks like the address bar of a browser.
  9. Rename the buttons to btn_Open, btn_FirstPage, btn_PreviousPage, btn_NextPage, and btn_LastPage. Also, set their DisplayStyle to Text and Text properties to "First Page", "Previous Page", Next Page", and "Last Page".
  10. Double-click on the btn_Open and add the following code for its click event.
    In C#
    tb_Filename.Text = tb_Filename.Text.Trim();
    if (tb_Filename.Text == "")
    {
        return;
    }
    
    // Close any previously loaded document
    if (pdfViewer1.PDFLoaded)
    {
        pdfViewer1.CloseDocument();
    }
    
    // Load PDF document
    try
    {
        pdfViewer1.LoadDocument(tb_Filename.Text);
    }
    catch (Exception ex1)
    {
        MessageBox.Show(
         "Sorry. There has been an error. Error Information: "
         + ex1.Message);
        return;
    }
    
    // Update form title bar
    this.Text = "PDFOne .NET Viewer - " + tb_Filename.Text;
    
    In VB
    tb_Filename.Text = Trim(tb_Filename.Text)
    If Trim(tb_Filename.Text) = "" Then
        Exit Sub
    End If
    
    ' Close any previously loaded document
    If PdfViewer1.PDFLoaded Then
        PdfViewer1.CloseDocument()
    End If
    
    ' Load PDF document
    Try
        PdfViewer1.LoadDocument(tb_Filename.Text)
    Catch Ex1 As Exception
        MessageBox.Show( _
                "Sorry. There has been an error. Error Information: " _
                & Ex1.Message)
        Exit Sub
    End Try
    
    ' Update form title bar
    Me.Text = "PDFOne .NET Viewer Component Demo - " & tb_Filename.Text
    
  11. For click-events of the other buttons, add the following code.
    In C#
    private void btn_PreviousPage_Click(object sender, EventArgs e)
    {
        pdfViewer1.PreviousPage();
    }
    
    private void btn_FirstPage_Click(object sender, EventArgs e)
    {
        pdfViewer1.FirstPage();
    }
    
    private void btn_NextPage_Click(object sender, EventArgs e)
    {
        pdfViewer1.NextPage();
    }
    
    private void btn_LastPage_Click(object sender, EventArgs e)
    {
        pdfViewer1.LastPage();
    }
    
    In VB
    Private Sub btn_FirstPage_Click(ByVal sender As System.Object, _
                                    ByVal e As System.EventArgs) _
                Handles btn_FirstPage.Click
        PdfViewer1.FirstPage()
    End Sub
    
    Private Sub btn_PreviousPage_Click(ByVal sender As System.Object, _
                                       ByVal e As System.EventArgs) _
                Handles btn_PreviousPage.Click
        PdfViewer1.PreviousPage()
    End Sub
    
    Private Sub btn_NextPage_Click(ByVal sender As System.Object, _
                                   ByVal e As System.EventArgs) _
                Handles btn_NextPage.Click
        PdfViewer1.NextPage()
    End Sub
    
    Private Sub btn_LastPage_Click(ByVal sender As System.Object, _
                                   ByVal e As System.EventArgs) _
                Handles btn_LastPage.Click
        PdfViewer1.LastPage()
    End Sub
    
  12. Compile and run the application. Enter the path of a PDF document in the text box and click on Open button.

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