C# 从 Winform 显示 pdf 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2220134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:30:20  来源:igfitidea点击:

Displaying a pdf file from Winform

c#winformspdf

提问by gsvirdi

I'm just creating a simple calculator in C# (windows form)

我只是在 C# (windows 窗体) 中创建一个简单的计算器

I've created a "User Help" which is a pdf file, what I want is to display that pdf file if the user clicks on the "Help" button in the WinForm. If assumed that Adobe reader is pre-installed on the user's machine....

我创建了一个“用户帮助”,它是一个 pdf 文件,如果用户单击 WinForm 中的“帮助”按钮,我想要的是显示该 pdf 文件。如果假设用户机器上预装了 Adob​​e Reader....

How to open the pdf file on button click in winForm?

如何在winForm中单击按钮打开pdf文件?

I don't plan to provide this pdf file on hard disk of user. Which means that I have to embed this pdf into the calculator (winForm) and have to display it on the button click.

我不打算在用户的硬盘上提供这个 pdf 文件。这意味着我必须将此 pdf 嵌入到计算器 (winForm) 中,并且必须在单击按钮时显示它。

Kindly guide me with the best practise for displaying an embedded file in winForm.

请指导我在 winForm 中显示嵌入文件的最佳实践。

采纳答案by Oliver

I would put it on within my program folder, add a link within my Start Menu folder to allow a direct access (without starting my tool) and just at on some click event System.Diagnostics.Process.Start(@".\Manual.pdf");

我会将它放在我的程序文件夹中,在我的“开始”菜单文件夹中添加一个链接以允许直接访问(无需启动我的工具),并且只是在某些点击事件中 System.Diagnostics.Process.Start(@".\Manual.pdf");

Update

更新

Ok, now we come to a completely new question: How to embed a file in my application and start it?

好的,现在我们来到一个全新的问题:如何在我的应用程序中嵌入文件并启动它?

For this question you'll find already several answers here, but here is the short version:

对于这个问题,你会在这里找到几个答案,但这里是简短的版本:

  1. Right click your project and select Add - Existing Item
  2. Select your file (don't double click it)
    • Click the little arrow next to the Add button and select Add As Link
  3. Double click on Properties - Resources.resx
  4. Click the little arrow next to Add Resource and select Add Existing File
  5. Select the same file again in the open dialog
  6. Now you can access the file within your code as byte[]from Properties.Resources.NameOfResource
  1. 右键单击您的项目并选择添加 - 现有项目
  2. 选择您的文件(不要双击它
    • 单击“添加”按钮旁边的小箭头,然后选择“添加为链接”
  3. 双击属性 - Resources.resx
  4. 单击添加资源旁边的小箭头并选择添加现有文件
  5. 在打开的对话框中再次选择相同的文件
  6. 现在您可以byte[]从代码中访问该文件Properties.Resources.NameOfResource

With these steps you reference your file where ever it exists within your structure. If you like that a copy of your pdf file will be put into a subfolder Resources within your project, just skip the points one and two in the above list.

通过这些步骤,您可以引用您的文件在您的结构中的任何位置。如果您喜欢将 pdf 文件的副本放入项目中的子文件夹 Resources 中,只需跳过上面列表中的第一和第二点。

To get your pdf now opened, you'll have to write the byte[] down to disk (maybe with Path.GetTempFileName()) and start it with Adobe Reader. (Don't forget to delete the file after usage)

要现在打开您的 pdf,您必须将 byte[] 写入磁盘(可能使用Path.GetTempFileName())并使用 Adob​​e Reader 启动它。(使用后不要忘记删除文件)

回答by Snake

I would suggest converting your pdf file to a Microsoft help file, so that you don't need to have Adobe Reader installed (it's buggy, and has way too much security issues). You cannot expect users to have this.

我建议将您的 pdf 文件转换为 Microsoft 帮助文件,这样您就不需要安装 Adob​​e Reader(它有问题,并且存在太多安全问题)。你不能指望用户有这个。

In reply to the starter's comment:

回复楼主的评论:

Yes you would need to create your help file as an HTML document instead of a pdf. There is no easy way to convert pdf to HTML.

是的,您需要将帮助文件创建为 HTML 文档而不是 pdf。没有简单的方法可以将 pdf 转换为 HTML。

回答by Assaf Lavie

It might be possible to embed Adobe's Reader in your form as an ActiveX component. But that means you'll have to make sure Reader is installed on the client machine for that to work.

可以将 Adob​​e 的 Reader 作为 ActiveX 组件嵌入您的表单中。但这意味着您必须确保在客户端计算机上安装了 Reader 才能正常工作。

In case it doesn't have to be strictly embedded you can just launch the PDF file and let whatever viewer the user has open it.

如果不必严格嵌入,您可以启动 PDF 文件并让用户打开它的任何查看器。

回答by Ian Ringrose

You could use the WebBrowser controland let IE load a PDF reader for you if there is one installed on the machine.

您可以使用WebBrowser 控件并让 IE 为您加载 PDF 阅读器(如果机器上安装了 PDF 阅读器)。

