如何使用 C# 渲染 pdf
我想使用 C# 以图形方式加载和绘制 pdf 文件。 我不需要编辑它们或任何东西,只需以给定的缩放级别渲染它们即可。
我发现的pdf库似乎专注于生成。 我该怎么做呢?
谢谢。
I want to load and draw pdf files graphically using C#. I don't need to edit them or anything, just render them at a given zoom level.
The pdf libraries I have found seem to be focussed on generation. How do I do this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
Google 开源了其与 Foxit Software 共同编写的优秀 PDF 渲染引擎 - PDFium。
有一个名为 PdfiumViewer 的 C# nuget 包,它为 PDFium 提供了 C# 包装器,并允许显示 PDF打印。
我已经使用过它,并且对渲染的质量印象深刻。
PDFium 直接使用流,因此不需要将任何数据写入磁盘。
这是我在 WinForms 应用程序中的示例
编辑:要在 WinForm 中获取 pdfRenderer 控件:将
PdfiumViewer
NuGet 包添加到项目中; 在 Windows 资源管理器中打开项目包文件夹,然后将PdfiumViewer.dll
文件拖到Toolbox
窗口中; 名为PdfRenderer
的控件将可用于添加:Google has open sourced its excellent PDF rendering engine - PDFium - that it wrote with Foxit Software.
There is a C# nuget package called PdfiumViewer which gives a C# wrapper around PDFium and allows PDFs to be displayed and printed.
I have used it and was very impressed with the quality of the rendering.
PDFium works directly with streams so it doesn't require any data to be written to disk.
This is my example from a WinForms app
Edit: To get the pdfRenderer control in WinForm: Add the
PdfiumViewer
NuGet package to the project; open the projects packages folder in Windows Explorer and drag thePdfiumViewer.dll
file onto theToolbox
window; A control calledPdfRenderer
will be available to add:如果 Adobe ActiveX 不是您想要的,还有其他一些选择(因为 Acrobat 必须存在于用户计算机上,并且您无法自己提供)。
要创建 PDF 预览,请首先查看 StackOverflow 上有关该主题的其他一些讨论:
.NET在winform 最后两点我谈论了一些你可以尝试的事情:
你可以获得一个商业渲染器(PDFViewForNet 、PDFRasterizer.NET、ABCPDF, ActivePDF ,XpdfRasterizer 以及其他答案中的其他内容...)。
不过,大多数都相当昂贵,特别是如果您只关心制作简单的预览/缩略图。
除了 Omar Shahine 的代码片段之外,还有一篇 CodeProject 文章 展示了如何使用 Adobe ActiveX,但它可能已经过时,很容易被新版本破坏,而且其合法性不明确(基本上内部使用是可以的,但你不能发布它,也不能在其他平台上使用它)生成 PDF 图像的服务器)。
您可以查看SumatraPDF的源代码,一个适用于 Windows 的开源 PDF 查看器。
还有 Poppler,一个使用 Poppler 的渲染引擎。 foolabs.com/xpdf/" rel="noreferrer">Xpdf 作为渲染引擎。
所有这些都很棒,但需要相当多的承诺才能使它们工作并与 .Net 交互,并且它们往往在 GPL 下分发。
您可能需要考虑使用 GhostScript 作为解释器,因为渲染页面相当困难过程简单。
缺点是您需要重新打包它才能将其与您的应用程序一起安装,或者将其作为先决条件(或至少是安装过程的一部分)。
这不是一个很大的挑战,而且它肯定比必须让其他渲染引擎与 .Net 合作更容易。
我做了一个小项目,您可以在 Developer Express 论坛 作为附件。
请注意 GhostScript 的许可证要求。
如果您不能离开,那么商业软件可能是您唯一的选择。
There are a few other choices in case the Adobe ActiveX isn't what you're looking for (since Acrobat must be present on the user machine and you can't ship it yourself).
For creating the PDF preview, first have a look at some other discussions on the subject on StackOverflow:
In the last two I talk about a few things you can try:
You can get a commercial renderer (PDFViewForNet, PDFRasterizer.NET, ABCPDF, ActivePDF, XpdfRasterizer and others in the other answers...).
Most are fairly expensive though, especially if all you care about is making a simple preview/thumbnails.
In addition to Omar Shahine's code snippet, there is a CodeProject article that shows how to use the Adobe ActiveX, but it may be out of date, easily broken by new releases and its legality is murky (basically it's ok for internal use but you can't ship it and you can't use it on a server to produce images of PDF).
You could have a look at the source code for SumatraPDF, an OpenSource PDF viewer for windows.
There is also Poppler, a rendering engine that uses Xpdf as 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 and they tend to be be distributed under the GPL.
You may want to consider using GhostScript as 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 cooperating with .Net.
I did a small project that you will find on the Developer Express forums as an attachment.
Be careful of the license requirements for GhostScript through.
If you can't leave with that then commercial software is probably your only choice.
这是我在另一个问题上的回答。
首先,您需要引用 Adobe Reader ActiveX 控件
Adobe Acrobat Browser Control Type Library 1.0
%programfiles&\Common Files\Adobe\Acrobat\ActiveX\AcroPDF.dll
然后您只需将其从工具箱拖到 Windows 窗体中。
并使用类似这样的代码来初始化 ActiveX 控件。
确保当您的窗体关闭时,您将处理掉 ActiveX 控件,
否则 Acrobat 可能会随处可见。
Here is my answer from a different question.
First you need to reference the Adobe Reader ActiveX Control
Adobe Acrobat Browser Control Type Library 1.0
%programfiles&\Common Files\Adobe\Acrobat\ActiveX\AcroPDF.dll
Then you just drag it into your Windows Form from the Toolbox.
And use some code like this to initialize the ActiveX Control.
Make sure when your Form closes that you dispose of the ActiveX Control
otherwise Acrobat might be left lying around.
PdfiumViewer 很棒,但与 System.Drawing 和 WinForms 的耦合相对紧密。 因此,我围绕 PDFium 创建了自己的包装器: PDFiumSharp
页面可以呈现为 PDFiumBitmap 又可以保存到磁盘或公开 作为流。 这样,任何能够从流中加载 BMP 格式图像的框架都可以使用该库来显示 pdf 页面。
例如,在 WPF 应用程序中,您可以使用以下方法来呈现 pdf 页面:
PdfiumViewer is great, but relatively tightly coupled to
System.Drawing
and WinForms. For this reason I created my own wrapper around PDFium: PDFiumSharpPages can be rendered to a PDFiumBitmap which in turn can be saved to disk or exposed as a stream. This way any framework capable of loading an image in BMP format from a stream can use this library to display pdf pages.
For example in a WPF application you could use the following method to render a pdf page:
ABCpdf 将为您完成此操作以及许多其他操作。
它不仅可以将您的 PDF 渲染为各种格式(例如 JPEG、GIF、PNG、TIFF、JPEG 2000;矢量 EPS、SVG、Flash 和 PostScript),而且还可以在各种颜色空间(例如灰色、 RGB、CMYK)和位深度(例如每个组件 1、8、16 位)。
这只是它的一部分功能!
有关更多详细信息,请参阅:
http://www.websupergoo.com/abcpdf-8.htm
哦,您可以通过免费许可证计划获得免费许可证。
使用 Acrobat 进行 PDF 渲染时存在 EULA 问题。 如果您想走这条路,请先仔细检查合法性。
ABCpdf will do this and many other things for you.
Not ony will it render your PDF to a variety of formats (eg JPEG, GIF, PNG, TIFF, JPEG 2000; vector EPS, SVG, Flash and PostScript) but it can also do so in a variety of color spaces (eg Gray, RGB, CMYK) and bit depths (eg 1, 8, 16 bits per component).
And that's just some of what it will do!
For more details see:
http://www.websupergoo.com/abcpdf-8.htm
Oh and you can get free licenses via the free license scheme.
There are EULA issues with using Acrobat to do PDF rendering. If you want to go down this route check the legalities very carefully first.
您可以将 NuGet 包 CefSharp.WinForms 添加到您的应用程序,然后将 ChromiumWebBroweser 控件添加到您的表单。
在代码中你可以这样写:
这是我发现的最简单的解决方案,它完全免费并且独立于用户的计算机设置,就像使用默认的 WebBrowser 控件一样。
You can add a NuGet package CefSharp.WinForms to your application and then add a ChromiumWebBroweser control to your form.
In the code you can write:
This is the easiest solution I have found, it is completely free and independent of the user's computer settings like it would be when using default WebBrowser control.
使用网络浏览器控件。 这需要安装 Adobe reader,但很可能您已经安装了它。 将控件的 URL 设置为文件位置。
Use the web browser control. This requires Adobe reader to be installed but most likely you have it anyway. Set the UrL of the control to the file location.
我使用过的最简单的库是 Paolo Gios 的 库。 基本上
这是一个很好的入门教程。
The easiest lib I have used is Paolo Gios's library. It's basically
This is a great tutorial to get you started.
我想在这里提到 Docotic.Pdf 库。 它可以在 C# 和 VB.NET 中将 PDF 转换为图像。 它还可以渲染和打印 PDF 文档。
该库是 100% 管理的,没有外部依赖。 它不依赖于 System.Drawing.dll 或 GDI+。 您将在 Windows、Linux、macOS、iOS 和 Android 上获得一致的输出。
我是 Docotic.Pdf 的开发人员之一,所以我真的很喜欢它。 请尝试一下,您可能也会喜欢它。
I would like to mention Docotic.Pdf library here. It can convert PDF to image in C# and VB.NET. And it can also render and print PDF documents.
The library is 100% managed without external dependencies. It does not depend on System.Drawing.dll or GDI+. You will get consistent output on Windows, Linux, macOS, iOS, and Android.
I am one of Docotic.Pdf developers, so I really like it. Please try it and you will probably like it, too.
ceTe 软件的动态 PDF 查看器可能会满足您的需求。 我使用过他们的生成器软件并且对此非常满意。
http://www.dynamicpdf.com/
Dynamic PDF Viewer from ceTe software might do what you're looking for. I've used their generator software and was pretty happy with it.
http://www.dynamicpdf.com/
这看起来是正确的:http://code.google.com/p/lib-pdf /
This looks like the right thing: http://code.google.com/p/lib-pdf/
您可以在 google 上搜索 PDF 查看器组件,并获得了多次点击。
不过,如果您确实不需要将它们嵌入到您的应用程序中 - 您只需需要 Acrobat Reader 或 FoxIt(或捆绑它,如果它符合各自的许可条款)和 shell 到它。 它不是那么酷,但它可以免费完成工作。
You could google for PDF viewer component, and come up with more than a few hits.
If you don't really need to embed them in your app, though - you can just require Acrobat Reader or FoxIt (or bundle it, if it meets their respective licensing terms) and shell out to it. It's not as cool, but it gets the job done for free.