none
How to click on linklabel in my program that opens a pdf file RRS feed

  • Question

  • i'm trying to build a program to make work a little easier. i'm not completely new to VB, however I cant seem to find anywhere how to do this. I have a linklabel in my program and what i'm looking to do is when the user clicks on it that it displays a pdf file. how do I add the file from my computer to my program so they do not have to download it from the web and what would the code be to open said file when the link is clicked? Any help would be greatly appreciated.

    thanks


    • Edited by jhedborn Monday, August 27, 2018 5:51 AM
    Monday, August 27, 2018 5:45 AM

Answers

  •  If you added the file to your resources correctly,  it should be  'My.Resources.FlashSheet.pdf'.

     Then in the 'Process.Start' line,  you want to use the same filename that you saved the resource as on the hard drive.

     It would be a good idea to make sure the Directory exists and create it if it does not.  Same with the pdf file,  only write it if it does not exist.  For example...

        Private Sub LinkLabel2_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
    
            If Not Directory.Exists("C:\FPPFiles") Then Directory.CreateDirectory("C:\FPPFiles") 'if your (FPPFiles) directory does not exist then create it
    
            Dim SaveAsFilename As String = "C:\FPPFiles\Flash.pdf" 'this is the full path and filename that the pdf file will be saved as
    
            If Not File.Exists(SaveAsFilename) Then File.WriteAllBytes(SaveAsFilename, My.Resources.FlashSheet) 'write the 'FlashSheet' byte array to the hard drive as 'Flash.pdf'
    
            Process.Start(SaveAsFilename) 'start the 'flash.pdf' file with the default program for pdf files
        End Sub
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by jhedborn Friday, August 31, 2018 12:49 AM
    Thursday, August 30, 2018 11:42 PM
  •  To add a file to your resources,  go to the menu and click (Project) and select (YourProjectName Properties).  When the properties window opens,  select the (Resources) tab.  Then you can use the (Add Resources) button at the top of the window to add the pdf file,  or as I usually do and find easier,  I open the folder that has the file in it, in windows explorer.  Then I right click and copy the file,  go back to the resources tab in VS and right click (Paste).


    If you say it can`t be done then i`ll try it

    • Marked as answer by jhedborn Friday, August 31, 2018 12:49 AM
    Friday, August 31, 2018 12:44 AM

