C# .NET在没有外部依赖的winform中打开PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/549504/
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
.NET open PDF in winform without external dependencies
提问by PeteT
Is there a FREE library which will allow me to open a pdf and show it on a winform project. I know I could open it in adobe reader or something but it always seems so bloated to me and I would be relying on it being installed. Is there a nice lightweight alternative where I could just include a dll in my project or similar avoiding external dependencies.
是否有一个免费的库可以让我打开 pdf 并在 winform 项目中显示它。我知道我可以在 adobe reader 或其他东西中打开它,但对我来说它总是那么臃肿,我会依赖它被安装。是否有一个不错的轻量级替代方案,我可以在我的项目中包含一个 dll 或类似的避免外部依赖项。
I don't need much functionality just view, change page zoom..
我不需要太多功能只是查看,更改页面缩放..
I have seen a few libraries but they seem to be about creating PDF's not viewing.
我看过一些图书馆,但它们似乎是关于创建 PDF 的而不是查看。
采纳答案by ShuggyCoUk
The ActiveX control installed with acrobat reader should work, either drop it in via the designer or use something like this.
与Acrobat Reader安装ActiveX控件应该工作,无论是通过设计师或使用类似的砸这个。
This will require the relevant version of the reader is installed on the client but since you indicate that you could just launch that anyway this should not be a problem. reader
这将需要在客户端上安装相关版本的阅读器,但由于您表示可以启动,无论如何这应该不是问题。读者
回答by Codingday
There is a free PDF library. It is mainly editing the contents but it might help.
有一个免费的PDF 库。它主要是编辑内容,但它可能会有所帮助。
回答by shahkalpesh
This is a guess.
这是一个猜测。
The way browser render PDF inside it with a plug-in. I am not sure whether it requires the application to be installed.
浏览器使用插件在其中渲染 PDF 的方式。我不确定它是否需要安装应用程序。
You can use Foxit reader OR Sumatra PDF.
And, you can host the browser control on your winform.
您可以使用 Foxit 阅读器或 Sumatra PDF。
而且,您可以在 winform 上托管浏览器控件。
回答by Renaud Bompuis
I'm not sure what you call a 'dependency' since you'll always have to rely on some external code to render the PDF unless you bundle the whole PDF rendering source inside your project.
我不确定你所说的“依赖”是什么,因为除非你将整个 PDF 渲染源捆绑在你的项目中,否则你总是不得不依赖一些外部代码来渲染 PDF。
There are some commercial renderers and very fewfree ones.
有一些商业渲染器和很少的免费渲染器。
If you want to avoid dependencies that you can't bundle then maybe you could have a look at the source codefor SumatraPDF, an OpenSource PDF viewer for windows that uses MuPDF.
There is also Poppler, a rendering engine that uses Xpdfas a rendering engine.
All of these are great but they will require a fair amount of commitment to make make them work and interface with .Net.
如果您想避免无法捆绑的依赖项,那么您可以查看SumatraPDF的源代码,这是一个使用MuPDF 的Windows 开源 PDF 查看器。
还有Poppler,一个使用Xpdf作为渲染引擎的渲染引擎。所有这些都很棒,但它们需要相当多的承诺才能使它们工作并与 .Net 交互。
Most other open source libraries are only used for creating and managing PDF pages and I don't know any that actually does rendering; it's such a hard problem to solve right.
大多数其他开源库仅用于创建和管理 PDF 页面,我不知道真正进行渲染的库;这是一个很难解决的问题。
You may still want to consider using GhostScriptas an interpreter because rendering pages is a fairly simple process.
The drawback is that you will need to either re-package it to install it with your app, or make it a pre-requisite (or at least a part of your install process).
It's not a big challenge, and it's certainly easier than having to massage the other rendering engines into .Net.
您可能仍要考虑使用GhostScript作为解释器,因为渲染页面是一个相当简单的过程。
缺点是您需要重新打包它以与您的应用程序一起安装,或者使其成为先决条件(或至少是安装过程的一部分)。
这不是一个大挑战,而且肯定比将其他渲染引擎按摩到 .Net 中要容易得多。
Have a look at this SO questionI answered today.
It contains some more information and a link to some working .Net codeI posted on another forum a few months ago.
回答by tbreffni
I would look into Foxit Readeras a lightweight alternative to Adobe Reader. It consists of a single .exe file that you can bundle with your application and thus shouldn't require any installation. According to their EULA this is allowed, as long as your application is not installed on mobile devices. All you would have to do is launch the reader as a new process, pointing to the appropriate pdf. Obviously the downside to this approach is it won't be integrated into your application's interface.
我会将Foxit Reader视为 Adobe Reader 的轻量级替代品。它由一个单独的 .exe 文件组成,您可以将其与您的应用程序捆绑在一起,因此不需要任何安装。根据他们的 EULA,这是允许的,只要您的应用程序未安装在移动设备上。您所要做的就是将阅读器作为一个新进程启动,指向相应的 pdf。显然,这种方法的缺点是它不会集成到您的应用程序界面中。
回答by Omar Shahine
First you need to reference the Adobe Reader ActiveX Control
首先你需要参考Adobe Reader ActiveX Control
Adobe Acrobat Browser Control Type Library 1.0
Adobe Acrobat 浏览器控件类型库 1.0
%programfiles&\Common Files\Adobe\Acrobat\ActiveX\AcroPDF.dll
%programfiles&\Common Files\Adobe\Acrobat\ActiveX\AcroPDF.dll
Then you just drag it into your Windows Form from the Toolbox.
然后您只需将它从工具箱拖到您的 Windows 窗体中。
And use some code like this to initialize the ActiveX Control.
并使用一些这样的代码来初始化 ActiveX 控件。
private void InitializeAdobe(string filePath)
{
try
{
this.axAcroPDF1.LoadFile(filePath);
this.axAcroPDF1.src = filePath;
this.axAcroPDF1.setShowToolbar(false);
this.axAcroPDF1.setView("FitH");
this.axAcroPDF1.setLayoutMode("SinglePage");
this.axAcroPDF1.Show();
}
catch (Exception ex)
{
throw;
}
}
Make sure when your Form closes that you dispose of the ActiveX Control
确保在您的表单关闭时处理 ActiveX 控件
this.axAcroPDF1.Dispose();
this.axAcroPDF1 = null;
otherwise Acrobat might be left lying around.
否则可能会留下 Acrobat。
回答by scotru
Take a look at this. It requires quite a few libraries (including GhostScript), so it's not terribly light weight. But all dependencies included native reading of PDF. It's in VB--but could be translated if you need C#:
看看这个。它需要相当多的库(包括 GhostScript),所以它的重量并不是特别轻。但是所有依赖项都包括 PDF 的本机阅读。它在 VB 中——但如果你需要 C#,可以翻译:
http://www.codeproject.com/Articles/37458/PDF-Viewer-Control-Without-Acrobat-Reader-Installe
http://www.codeproject.com/Articles/37458/PDF-Viewer-Control-Without-Acrobat-Reader-Installe
回答by CW1255
Put a webBrowser control on your form. strPdfFile = "C:\SomeFile.pdf" webBrowser.Url = new Uri(strPdfFile);
在表单上放置一个 webBrowser 控件。strPdfFile = "C:\SomeFile.pdf" webBrowser.Url = new Uri(strPdfFile);
This is worked for us until tried on a Win7-64bit system. Need to do some debugging now.
这对我们有用,直到在 Win7-64 位系统上尝试。现在需要做一些调试。