[英]Include and launch PDF from C# Visual Studio Project
In my C# application i have a help button. 在我的C#应用程序中,我有一个帮助按钮。 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")
. 按下该按钮后,我希望应用程序在系统默认的PDF阅读器中打开PDF文件,我可以使用诸如
Process.Start("pathToPDF")
类的命令来完成此操作。
The problem is that I would like to include the PDF as a resource instead of calling an external file. 问题是我想将PDF包含为资源,而不是调用外部文件。 I do not wish to copy the PDF to the users computer and do not want to host it online or on a NAS.
我不想将PDF复制到用户计算机,也不想在线或在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.). 在解决方案资源管理器中右键单击您的项目,然后添加现有文件并选择pdf(如果找不到它,请确保显示的是所有文件,而不仅仅是.cs文件等)。
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); 现在,您可以使用Process.Start(filename.pdf);打开预期的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 ) 不提供文件而显示PDF的唯一安全方法是包括您自己的查看器组件(例如, 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. 某些组件允许将“内存”中的PDF(如在嵌入式资源中)直接加载到Viewer Component中,另一种方法是创建一个加密的二进制文件,以随应用程序一起提供,并在必要时进行加密/加载。
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. 一旦您想在外部查看器中显示PDF,请注意,用户仍然可以保存PDF。
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.
可以将文件作为资源嵌入,但是这样会带来一个问题,即如果您临时提取文件以显示它,则无法真正控制该文件的清理,因为该文件已被PDF锁定读者申请书。 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.
因此,无论如何,您最终都会得到PDF文件;)为了使您的应用程序文件夹更整洁,您可以做的是不要将PDF安装在该应用程序文件夹下,而是在“公共文档”目录下。
For Example: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments), "MySoftware", "Help.pdf") 例如:Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments),“ MySoftware”,“ Help.pdf”)
Which normally targets: C:\\Users\\Public\\Documents\\MySoftware\\Help.pdf 通常针对的对象是:C:\\ Users \\ Public \\ Documents \\ MySoftware \\ Help.pdf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.