我对使用iTextSharp有点陌生。我有一个PDF文档的存储库,我需要将其复制到图像中(每页一张图像)并对它们进行处理。这些PDF有文字,光栅图像和矢量图像,并可能,更多的东西在其中。我不是很熟悉PDF的结构,我宁愿使用iTextSharp之前,必须购买一些PDF软件包。
我已经完成了使用iTextSharp在C#上从每个PDF文档中提取文本和光栅图像的工作,但是尝试将它们呈现成图像会产生混合的结果,如果有矢量图形,我就无法轻松地提取和呈现它们。
对于我对PDF内部工作和iTextSharp缺乏了解表示歉意,但是是否有一种方法,使用iTextSharp将每个PDF页面绘制到System.Drawing.Image对象上,就像它们在PDF阅读器程序上显示的方式一样?如果有像System.Drawing.Bitmap RenderPage(PdfReader reader, int iPage)这样的方法,那就太好了。
多亏了所有人。任何帮助都将不胜感激。
发布于 2014-07-30 02:07:52
我找到了一种用另一个库来做这件事的方法。我用的是Ghostscript.NET。
Ghostscript.NET是Ghostscript库的本机代码的.NET包装器,因此它可能无法在Windows设备上工作,因为它需要实际的本机代码DLL才能工作。
有关通过Ghostscript.NET包安装NuGet的说明载于本网站:
https://www.nuget.org/packages/Ghostscript.NET/
一旦安装了包,您就需要Ghostscript本机代码DLL。要获得它,首先从下面的链接中安装Ghostscript,然后在安装目录中查找gsdll32.dll并将其复制到一个安全的位置:
http://www.ghostscript.com/download/gsdnld.html
这个DLL是32位的。如果您正在为64位进行编程,则应该下载并安装64位版本。获得DLL后,您可以卸载Ghostscript,因为DLL是独立的。
最后,我编写了以下代码(假设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);
}
}https://stackoverflow.com/questions/25026626
复制相似问题