9 Replies - 1418 Views - Last Post: 15 July 2018 - 08:55 AM Rate Topic: -----

#1 RowelF   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-July 18

Visual Basic - To Open PDF in Menu Strip but Activating PDF Control

Posted 14 July 2018 - 03:30 AM

I have a PDF which i want to open when user clicks a ToolMenuStripItem

I know how to open a file from drive location but how to open PDF from my.resources so that it can be deployed with the application
Is This A Good Question/Topic? 0
  • +

Replies To: Visual Basic - To Open PDF in Menu Strip but Activating PDF Control

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14862
  • View blog
  • Posts: 59,297
  • Joined: 12-June 08

Re: Visual Basic - To Open PDF in Menu Strip but Activating PDF Control

Posted 14 July 2018 - 07:26 AM

Is this really VB6 or VB.NET?
Was This Post Helpful? 0
  • +
  • -

#3 RowelF   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-July 18

Re: Visual Basic - To Open PDF in Menu Strip but Activating PDF Control

Posted 14 July 2018 - 10:19 AM

I have a PDF file in my Resources which I want to open when user clicks a ToolMenuStripItem. But please take note, when user click the menu item, it should activate the PDF Browser Control where the pdf file should come out for reading (just for READING only).

I already know how to open PDF file within the Menu strip (refer to the code below, but this opens OUTSIDE the program) but I want the pdf file stored in my Resosurces opened within the PDF Browser control.

Dim pdf As Byte() = Form1.My.Resources.APPROXIMATIONpdf
Using tmp As New FileStream("test.pdf", FileMode.Open)
tmp.Write(pdf, 0, pdf.Length)
End Using
Process.Start("test.pdf")

Thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#4 RowelF   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-July 18

Re: Visual Basic - To Open PDF in Menu Strip but Activating PDF Control

Posted 14 July 2018 - 10:24 AM

View Postmodi123_1, on 14 July 2018 - 07:26 AM, said:

Is this really VB6 or VB.NET?


Ooops. I stand to be corrected. I am using Visual Basic in Visual Studio. I already posted my complete question in the VB.Net. Thanks.
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1533
  • View blog
  • Posts: 3,861
  • Joined: 01-February 13

Re: Visual Basic - To Open PDF in Menu Strip but Activating PDF Control

Posted 14 July 2018 - 03:24 PM

If you have Adobe Acrobat Reader installed, you can add the Adobe Pdf Reader control to your Visual Studio toolbox. On the Visual Studio menu select (Tools) and then click (Choose Toolbox Items) in the drop-down menu. When the (Choose Toolbox Items) window opens, select the (COM) tab. You should find (Adobe Pdf Reader) listed, check that checkbox and then press the (OK) button.

Now you will be able to add an (AxAcroPDF) control to your Form in the designer. You can use its LoadFile method to load a pdf file from the hard drive as seen here.
AxAcroPDF1.LoadFile("C:\TestFolder\MyPdfFile.pdf")


You can not load the file directly from the application resources and adding a pdf file to the resources would not be the first way I would recommend including a file with your application. Normally I would recommend keeping the file(s) in a folder relative to the application's exe file and include that folder when setting up your application's installation package so it is installed along with your application.

However, if you can't be talked out of adding it to the resources, then you will still have to write the file from your resources to the hard drive as a pdf file before opening it anyways. For example...
        IO.File.WriteAllBytes("C:\TestFolder\MySavedPdfFile.pdf", My.Resources.MyPdfFile) 'write resource bytes to pdf file
        AxAcroPDF1.LoadFile("C:\TestFolder\MySavedPdfFile.pdf") 'load the pdf file


Was This Post Helpful? 1
  • +
  • -

#6 RowelF   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-July 18

Re: Visual Basic - To Open PDF in Menu Strip but Activating PDF Control

Posted 14 July 2018 - 06:16 PM

Thanks!

Looked good as it started but when I hit the menu strip, I noticed that indeed the MySavedPdffile.pdf was loaded in the C:\TestFolder. However, as the application is running, a notification "Exception Unhandled - System.NullReferenceUnhandled: Object Variable or With Block Variable not set" popped up.

The new code for the Menu strip incorporating your comments as follows:

