简体   繁体   English

如何使用iTextSharp将PDF页面绘制到System.Drawing.Image中?

[英]How to draw a PDF page into a System.Drawing.Image using iTextSharp?

I am kind of new to using iTextSharp. 我是使用iTextSharp的新手。 I have a repository of PDF documents at work that I need to copy into images (one image per page) and process them. 我有一个工作中的PDF文档存储库,需要将其复制到图像(每页一张图像)并进行处理。 These PDF have text, raster images and vector images, and possibly, more stuff in them. 这些PDF具有文本,光栅图像和矢量图像,并且可能包含更多内容。 I am not very familiar with PDF structure, and I would rather use iTextSharp before having to buy some PDF package. 我对PDF的结构不是很熟悉,我宁愿在购买一些PDF包之前使用iTextSharp。

I have done work on extracting text and raster images from each PDF document using iTextSharp on C#, but trying to render them into an image gives mixed results, and if there are vector graphics, there is nothing I can do to extract and render them easily. 我已经完成了在C#上使用iTextSharp从每个PDF文档中提取文本和光栅图像的工作,但是尝试将它们渲染为图像会产生混合的结果,而且如果有矢量图形,我将无能为力地轻松提取和渲染它们。

I apologize for my lack of knowledge on PDF internal works and iTextSharp, but is there a way, using iTextSharp, to draw each PDF page to a System.Drawing.Image object in the same way as they appear, say, on a PDF reader program? 对于缺乏对PDF内部作品和iTextSharp的了解,我深表歉意,但是有一种方法可以使用iTextSharp将每个PDF页面绘制到System.Drawing.Image对象上,就像在PDF阅读器上一样程序? It would be great if there was a method such as System.Drawing.Bitmap RenderPage(PdfReader reader, int iPage) . 如果有诸如System.Drawing.Bitmap RenderPage(PdfReader reader, int iPage)类的方法,那将是很好的。

Thanks to all. 谢谢大家。 Any help will be appreciated. 任何帮助将不胜感激。

I found a way to do it using another library. 我找到了使用另一个库的方法。 I used Ghostscript.NET. 我使用了Ghostscript.NET。

Ghostscript.NET is a .NET wrapper for Ghostscript library's native code, so, it probably won't work on Windows RT devices because it requires the actual native code DLL to work. Ghostscript.NET是Ghostscript库本机代码的.NET包装,因此,它可能无法在Windows RT设备上运行,因为它需要实际的本机代码DLL才能工作。

Instructions to install Ghostscript.NET via NuGet package are in this website: 通过NuGet软件包安装Ghostscript.NET的说明在此网站中:

https://www.nuget.org/packages/Ghostscript.NET/ https://www.nuget.org/packages/Ghostscript.NET/

Once the package is installed, you need the Ghostscript native code DLL. 安装软件包后,您需要Ghostscript本机代码DLL。 To obtain it, install Ghostscript first from the link bellow, then, find gsdll32.dll in the installation directory and copy it in a safe place: 要获取它,请首先从下面的链接安装Ghostscript,然后在安装目录中找到gsdll32.dll并将其复制到一个安全的地方:

http://www.ghostscript.com/download/gsdnld.html http://www.ghostscript.com/download/gsdnld.html

This DLL is for 32 bits. 这个DLL是32位的。 If you are programming for 64 bits, you should download and install the 64 bits version. 如果要使用64位编程,则应下载并安装64位版本。 After obtaining the DLL, you can uninstall Ghostscript since the DLL is stand alone. 获取DLL后,由于DLL是独立的,因此可以卸载Ghostscript。

Finally, I wrote the following code (assumes that Ghostscript native DLL is on the same path as the application) to render a PDF's pages into System.Drawing.Images: 最后,我编写了以下代码(假设Ghostscript本机DLL与应用程序位于同一路径上),以将PDF的页面呈现到System.Drawing.Images中:

string sDLLPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),
    "gsdll32.dll");
GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(sDLLPath);
using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
{
    rasterizer.Open("sample.pdf", gvi, false);

    int dpi_x = 96;
    int dpi_y = 96;
    for (int i = 1; i <= rasterizer.PageCount; i++)
    {
        Image img = rasterizer.GetPage(dpi_x, dpi_y, i);
        // System.Drawing.Image obtained. Now it can be used at will.
        // Simply save it to storage as an example.
        img.Save(Path.Combine("C:\\Temp", "page_" + i + ".png")),
            System.Drawing.Imaging.ImageFormat.Png);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM