21

On the current window that I have, I have a button. I want to be able to click the button and open up a .pdf file which is in the resources folder of this project. Is there an easy want to do this?

The other methods I've looked at uses filepaths but the filepaths may not be the same all the time but the .pdf file will be in the resources folder at all times. Is there a way to access this and open it when the button is clicked?

Anything along the lines of?

string filename = "instructions.pdf";
file.open();

Problem solved with

private void Button1_Click(object sender, EventArgs e)
{
    string filename = "instructions.pdf";
    System.Diagnostics.Process.Start(filename);
}

With instructions.pdf in the bin/debug folder where the program.exe is.

Jed5931
  • 265
  • 1
  • 2
  • 8

3 Answers3

37

To open a file with a system default viewer you need call

System.Diagnostics.Process.Start(filename);

But I haven't understood the problem with a filepath. If you need a relative path from the program .exe file to a folder with resources, then you can add "Resources\" or "..\Resources\" (if Resources folder is higher) to your filepath.

Or you can add your pdf to a project as an embedded resource and then, when you need to open it, you can save it to some temporal location using

Path.GetTempPath()

and open it.

Artem
  • 1,535
  • 10
  • 13
  • My problem with the path was that it may include `C:\My Documents` and if I was to open it in a different computer, the path wouldn't necessarily be the same. I'll try to work it out with your suggestion – Jed5931 Apr 19 '16 at 14:55
  • You can keep the file near the application (in the same folder) and open it just as "instructions.pdf" without specifying folder. – Artem Apr 19 '16 at 15:09
  • 5
    The Process.Start() mostly worked. I was seeing error: “The specified executable is not a valid application for this OS platform” That was resolved by this post: https://stackoverflow.com/a/47066203/2862 I was running as a .NET Core 3.1 WinForms add under Windows 10. – John Dyer Nov 18 '20 at 21:32
1

If you want to open the pdf file using Adobe Reader or similar application, you can use Process.Start function.

ProcessStartInfo startInfo = new ProcessStartInfo("pathtofile");
Process.Start(startInfo);

This will behave as you clicked on the file in Windows folder. If you cannot place the file path, then you can copy file from resource to a temporary folder and use that path.

Mafii
  • 7,227
  • 1
  • 35
  • 55
ali
  • 1,301
  • 10
  • 12
  • this will not guarantee opening of Acrobat Reader. It will open in the machines default app for PDF files, not the same thing. – t.durden Feb 26 '20 at 21:50
-1

To open PDF file from windows application C# follow the following steps :

1- open new windows application

2- go to Toolbox then Right Click any where then select Choose Items see image :

enter image description here

3- Go to COM Components and select Adobe PDF Reader see image :

enter image description here

4- go again to Toolbox and search for control Adobe PDF Reader see image :

enter image description here

5- drag and drop the control to your windows form or double click it

6- drag button or stripe menu

7- double click the button or menu and write the following code :

            string filename = "MyPDF.pdf";
            System.Diagnostics.Process.Start(filename);

8- copy your pdf file to the path of your program and paste it in debug folder in the following path :

c:\myprojectfiles\bin\debug\MyPDF.pdf

9- Run your program and click the button it will open the PDF file.

Hope this will help you

MarwanAbu
  • 181
  • 8