locked
How to open .Pdf file in Vb.Net Win form

    Question

  • Hi Martin,

     

    I'm looking to open .PDF files in VB.net Winform. Is there a way i can do that. The files will be from aNetwork Directory.

     

    Thanks

    sujo

    Thursday, July 21, 2011 10:30 PM

Answers

  • Hi Martin,

     

    I'm looking to open .PDF files in VB.net Winform. Is there a way i can do that. The files will be from aNetwork Directory.

     

    Thanks

    sujo

    Drag and drop Adobe's PDF ActiveX Control onto your form after adding it to your toolbox:

    http://www.kodefuguru.com/image.axd?picture=2009%2F6%2FChooseToolboxItems.png 

    Then set the path of pdf using LoadFile property.

    HTH.


    Best regards, Saygılarımla, Onur Güzel

    Yazgeliştir Forumları VB.NET / C# Süper Moderatorü.

    Microsoft Haber Grupları Profilim (VB.NET)

    • Marked as answer by Liliane Teng Thursday, July 28, 2011 7:14 AM
    Friday, July 22, 2011 2:32 PM
  • Hi Sujo John,

     Take alook at this link for an answer to your question.

    http://social.msdn.microsoft.com/Forums/en-US/winformsapplications/thread/4a6469a4-1df5-4b47-a042-b50415c1872a/

    Curtis


    Always Lost in Code. Always mark answers as correct if they answer your question and solve your problem. This way others when searching for similar problems can find the answer faster.
    • Marked as answer by Liliane Teng Thursday, July 28, 2011 7:14 AM
    Thursday, July 21, 2011 11:19 PM
  • The following demos how to load a PDF file into a WebBrowser control. To try the code out place a WebBrowser control on a WinForm and name it wbDocument then add a button as Button1. I set the code up so that the webbrowser only loads a document if it is not currently loaded which you may easily change to allow the document to be re-loaded.

    Note the code in the Form Closing event is optional, it may be good idea to try with and without this code.

    Private mFileName As String = ""
    Public Property FileName() As String
      Get
        Return mFileName
      End Get
      Set(ByVal value As String)
        mFileName = value
      End Set
    End Property
    Private Sub Button1_Click() Handles Button1.Click
      Dim OpenFileDialog As New OpenFileDialog
      OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
      OpenFileDialog.Filter = "PDF Files (*.pdf)|*.pdf|All Files (*.*)|*.*"
      If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
        If OpenFileDialog.FileName <> FileName Then
          FileName = OpenFileDialog.FileName
          wbDocument.Navigate(FileName)
        End If
      End If
    End Sub
    Private Sub DemoViewer_FormClosing() Handles Me.FormClosing
      If FileName.Length > 0 Then
        wbDocument.Hide()
        wbDocument.Navigate("about:blank")
        Do Until wbDocument.ReadyState = WebBrowserReadyState.Complete
          Application.DoEvents()
          System.Threading.Thread.Sleep(100)
        Loop
        wbDocument.Dispose()
        System.Threading.Thread.Sleep(100)
      End If
    End Sub

     

    KSG
    • Marked as answer by Liliane Teng Thursday, July 28, 2011 7:14 AM
    Sunday, July 24, 2011 9:50 AM

All replies

  • Hi Sujo John,

     Take alook at this link for an answer to your question.

    http://social.msdn.microsoft.com/Forums/en-US/winformsapplications/thread/4a6469a4-1df5-4b47-a042-b50415c1872a/

    Curtis


    Always Lost in Code. Always mark answers as correct if they answer your question and solve your problem. This way others when searching for similar problems can find the answer faster.
    • Marked as answer by Liliane Teng Thursday, July 28, 2011 7:14 AM
    Thursday, July 21, 2011 11:19 PM
  • Hi Martin,

     

    I'm looking to open .PDF files in VB.net Winform. Is there a way i can do that. The files will be from aNetwork Directory.

     

    Thanks

    sujo

    Drag and drop Adobe's PDF ActiveX Control onto your form after adding it to your toolbox:

    http://www.kodefuguru.com/image.axd?picture=2009%2F6%2FChooseToolboxItems.png 

    Then set the path of pdf using LoadFile property.

    HTH.


    Best regards, Saygılarımla, Onur Güzel

    Yazgeliştir Forumları VB.NET / C# Süper Moderatorü.

    Microsoft Haber Grupları Profilim (VB.NET)

    • Marked as answer by Liliane Teng Thursday, July 28, 2011 7:14 AM
    Friday, July 22, 2011 2:32 PM
  • The following demos how to load a PDF file into a WebBrowser control. To try the code out place a WebBrowser control on a WinForm and name it wbDocument then add a button as Button1. I set the code up so that the webbrowser only loads a document if it is not currently loaded which you may easily change to allow the document to be re-loaded.

    Note the code in the Form Closing event is optional, it may be good idea to try with and without this code.

    Private mFileName As String = ""
    Public Property FileName() As String
      Get
        Return mFileName
      End Get
      Set(ByVal value As String)
        mFileName = value
      End Set
    End Property
    Private Sub Button1_Click() Handles Button1.Click
      Dim OpenFileDialog As New OpenFileDialog
      OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
      OpenFileDialog.Filter = "PDF Files (*.pdf)|*.pdf|All Files (*.*)|*.*"
      If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
        If OpenFileDialog.FileName <> FileName Then
          FileName = OpenFileDialog.FileName
          wbDocument.Navigate(FileName)
        End If
      End If
    End Sub
    Private Sub DemoViewer_FormClosing() Handles Me.FormClosing
      If FileName.Length > 0 Then
        wbDocument.Hide()
        wbDocument.Navigate("about:blank")
        Do Until wbDocument.ReadyState = WebBrowserReadyState.Complete
          Application.DoEvents()
          System.Threading.Thread.Sleep(100)
        Loop
        wbDocument.Dispose()
        System.Threading.Thread.Sleep(100)
      End If
    End Sub

     

    KSG
    • Marked as answer by Liliane Teng Thursday, July 28, 2011 7:14 AM
    Sunday, July 24, 2011 9:50 AM
  • Hello Sujo,

    Above replies have given you helpful suggestions. Would you mind letting us know the result on your side now? If you need further assistance, please feel free to follow up.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 26, 2011 8:57 AM