Convert PDF to Image Without Using Ghostscript Dll

Convert PDF to Image without using Ghostscript DLL

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

How to use Ghostscript for converting PDF to Image

You can use C# to run the GhostScript command line or use Platform Invoke (pInvoke) calls to call the GhostScript dll directly.

GhostScript is primarily file based, so the input is path to a file on disk and the output is the creation of files on disk. The parameters used to call either the dll or exe are basically the same, so there is not a huge benefit to calling the dll directly, but does make for nicer code.

I have C# wrapper that can be used to call the ghostscript dll, if you email me (address on profile) I will sent it to you.

HTH

UPDATE:

code repo moved to https://bitbucket.org/brightertools/ghostscript

PDF to Image using GhostScript. No image file has to be created

Yes, but you will need to interface directly to Ghostscript using the Ghostscript DLL (I'm assuming Windows since you mention C#).

The simplest solution is probably to use the display device which sends an in-memory bitmap back to the parent application, the default GS application then creates a window and a device context, and draws the bitmap in it.

You should be able to use the GS application as a starting point to see how this is done, and you don't need to create a device of your own which means you don't need to recompile the Ghostscript binary.

How to convert PDF files to images

The thread "converting PDF file to a JPEG image" is suitable for your request.

One solution is to use a third-party library. ImageMagick is a very popular and is freely available too. You can get a .NET wrapper for it here. The original ImageMagick download page is here.

  • Convert PDF pages to image files using the Solid Framework Convert PDF pages to image files using the Solid Framework (dead link, the deleted document is available on Internet Archive).
  • Convert PDF to JPG Universal Document Converter
  • 6 Ways to Convert a PDF to a JPG Image

And you also can take a look at the thread
"How to open a page from a pdf file in pictureBox in C#".

If you use this process to convert a PDF to tiff, you can use this class to retrieve the bitmap from TIFF.

public class TiffImage
{
private string myPath;
private Guid myGuid;
private FrameDimension myDimension;
public ArrayList myImages = new ArrayList();
private int myPageCount;
private Bitmap myBMP;

public TiffImage(string path)
{
MemoryStream ms;
Image myImage;

myPath = path;
FileStream fs = new FileStream(myPath, FileMode.Open);
myImage = Image.FromStream(fs);
myGuid = myImage.FrameDimensionsList[0];
myDimension = new FrameDimension(myGuid);
myPageCount = myImage.GetFrameCount(myDimension);
for (int i = 0; i < myPageCount; i++)
{
ms = new MemoryStream();
myImage.SelectActiveFrame(myDimension, i);
myImage.Save(ms, ImageFormat.Bmp);
myBMP = new Bitmap(ms);
myImages.Add(myBMP);
ms.Close();
}
fs.Close();
}
}

Use it like so:

private void button1_Click(object sender, EventArgs e)
{
TiffImage myTiff = new TiffImage("D:\\Some.tif");
//imageBox is a PictureBox control, and the [] operators pass back
//the Bitmap stored at that position in the myImages ArrayList in the TiffImage
this.pictureBox1.Image = (Bitmap)myTiff.myImages[0];
this.pictureBox2.Image = (Bitmap)myTiff.myImages[1];
this.pictureBox3.Image = (Bitmap)myTiff.myImages[2];
}

Convert pdf into images using C# - Ghostscript

Many thanks to KenS and all the others.

My problem was that I defined -dNOPAUSE as my first argument. However, the first argument shouldn't be defined for ghostscript. It is not realized from ghostscript, so that's why I got only the first page. Thanks to KenS suggestion with -dNOPAUSE, I was able to find the error.

I hope this helps others, too.

Converting pdf to image using c# and Ghostscript

Most likely this is caused by a missing font or CIDFont, the rectangles are the .notdef glyph which is used when a glyph cannot be found. Of course, its not possible to tell without seeing the original PDF file.

However if you check the Ghostscript back channel (and no I cannot tell you how to do this with Ghostscript.NET as that is not an Artifex product) you will probably see warnings about missing glyphs.

I can look further but only if you make the PDF file available. It would also be helpful to know what version of Ghostscript you are using.

MemoryStream (pdf) to Ghostscript to MemoryStream (jpg)

The thing is that the PDF language, unlike the PostScript language, inherently requires random access to the file. If you provide PDF directly to Standard Input or via PIPE, Ghostscript will copy it to a temporary file before interpreting the PDF. So, there is no point of passing PDF as MemoryStream (or byte array) as it will anyway end up on the disk before it is interpreted.

Take a look at the Ghostscript.NET and it's GhostscriptRasterizer sample for the 'in-memory' output.



Related Topics



Leave a reply



Submit