C# 不使用 Ghostscript DLL 将 PDF 转换为图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12831742/
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
Convert PDF to Image without using Ghostscript DLL
提问by Deepak Jena
Is there any way, I can convert HTML Document (file not URL) to Image, or PDF to image?
有什么办法可以将 HTML 文档(文件不是 URL)转换为图像,或将 PDF 转换为图像?
I am able to do the above using Ghostscript DLL , Is there any other way , I can do it, without using the Ghostscript DLL?
我可以使用 Ghostscript DLL 执行上述操作,还有其他方法吗,我可以在不使用 Ghostscript DLL 的情况下做到这一点?
I am developing a C# Windows Application.
我正在开发一个 C# Windows 应用程序。
采纳答案by Furqan Safdar
Use LibPdf, for PDF to Image conversion
使用LibPdf,用于 PDF 到图像的转换
LibPdflibrary converts converts PDF file to an image. Supported image formats are PNG and BMP, but you can easily add more.
LibPdf库将 PDF 文件转换为图像。支持的图像格式为 PNG 和 BMP,但您可以轻松添加更多格式。
Usage example:
用法示例:
using (FileStream file = File.OpenRead(@"..\path\to\pdf\file.pdf")) // in file
{
var bytes = new byte[file.Length];
file.Read(bytes, 0, bytes.Length);
using (var pdf = new LibPdf(bytes))
{
byte[] pngBytes = pdf.GetImage(0,ImageType.PNG); // image type
using (var outFile = File.Create(@"..\path\to\pdf\file.png")) // out file
{
outFile.Write(pngBytes, 0, pngBytes.Length);
}
}
}
ImageMagick, you should also look at this freely available and powerful tool. It's capable of doing what you want and also provides some .NET bindings (as well as bindings to several other languages).
ImageMagick,您还应该看看这个免费可用且功能强大的工具。它能够做你想做的事情,还提供一些 .NET 绑定(以及与其他几种语言的绑定)。
In its simplest form, it's just like writing a command
最简单的形式,就像写一个命令
convert file.pdf imagefile.png
回答by Furqan Safdar
You can use below any one library for PDF to Image conversion
您可以使用下面的任何一个库进行 PDF 到图像的转换
Use Aspose.pdflink below: http://www.aspose.com/docs/display/pdfnet/Convert+all+PDF+pages+to+JPEG+Images
使用 下面的Aspose.pdf链接:http: //www.aspose.com/docs/display/pdfnet/Convert+all+PDF+pages+to+JPEG+Images
code sample:
代码示例:
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(MyPdfPath));
using (FileStream imageStream = new FileStream(MyOutputImage.png, FileMode.Create))
{
Resolution resolution = new Resolution(300);
PngDevice pngDevice = new PngDevice(resolution);
pngDevice.Process(pdfDocument.Pages[PageNo], MyOutputImage);
imageStream.Close();
}
Use Bytescout PDF Rendererlink below: http://bytescout.com/products/developer/pdfrenderersdk/convert-pdf-to-png-basic-examples
使用 下面的Bytescout PDF 渲染器链接:http: //bytescout.com/products/developer/pdfrenderersdk/convert-pdf-to-png-basic-examples
code sample :
代码示例:
MemoryStream ImageStream = new MemoryStream();
RasterRenderer renderer = new RasterRenderer();
renderer.RegistrationName = "demo";
renderer.RegistrationKey = "demo";
// Load PDF document.
renderer.LoadDocumentFromFile(FilePath);
for (int i = 0; i < renderer.GetPageCount(); i++)
{
// Render first page of the document to PNG image file.
renderer.RenderPageToStream(i, RasterOutputFormat.PNG, ImageStream);
}
Image im = Image.FromStream(ImageStream);
im.Save("MyOutputImage.png");
ImageStream.Close();
回答by Dragana Le Mitova
In case someone wants to use Ghostscript.NET.
如果有人想使用Ghostscript.NET。
Ghostscript.NET - (written in C#) is the most completed managed wrapper library around the Ghostscript library (32-bit & 64-bit), an interpreter for the PostScript language, PDF.
Ghostscript.NET -(用 C# 编写)是 Ghostscript 库(32 位和 64 位)周围最完整的托管包装库,它是 PostScript 语言 PDF 的解释器。
It is dependent on executable file you have to install on your machine. Here is a link from where you can see and download the latest version of the exe.
它取决于您必须在机器上安装的可执行文件。这是一个链接,您可以从中查看和下载最新版本的 exe。
P.S. I had some troubles with the latest version 9.50 not being able to count the pages.
PS 我在最新版本 9.50 无法计算页数时遇到了一些麻烦。
I prefer using the 9.26 version.
我更喜欢使用 9.26 版本。
https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs926/gs926aw32.exe
https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs926/gs926aw64.exe
https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs926/gs926aw32.exe
https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs926/gs926aw64.exe
Next step is to find and install Ghostscript.NET from Nuget. I download the PDF from CDN url and use the MemoryStreamto open and process the PDF file. Here is a sample code:
下一步是从 Nuget 查找并安装 Ghostscript.NET。我从 CDN url 下载 PDF 并使用MemoryStream打开和处理 PDF 文件。这是一个示例代码:
using (WebClient myWebClient = new WebClient())
{
using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
{
/* custom switches can be added before the file is opened
rasterizer.CustomSwitches.Add("-dPrinted");
*/
byte[] buffer = myWebClient.DownloadData(pdfUrl);
using (var ms = new MemoryStream(buffer))
{
rasterizer.Open(ms);
var image = rasterizer.GetPage(0, 0, 1);
var imageURL = "MyCDNpath/Images/" + filename + ".png";
_ = UploadFileToS3(image, imageURL);
}
}
}
You can also use it with temporary FileStream. Here is another example. Note that the File is temporary and has DeleteOnClose mark.
您还可以将它与临时 FileStream 一起使用。这是另一个例子。请注意,该文件是临时的并且具有 DeleteOnClose 标记。
using (WebClient myWebClient = new WebClient())
{
using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
{
/* custom switches can be added before the file is opened
rasterizer.CustomSwitches.Add("-dPrinted");
*/
byte[] buffer = myWebClient.DownloadData(pdfUrl);
int bufferSize = 4096;
using (var fileStream = System.IO.File.Create("TempPDFolder/" + pdfName, bufferSize, System.IO.FileOptions.DeleteOnClose))
{
// now use that fileStream to save the pdf stream
fileStream.Write(buffer, 0, buffer.Length);
rasterizer.Open(fileStream);
var image = rasterizer.GetPage(0, 0, 1);
var imageURL = "MyCDNpath/Images/" + filename + ".png";
_ = UploadFileToS3(image, imageURL);
}
}
}
Hope it will help someone struggling to get high quality images from pdf for free.
希望它能帮助那些努力从 pdf 免费获取高质量图像的人。
回答by Mohammad Hassani
the best and free nuget package that you can save every page of your Pdf to png and with custom resilution Docnet.corethis can be use in the .net core project.
最好和免费的 nuget 包,您可以将 Pdf 的每一页保存为 png,并且使用自定义分辨率Docnet.core这可以在 .net 核心项目中使用。
they have github and nice examples but here i want to add my code for reading en pdf with more that one page
他们有 github 和不错的例子,但在这里我想添加我的代码,用于阅读更多一页的 en pdf
string webRootPath = _hostingEnvironment.WebRootPath;
string fullPath = webRootPath + "/uploads/user-manual/file.pdf";
string fullPaths = webRootPath + "/uploads/user-manual";
using (var library = DocLib.Instance)
{
using (var docReader = library.GetDocReader(fullPath, 1080, 1920))
{
for (int i = 1; i < docReader.GetPageCount(); i++)
{
using (var pageReader = docReader.GetPageReader(i))
{
var bytes = EmailTemplates.GetModifiedImage(pageReader);
System.IO.File.WriteAllBytes(fullPaths+"/page_image_" +i+".png", bytes);
}
}
}
}
Other functions that u used in this function are in there githubs.
你在这个函数中使用的其他函数在 githubs 中。

