Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a .pdf file in windows form through a button click

Tags:

c#

pdf

winforms

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.

like image 508
Jed5931 Avatar asked Apr 19 '16 14:04

Jed5931


People also ask

How do I open a PDF in Windows form?

It is a free Adobe Acrobat PDF Reader. Start C# Windows application and add the control to the C# Toolbox. Right-click on any tab of toolbox and select "Choose Items... Select the "COM Components" tab and click the check "Adobe PDF Reader" and click OK.

How do I change the way my PDF opens?

1. Right-click the PDF, choose Open With > Choose default program or another app in.

How do I force a PDF to open at a specific view?

With the PDF document open in Acrobat, click Document Properties on the File menu. The Document Properties dialog box is displayed. Select Initial View tab. The Initial View options are displayed.

How are PDF files opened?

Find the PDF you want to open in your Files and double click to open. Select Adobe Acrobat (or whichever reader you downloaded) from the list of available options. If no list appears or the page opens in another application, you can right-click the file and select Open With to choose your PDF reader. Click Open.


1 Answers

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.

like image 187
Artem Avatar answered Oct 07 '22 15:10

Artem