All replies

  • Right-click the project in Solution Explorer, select Add, Existing item, then find and select the PDF file from your computer. It will appear in Solution Explorer. Select it, go to Properties (or press <F4>), then set the option “Copy to Output Directory=Copy if newer”.

    In your form, handle the LinkClicked event of the label. (Double-click it in Form Designer). Then enter this line, specifying the name of your file:

       Process.Start(Path.Combine(Application.StartupPath, "MyFile.pdf"))

     




    • Edited by Viorel_MVP Monday, August 27, 2018 7:31 AM
    • Proposed as answer by IronRazerz Monday, August 27, 2018 6:33 PM
    Monday, August 27, 2018 7:28 AM
  • thank you so much, however all is going good up until I type the code in. 

    Process.Start(Path.Combine(Application.StartupPath, "FlashSheet.pdf"))

    "path" is saying it is not declared, do I put the application path in there?

    thanks


    or perhaps i'm not being clear how i'm explaining it. where path is would I put C:\program files\adobe...…. to reference the program to open the file. the end goal is that the file is part of the build so when I install this on a second or third machine the file is part of the program and the link label opens it in the default program for pdf's for that maching
    • Edited by jhedborn Wednesday, August 29, 2018 4:36 AM
    Wednesday, August 29, 2018 4:18 AM
  • Hi,

    1.You can first add the pdf file to the resources

    2.Use the code below,import files to your local computer

      Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
            Dim b() As Byte = My.Resources.Resource1._11
            Dim s As IO.Stream = File.Create("d:\1.pdf")
            s.Write(b, 0, b.Length)
            s.Close()
            Process.Start("d:\1.pdf")
        End Sub 

    3.Remember to delete the file

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, August 29, 2018 8:49 AM
  • .... "path" is saying it is not declared, do I put the application path in there?


    ...  the end goal is that the file is part of the build so when I install this on a second or third machine the file is part of the program and the link label opens it in the default program for pdf's for that maching

      The Path Class is part of the System.IO namespace.  So,  you need to do one of the following... either Import the System.IO namespace in your class like shown here...

    Import System.IO 'import the System.IO namespace
    
    Public Class Form1
    
      'Your class code....
    
    End Class
     

     Or you can add the Namespace to the 'Path' class name like shown here...

    Process.Start(System.IO.Path.Combine(Application.StartupPath, "MyFile.pdf"))


     

     If you add the pdf file to your application's Resources it is stored as a Byte array.  You can use the File.WriteAllBytes Method to write the byte array from the resources back to the users hard drive as shown below.  Notice that the File class is also in the System.IO namespace.  If you Imported the namespace as I mentioned above,  then you can remove the 'System.IO' from the below example.

    System.IO.File.WriteAllBytes("C:\SomeFolderPath\MyPdfFile.pdf", My.Resources.ThePdfFile)
     

     Now you can supply the full path and filename to the Process.Start method.  That will open the pdf with the default program for pdf files.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, August 29, 2018 11:10 AM
    Wednesday, August 29, 2018 11:08 AM
  • here is what I have, no errors in code "red squiggle line" however when I run the debug it gives me and error: 

    System.ArgumentNullException: 'Value cannot be null.
    Parameter name: bytes'

    here is my code:

     Private Sub LinkLabel2_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
            File.WriteAllBytes("C:\FPPFiles\flash.pdf", My.Resources.Resource1.FlashSheet)
            Process.Start(System.IO.Path.Combine(Application.StartupPath, "C:\FPPFiles\flash.pdf"))

        End Sub

    I import system.io in the beginning before the main form1.

    my pdf is listed in the resource folder in the solution explorer. 

    Thursday, August 30, 2018 11:30 PM
  •  If you added the file to your resources correctly,  it should be  'My.Resources.FlashSheet.pdf'.

     Then in the 'Process.Start' line,  you want to use the same filename that you saved the resource as on the hard drive.

     It would be a good idea to make sure the Directory exists and create it if it does not.  Same with the pdf file,  only write it if it does not exist.  For example...

        Private Sub LinkLabel2_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
    
            If Not Directory.Exists("C:\FPPFiles") Then Directory.CreateDirectory("C:\FPPFiles") 'if your (FPPFiles) directory does not exist then create it
    
            Dim SaveAsFilename As String = "C:\FPPFiles\Flash.pdf" 'this is the full path and filename that the pdf file will be saved as
    
            If Not File.Exists(SaveAsFilename) Then File.WriteAllBytes(SaveAsFilename, My.Resources.FlashSheet) 'write the 'FlashSheet' byte array to the hard drive as 'Flash.pdf'
    
            Process.Start(SaveAsFilename) 'start the 'flash.pdf' file with the default program for pdf files
        End Sub
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by jhedborn Friday, August 31, 2018 12:49 AM
    Thursday, August 30, 2018 11:42 PM
  • I put in above code still got an error saying flashsheet is not a member of family protection plan resources. so i'm assuming I did not add it correctly.

    what I did to add the pdf as a resource is clicked on my program in the solution explorer

    add->component->resources file 

    then added the pdf.

    after the code did not work. I delete both the resource folder and the Resource1.resx file in the solution explorer

    I tried then highlighting program again in the solution explorer and hit

    add->existing item-> flashsheet.pdf

    thank you so much and your patience with me

    Friday, August 31, 2018 12:04 AM
  •  To add a file to your resources,  go to the menu and click (Project) and select (YourProjectName Properties).  When the properties window opens,  select the (Resources) tab.  Then you can use the (Add Resources) button at the top of the window to add the pdf file,  or as I usually do and find easier,  I open the folder that has the file in it, in windows explorer.  Then I right click and copy the file,  go back to the resources tab in VS and right click (Paste).


    If you say it can`t be done then i`ll try it

    • Marked as answer by jhedborn Friday, August 31, 2018 12:49 AM
    Friday, August 31, 2018 12:44 AM
  • so simple, thanks so much for all your help!!!
    Friday, August 31, 2018 12:49 AM
  • so simple, thanks so much for all your help!!!

     You're welcome.  8)

    If you say it can`t be done then i`ll try it

    Friday, August 31, 2018 1:00 AM