我正在用C#(Windows窗体)创建一个简单的计算器。
我已经创建了一个“用户帮助”文件,它是一个pdf文件。我想要的是,在用户单击WinForm中的“帮助”按钮时显示该pdf文件。假设用户的计算机上预安装了Adobe Reader....
如何在WinForm中的按钮点击时打开pdf文件?我不打算将此pdf文件提供给用户的硬盘驱动器。这意味着我必须将此pdf嵌入到计算器(WinForm)中,并在按钮单击时显示它。
请指导我在WinForm中显示嵌入文件的最佳实践。我正在用C#(Windows窗体)创建一个简单的计算器。
我已经创建了一个“用户帮助”文件,它是一个pdf文件。我想要的是,在用户单击WinForm中的“帮助”按钮时显示该pdf文件。假设用户的计算机上预安装了Adobe Reader....
如何在WinForm中的按钮点击时打开pdf文件?我不打算将此pdf文件提供给用户的硬盘驱动器。这意味着我必须将此pdf嵌入到计算器(WinForm)中,并在按钮单击时显示它。
请指导我在WinForm中显示嵌入文件的最佳实践。您可以引用Adobe Reader ActiveX控件并将其捆绑到您的应用程序中。
只需从COM组件选项卡(右键单击工具箱,然后单击“选择项目...”)添加AcroPDF.PDF.1
到您的工具箱中,然后将一个实例拖放到您的Winform上,设计师会为您创建代码。或者,在添加必要的引用之后,您可以使用以下代码:
AxAcroPDFLib.AxAcroPDF pdf = new AxAcroPDFLib.AxAcroPDF();
pdf.Dock = System.Windows.Forms.DockStyle.Fill;
pdf.Enabled = true;
pdf.Location = new System.Drawing.Point(0, 0);
pdf.Name = "pdfReader";
pdf.OcxState = ((System.Windows.Forms.AxHost.State)(new System.ComponentModel.ComponentResourceManager(typeof(ViewerWindow)).GetObject("pdfReader.OcxState")));
pdf.TabIndex = 1;
// Add pdf viewer to current form
this.Controls.Add(pdf);
pdf.LoadFile(@"C:\MyPDF.pdf");
pdf.setView("Fit");
pdf.Visible = true;
AxInterop.AcroPDFLib.dll
和Interop.AcroPDFLib.dll
),它用于显示PDF。 - Winston SmithWebBrowser1.Navigate("SomePDF.pdf");
我会把它放在我的程序文件夹中,在我的开始菜单文件夹中添加一个链接以允许直接访问(而无需启动我的工具),仅在某个点击事件上使用System.Diagnostics.Process.Start(@".\Manual.pdf");
好的,现在我们来到一个全新的问题:如何将文件嵌入到我的应用程序并启动它?
对于这个问题,你已经在这里找到了几个答案,但这是简短的版本:
Properties.Resources.NameOfResource
中访问byte[]
格式的文件通过这些步骤,您可以引用您的文件,无论它存在于您的结构中的任何位置。如果您希望将pdf文件的副本放入项目的子文件夹Resources中,只需跳过上面列表中的第一和第二点。
要现在打开您的pdf文件,您需要将byte[]
写入磁盘(可能使用Path.GetTempFileName()
),然后使用Adobe Reader启动它。(不要忘记在使用后删除文件)
在Google Code上有一个C# PDF查看器项目。http://code.google.com/p/pdfviewer-win32/该项目包含了一个查看器和一个库,它们使用mupdf和xpdf来呈现PDF文档,可以在WinForms程序中使用。我目前正在开发一个用户控件库,供人们使用并将其放入他们的程序中以实现PDF查看功能。它运行得非常好。
如果用户在他们的计算机上安装了Adobe Reader(或任何其他PDF阅读器),则可以使用:
System.Diagnostics.Process.Start(
"My-PDF-file.pdf");
希望这可以帮到您。
注意:如果用户没有安装任何PDF阅读器应用程序,显然这将失败。
https://www.codeproject.com/Articles/37458/PDF-Viewer-Control-Without-Acrobat-Reader-Installe
使用VB编写,依赖许多(部分为商业库,如果您的解决方案是商业的)库。 PdfiumViewerhttps://github.com/pvginkel/PdfiumViewer
是很棒的,而且可以通过NuGet获取。
The PdfiumViewer library primarily consists out of three components:
•The PdfViewer control. This control provides a host for the PdfRenderer control and has a default toolbar with limited functionality;
•The PdfRenderer control. This control implements the raw PDF renderer. This control displays a PDF document, provides zooming and scrolling functionality and exposes methods to perform more advanced actions;
•The PdfDocument class provides access to the PDF document and wraps the Pdfium library.
data = File.ReadAllBytes(@"C:\temp\abc.pdf");
PdfiumViewer.PdfDocument doc;
using (Stream stream = new MemoryStream(data))
{
doc = PdfiumViewer.PdfDocument.Load(stream);
var viewer = new PdfiumViewer.PdfViewer();
viewer.Document = doc;
var form = new System.Windows.Forms.Form();
form.Size = new Size(600, 800);
viewer.Dock = System.Windows.Forms.DockStyle.Fill;
form.Controls.Add(viewer);
form.ShowDialog();
}
你可以将Adobe Reader作为一个ActiveX组件嵌入到表单中。但这意味着你必须确保客户端机器上已安装了Reader才能正常工作。
如果不一定需要严格嵌入,你可以直接启动PDF文件,让用户使用自己的阅读器打开它。
在本例中,创建的应用程序必须在多台计算机上运行。要引用不在网络中但在程序文件夹本身中的文件,请使用以下代码片段:
首先包含以下库:
using System.IO;
然后使用一个按钮、图片框或者其它控件来创建一个Click事件,并使用下面的代码片段:
private void pictureBox2_Click(object sender, EventArgs e)
{
//get current folderpath of the .exe
string ProgramPath = AppDomain.CurrentDomain.BaseDirectory;
//jump back relative to the .exe-Path to the Resources Path
string FileName = string.Format("{0}Resources\\Master_Thesis_Expose.pdf", Path.GetFullPath(Path.Combine(ProgramPath, @"..\..\")));
//Open PDF
System.Diagnostics.Process.Start(@"" + FileName + "");
}