locked
Loading a pdf file from Resources RRS feed

  • Question

  • I am using vb 2010.  I have a pdf file that I saved in Resources.  I want this file to be opened when the end user click  on "help" from a menuStrip option in a window form.  I appreciate your help.
    Thursday, December 13, 2012 5:49 AM

Answers

  • If you would like to view the PDF in a windows form within your application, take Frank's code excluding the process start and pass it to a function in the link below which is a VB.NET project for viewing PDF files in a win-form project via a webbrowser control.

    http://kevininstructor.home.comcast.net/~kevininstructor/DotNet/PDF_View.zip

    Here is a partial working example of what Frank provided less the removal of the file

    http://kevininstructor.home.comcast.net/~kevininstructor/DotNet/ExtractFileFromSelf.zip


    KSG


    • Edited by KareninstructorMVP Thursday, December 13, 2012 8:08 PM
    • Marked as answer by Mr. XML Friday, December 14, 2012 3:57 AM
    Thursday, December 13, 2012 8:05 PM
  • I've changed my suggestion. ;-)

    Let's do it this way: Always give it the same path so that it can be deleted (or attempted to be - assuming the user has closed the PDF file) on its way out:

    Option Strict On Option Explicit On ' Imports System.IO.Path ' Public Class Form1 ' Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load ' If ExtractPdf() Then Dim tempFilePath As String = Combine(GetTempPath(), "temp.pdf") ' Try Process.Start(tempFilePath) Catch ex As Exception MessageBox.Show("An error has occurred:" & vbCrLf & vbCrLf & ex.Message, _ "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End If ' End Sub ' Private Sub Form1_FormClosing(ByVal sender As Object, _ ByVal e As System.Windows.Forms.FormClosingEventArgs) _ Handles Me.FormClosing ' If e.CloseReason = CloseReason.UserClosing Then Dim pdfPath As String = Combine(GetTempPath(), "temp.pdf") ' If My.Computer.FileSystem.FileExists(pdfPath) Then Try My.Computer.FileSystem.DeleteFile(pdfPath) Catch ex As Exception ' Nothing you can do about it... End Try End If End If '' End Sub ' Private Function ExtractPdf() As Boolean ' Dim retVal As Boolean = True ' Try Dim tempFilePath As String = Combine(GetTempPath(), "temp.pdf") My.Computer.FileSystem.WriteAllBytes(tempFilePath, _ My.Resources.TestOutput_Dev11_2, False) Catch ex As Exception retVal = False End Try ' Return retVal '' End Function ' End Class



    Please call me Frank :)

    • Marked as answer by Mr. XML Friday, December 14, 2012 3:48 AM
    Thursday, December 13, 2012 4:50 PM
  • Hi Kevin,

    You have saved me again.  Very elegant.  I will need some time to absorb it.

    I am hoping to get it to work with the rest of my project. l will have to perform minor changes, to get to work under the the menustrip.

    Thank so much.  Sam

    • Marked as answer by Mr. XML Friday, December 14, 2012 3:57 AM
    Friday, December 14, 2012 3:57 AM

All replies

  • I've changed my suggestion. ;-)

    Let's do it this way: Always give it the same path so that it can be deleted (or attempted to be - assuming the user has closed the PDF file) on its way out:

    Option Strict On Option Explicit On ' Imports System.IO.Path ' Public Class Form1 ' Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load ' If ExtractPdf() Then Dim tempFilePath As String = Combine(GetTempPath(), "temp.pdf") ' Try Process.Start(tempFilePath) Catch ex As Exception MessageBox.Show("An error has occurred:" & vbCrLf & vbCrLf & ex.Message, _ "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End If ' End Sub ' Private Sub Form1_FormClosing(ByVal sender As Object, _ ByVal e As System.Windows.Forms.FormClosingEventArgs) _ Handles Me.FormClosing ' If e.CloseReason = CloseReason.UserClosing Then Dim pdfPath As String = Combine(GetTempPath(), "temp.pdf") ' If My.Computer.FileSystem.FileExists(pdfPath) Then Try My.Computer.FileSystem.DeleteFile(pdfPath) Catch ex As Exception ' Nothing you can do about it... End Try End If End If '' End Sub ' Private Function ExtractPdf() As Boolean ' Dim retVal As Boolean = True ' Try Dim tempFilePath As String = Combine(GetTempPath(), "temp.pdf") My.Computer.FileSystem.WriteAllBytes(tempFilePath, _ My.Resources.TestOutput_Dev11_2, False) Catch ex As Exception retVal = False End Try ' Return retVal '' End Function ' End Class



    Please call me Frank :)

    • Marked as answer by Mr. XML Friday, December 14, 2012 3:48 AM
    Thursday, December 13, 2012 4:50 PM
  • If you would like to view the PDF in a windows form within your application, take Frank's code excluding the process start and pass it to a function in the link below which is a VB.NET project for viewing PDF files in a win-form project via a webbrowser control.

    http://kevininstructor.home.comcast.net/~kevininstructor/DotNet/PDF_View.zip

    Here is a partial working example of what Frank provided less the removal of the file

    http://kevininstructor.home.comcast.net/~kevininstructor/DotNet/ExtractFileFromSelf.zip


    KSG


    • Edited by KareninstructorMVP Thursday, December 13, 2012 8:08 PM
    • Marked as answer by Mr. XML Friday, December 14, 2012 3:57 AM
    Thursday, December 13, 2012 8:05 PM
  • Very nice Kevin!

    One thing that I'll point out though is that this assumes the user has a version of Acrobat reader which supports it - the older ones didn't but it has for several years now.


    Please call me Frank :)

    Thursday, December 13, 2012 8:13 PM
  • Thanks, my main object was to compliment your reply.

    KSG

    Thursday, December 13, 2012 8:19 PM
  • Thanks, my main object was to compliment your reply.

    KSG


    I may have a use for this in something of my own - I hope that's ok?

    Please call me Frank :)

    Thursday, December 13, 2012 8:21 PM
  • Thanks, my main object was to compliment your reply.

    KSG


    I may have a use for this in something of my own - I hope that's ok?

    Please call me Frank :)

    It is public now :-)


    KSG

    Thursday, December 13, 2012 8:23 PM
  • It is public now :-)


    KSG

    True, but I thought I'd ask.

    Thanks Kevin!


    Please call me Frank :)

    Thursday, December 13, 2012 8:32 PM
  • Hi Frank,

    Great code.  I am very appreciative.

    Friday, December 14, 2012 3:48 AM
  • Hi Kevin,

    You have saved me again.  Very elegant.  I will need some time to absorb it.

    I am hoping to get it to work with the rest of my project. l will have to perform minor changes, to get to work under the the menustrip.

    Thank so much.  Sam

    • Marked as answer by Mr. XML Friday, December 14, 2012 3:57 AM
    Friday, December 14, 2012 3:57 AM