How to Open a PDF File in VB

Save

Do you need to open a PDF file from a program you're writing in Visual Basic? You can open almost any type of file from within a VB program as long as you know the correct code sequence. One way of opening a file is by coding the open process to a button that the user can click. There are many different ways to open a PDF file, but the basic code will work in many of those cases.

Things You'll Need

  • Windows computer with a Visual Basic compiler

From Default Viewer

  • Create a Private Sub button event to handle the PDF file by using the following code example:

    "Private Sub ButtonOpen(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOpen.Click

    End Sub"

  • Use the Process.Start method to open the PDF with the default PDF viewer. For example, use the following code to open "book.pdf" located in the Documents folder:

    "Process.Start("C:\Documents\book.pdf")", but use a valid location for your PDF file. Place this code before the "End Sub" section.

  • Compile the code and test the results. The button click event opens the PDF file you specified.

From Default Browser

  • Create a Private Sub button event to handle the PDF file by using the following code example:

    "Private Sub ButtonOpen(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOpen.Click

    End Sub"

  • Use the WebBrowser.Navigate method to open the PDF with the default Web browser. For example, type the following code before the "End Sub" section:

    "WebBrowser.Navigate("C:\Documents\book.pdf")", but use a valid location for your PDF file.

  • Compile the code and test the results. The button click event opens the PDF file you specified.

With a Specific Program

  • Create a Private Sub button event to handle Adobe Reader by using the following code example:

    "Private Sub ButtonOpen(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOpen.Click

    End Sub"

  • Add the "Adobe PDF Reader" control from your toolbox to the form you are using. Find the AdobeReader.src property and change it to "AdobeReader.src = "C:\Documents\book.pdf"" but use a valid location for your PDF file.

  • Compile the code and test the results. The button click event opens the PDF file you specified within the application window.

Tips & Warnings

  • Before starting this workflow, it is important to have a basic understanding of VB.
  • You'll need access to a VB compiler to make this code compile and run.

References

  • Photo Credit Jupiterimages/Photos.com/Getty Images
Promoted By Zergnet

Comments

You May Also Like

  • How to Download the MSDN Library

    Microsoft's MSDN Library contains programming documentation and information. It includes technical references, problem-solving reports ("white pages"), application and software kits, snippets of...

  • How to Open Internet Explorer From Visual Basic

    You can easily open Internet Explorer from your Visual Basic application by using the "Process.Start" method. This powerful method gives you the...

  • How to Open a File in Visual Basic

    One of the first things you learn in programming is how to write to a file and how to open the same...

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!