I had just done this for a batching OCR application and found that using Ghostscript.NET (
see here)worked great and was reliable. The biggest issue I found was that just because the development station has Ghostscript installed, does not mean the system running the routine does. In this case, its best to provide a means to initialize Ghostscript in memory using a specified gsdll32.dll file path. This is slightly modified version of my working code. The only difference is that we do not provide and input path, we provide an object that has that data in it.
using Ghostscript.NET;
static GhostscriptVersionInfo GetVersionInfo(string dllPath)
{
GhostscriptVersionInfo gsVersion = null;
try { gsVersion = GhostscriptVersionInfo.GetLastInstalledVersion(GhostscriptLicense.GPL | GhostscriptLicense.AFPL, GhostscriptLicense.GPL); }
catch { }
if (gsVersion == null)
{
try { gsVersion = new GhostscriptVersionInfo(new Version(0, 0, 0), dllPath, string.Empty, GhostscriptLicense.GPL); }
catch { }
}
return gsVersion;
}
static void ExtractPagesAsPngs(GhostscriptVersionInfo gsVersion, string scrFilePath, string destFilePath, int[] sheetsToExtract, int[] allowedDpiValues)
{
if ((allowedDpiValues == null) || (allowedDpiValues.Length < 1)) { allowedDpiValues = new int[] {300, 256, 150, 128, 96, 72, 64, 32, 16, 8}; }
var gsDev = new GhostscriptPngDevice(GhostscriptPngDeviceType.Png256);
gsDev.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
gsDev.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
gsDev.CustomSwitches.Add("-dDOINTERPOLATE");
for (var i = 0; i < sheetsToExtract.Length; i++)
{
gsDev.InputFiles.Add(scrFilePath);
gsDev.Pdf.FirstPage = sheetsToExtract[i];
gsDev.Pdf.LastPage = sheetsToExtract[i];
if (sheetsToExtract.Length == 1) { gsDev.OutputPath = destFilePath; }
else { gsDev.OutputPath = destFilePath.Replace(".png", " (" + sheetsToExtract[i].ToString() + ").png"); }
for (var j = 0; j < allowedDpiValues.Length; j++)
{
if (allowedDpiValues[j] < 1) { allowedDpiValues[j] = 1; }
}
var dpiToTry = allowedDpiValues.Distinct().OrderBy(x => x).ToArray();
int dpiIdx = 0;
bool extracted = false;
while (!extracted && (dpiIdx < dpiToTry.Length))
{
gsDev.ResolutionXY = new GhostscriptImageDeviceResolution(dpiToTry[dpiIdx], dpiToTry[dpiIdx]);
try { gsDev.Process(gsVersion, true, null); }
catch { }
if (File.Exists(gsDev.OutputPath)) { extracted = true; }
else { dpiIdx++; }
}
}
}