Convert PDF to Image without using Ghostscript DLL

2020-01-27 03:31发布

Is there any way, I can convert HTML Document (file not URL) to Image, or PDF to image?

I am able to do the above using Ghostscript DLL , Is there any other way , I can do it, without using the Ghostscript DLL?

I am developing a C# Windows Application.

标签: c#
3条回答
叼着烟拽天下
2楼-- · 2020-01-27 03:52

Use LibPdf, for PDF to Image conversion

LibPdf library converts converts PDF file to an image. Supported image formats are PNG and BMP, but you can easily add more.

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).

In its simplest form, it's just like writing a command

convert file.pdf imagefile.png
查看更多
Lonely孤独者°
3楼-- · 2020-01-27 04:12

You can use below any one library for PDF to Image conversion

Use Aspose.pdf link below: 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 Renderer link below: 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();
查看更多
叛逆
4楼-- · 2020-01-27 04:12

In case someone wants to use 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.

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.

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

P.S. I had some troubles with the latest version 9.50 not being able to count the pages.

I prefer using the 9.26 version.

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 MemoryStream to open and process the PDF file. Here is a sample code:

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.

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.

查看更多
登录 后发表回答