Imports System.IO
Imports System.Diagnostics
Imports AcroPDFLib
Imports AxAcroPDFLib
  
  Public Class Mainmenu  
    Private ReadOnly AxAcroPDF1 As Object
    Private Sub FractionRulesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewtonsMofAToolStripMenuItem.Click
        IO.File.WriteAllBytes("C:\TestFolder\MySavedPdfFile.pdf", My.Resources.APPROXIMATION)
        'load the pdf file
        AxAcroPDF1.LoadFile("C:\TestFolder\MySavedPdfFile.pdf")
        Dim pdf As Byte() = Form1.My.Resources.APPROXIMATION
        Using tmp As New FileStream("MySavedPdffile.pdf", FileMode.Open)
            tmp.Write(pdf, 0, pdf.Length)
        End Using
        Process.Start("MySavedPdffile.pdf")  



I can't find the code that the "menu strip" code links to the PDF browser control where the pdf file should come out as follows:

Private Sub Method_Enter(sender As Object, e As EventArgs) Handles Method.Enter

End Sub

I believe there must be a connection between these two.

Thanks in advance for the solution.

This post has been edited by IronRazer: 15 July 2018 - 03:06 AM
Reason for edit:: Inserted Code Tags

Was This Post Helpful? 0
  • +
  • -

#7 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1533
  • View blog
  • Posts: 3,861
  • Joined: 01-February 13

Re: Visual Basic - To Open PDF in Menu Strip but Activating PDF Control

Posted 15 July 2018 - 03:28 AM

Please insert your code between code tags when posting code. Also, when you get an error/exception you should indicate which line of code it happens on and the full error message in your post.

If you have successfully added the AxAcroPDF control to your toolbox and then added one of them to your Form from the toolbox, then all you should need to use is the lines below. The rest of your code is not needed for doing this.
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        IO.File.WriteAllBytes("C:\TestFolder\MySavedPdfFile.pdf", My.Resources.MyPdfFile)
        AxAcroPDF1.LoadFile("C:\TestFolder\MySavedPdfFile.pdf")
    End Sub



When I press Button1 (code above), it first saves the pdf file to the hard drive, then loads that pdf file from the hard drive into the AxAcroPDF control that is on my Form.
Attached Image
Was This Post Helpful? 1
  • +
  • -

#8 RowelF   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-July 18

Re: Visual Basic - To Open PDF in Menu Strip but Activating PDF Control

Posted 15 July 2018 - 06:16 AM

Thank you for your prompt reply Mr. IronRazer. I was actually thinking that this can't be done because I read one write up that there is an issue between Adobe and Microsoft, thus the issue of "Variable not set". But obviously, you made it running with that very short codes, salute!

However, please take note that I will not be using any BUTTON for this purpose. My plan is to use the Menu and not the button to launch the pdf file.

Will that same code be applicable using the menu, specifically the Files1 menu strip? or maybe it will be a mission impossible... I have been searching the net for long now but could not find. Lucky to be here.


Thanks again!
Was This Post Helpful? 0
  • +
  • -

#9 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1533
  • View blog
  • Posts: 3,861
  • Joined: 01-February 13

Re: Visual Basic - To Open PDF in Menu Strip but Activating PDF Control

Posted 15 July 2018 - 06:33 AM

I am not sure what menu you are talking about but, I am guessing it is a ToolStrip or MenuStrip control that you have added to the Form. If you are using either of these, then each item in the menu has a Click event just the same as a Button control has a Click event. You just need to put those two lines of code into the Click event sub of the menu item.

Go to the menu item you want to open the pdf in the Form's Designer window. Now double click on the menu item and that will create the Click event sub if it does not exist in your code already. It will then take you to the code view of that menu item's Click event sub. That is where the code should go.
Was This Post Helpful? 0
  • +
  • -

#10 RowelF   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-July 18

Re: Visual Basic - To Open PDF in Menu Strip but Activating PDF Control

Posted 15 July 2018 - 08:55 AM

As I touched some settings in my original VB file, I started a new one and took your advice and finally got it running.

A big thank you idol, petmalu RazorHead and to dream.in.code administrators. I can focus now in preparing the documents I need.

God bless the community :flowers:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1