简体   繁体   中英

Include and launch PDF from C# Visual Studio Project

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.

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM