0

In my C# application i have a help button. When it's pressed I would like for the application to open up a PDF file in the systems default PDF reader, something I can do with a command like Process.Start("pathToPDF").

The problem is that I would like to include the PDF as a resource instead of calling an external file. I do not wish to copy the PDF to the users computer and do not want to host it online or on a NAS.

  • Any 1 in following could be correct solution http://stackoverflow.com/questions/4504442/viewing-pdf-in-windows-forms-using-c-sharp – KulOmkar Sep 26 '16 at 12:28

2 Answers2

0

Right click on your project in the Solution Explorer, then add existing file and choose your pdf (if you cannot find it, make sure you are showing all files and not just .cs files etc.).

Click on the newly added item once in the solution explorer and in the properties window, you set Copy to Output Directory to Copy Always or Copy if newer.

Now you can open the pdf file as expected using Process.Start(filename.pdf);

Sir JokesALot
  • 214
  • 2
  • 5
0

The only Secure way to show a PDF without providing a file is to include your own Viewer Component (Ex. http://www.o2sol.com/pdfview4net/overview.htm)

Some components allow to load a PDF from Memory (as in a embedded Resource) directly into your Viewer Component, another way would be to create an encrypted binary file to ship with your application and encrypt/load when necessary.

As soon you want to show the PDF in an external viewer ,be aware that the User will have the ability to save the PDF anyway.

Maybe you can explain your reasons to not want to include the file, so we can suggest other solutions to you?

Update: As noted in your comment, the goal is to have a clean installation. It would be possible to embed the File as a resource, but then you would have the problem that if you extract the file temporarily to display it, you can't really control the clean-up of that file, because it's locked by the PDF Reader Application. So you would end up with the PDF File anyway ;) What you can do to keep your Application Folder cleaner, is to not install the PDF under that Application Folder but under the "Common Documents" Directory.

For Example: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments), "MySoftware", "Help.pdf")

Which normally targets: C:\Users\Public\Documents\MySoftware\Help.pdf

Markus Doerig
  • 166
  • 1
  • 3
  • It doesn't have anything to do with security. Just want to keep the directories clean. – Cyber Sausage Sep 26 '16 at 13:06
  • as larger your application will get as more you will forget the "Single-Executable" goal. It's normal that a .Net Application has many files. To provide a single-file-Setup for your Application is what the End-User really wants, he mostly doesn't care of how many files your application creates (he often doesn't even know where they are located). – Markus Doerig Sep 27 '16 at 13:00
  • You guys are probably right. Copied the PDF to the temp directory if it doesn't exist and then opens it. – Cyber Sausage Sep 28 '16 at 08:45