However the last time I tried this, I had to write the PDF file to disk first, so I could point the WebBrowser control at it.

然而,上次我尝试这样做时,我必须先将 PDF 文件写入磁盘,以便我可以将 WebBrowser 控件指向它。

回答by Winston Smith

You can reference the Adobe Reader ActiveX control and bundle it with your application.

您可以引用 Adob​​e Reader ActiveX 控件并将其与您的应用程序捆绑在一起。

Simply add AcroPDF.PDF.1to your Toolbox from the COM Components tab (right click toolbox and click Choose Items...) then drag an instance onto your Winform to have the designer create the code for you. Alternately, after adding the necessary reference you can use the following code:

只需AcroPDF.PDF.1从 COM 组件选项卡添加到您的工具箱(右键单击工具箱并单击Choose Items...),然后将一个实例拖到您的 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;

回答by Tommy

If your user has Adobe Reader (or any other PDF reader) installed on their machine, you could use:

如果您的用户在他们的机器上安装了 Adob​​e Reader(或任何其他 PDF 阅读器),您可以使用:

System.Diagnostics.Process.Start(
       "My-PDF-file.pdf");

Hope this helps.

希望这可以帮助。

Note: Obviously, this will fail if the user does not have any PDF Reader applications installed.

注意:显然,如果用户没有安装任何 PDF 阅读器应用程序,这将失败。

回答by Cory

There is a C# pdf viewer project on google code. http://code.google.com/p/pdfviewer-win32/there is the viewer and there is the library that it uses available that uses mupdf and xpdf to render the pdf documents in your winforms program. I am currently developing a User control library for people to use and drop into their programs for pdf viewing capabilities. it works pretty good.

谷歌代码上有一个 C# pdf 查看器项目。http://code.google.com/p/pdfviewer-win32/有查看器和它使用的可用库,它使用 mupdf 和 xpdf 在您的 winforms 程序中呈现 pdf 文档。我目前正在开发一个用户控件库,供人们使用并放入他们的程序中以获得 pdf 查看功能。它工作得很好。

回答by Bipzz Thapa

AxAcroPDF1.LoadFile("C:ShippingForm.pdf")
AxAcroPDF1.src = "C:ShippingForm.pdf"
AxAcroPDF1.setShowToolbar(False)
AxAcroPDF1.setView("fitH")
AxAcroPDF1.setLayoutMode("SinglePage")
AxAcroPDF1.setShowScrollbars(False)
AxAcroPDF1.Show()

回答by Scott Net

If you want to display a pdf inside your application, the WebBrowser control is probably preferable over the Adobe Reader control, as it will open the file very smoothly in PDF Reader or whatever IE is using as a default to open pdfs. You simply add the WebBrowser control to an existing or new form and navigate to the pdf file.

如果您想在应用程序中显示 pdf,WebBrowser 控件可能比 Adob​​e Reader 控件更可取,因为它可以在 PDF Reader 或 IE 使用的任何默认打开 pdf 文件中非常流畅地打开文件。您只需将 WebBrowser 控件添加到现有或新表单并导航到 pdf 文件。

Never assume that a user has Adobe or any other third party controls or libraries installed on their machines, always package them with your executable or you may have problems.

永远不要假设用户在他们的机器上安装了 Adob​​e 或任何其他第三方控件或库,始终将它们与您的可执行文件打包在一起,否则您可能会遇到问题。

The Adobe Reader control obviously doesn't integrate as well with .NET as an intrinsic Windows component. As a rule, I always favor the use of built in .Net controls over third party vendors. As far as embedding the file in the actual executable; not going to happen until Microsoft decides any old PDF can be worked into the CLS and compiled into MSIL. What you have when you develop any app in .NET is code that can be compiled into intermediate MSIL to be translated at runtime by the CLR into native code and executed in the OS.

Adobe Reader 控件显然没有像固有的 Windows 组件那样与 .NET 集成。作为一项规则,我总是喜欢使用内置的 .Net 控件而不是第三方供应商。至于将文件嵌入到实际的可执行文件中;除非 Microsoft 决定任何旧的 PDF 都可以用于 CLS 并编译为 MSIL,否则不会发生。当您在 .NET 中开发任何应用程序时,您所拥有的是可以编译为中间 MSIL 的代码,以便在运行时由 CLR 转换为本机代码并在操作系统中执行。

WebBrowser1.Navigate("SomePDF.pdf");

回答by Ricardo Fercher

How to open PDF file with relative path

如何使用相对路径打开PDF文件

In this case the created Application has to run on several PC′s. To reference on a file which is not in the network, but in the Programm Folder itself, use the following code Snippet:

在这种情况下,创建的应用程序必须在多台 PC 上运行。要引用不在网络中但在程序文件夹本身中的文件,请使用以下代码片段:

First of all include the following Library:

首先包括以下库:

using System.IO;

Then use a Button, picturebox, or whateverto create a ClickEvent and use the following code snippet:

然后使用Buttonpicturebox其他任何东西来创建 ClickEvent 并使用以下代码片段:

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

    }

|Thumb up|

|竖起大拇指|