4

I am attempting to convert a pdf document to images using ghostscript. The desired dpi is set to 72px which should be high enough for text to display clear but most of the text is illegible.

I can raise the dpi but that will cause very large image files which I would prefer not to have.

I know there are arguments for ghostscript to add anti aliasing etc (e.g. -dDOINTERPOLATE). How do I add them to the following piece of code, or is there a better way to do this?

        int desired_x_dpi = 72;
        int desired_y_dpi = 72;

        GhostscriptRasterizer _rasterizer = new GhostscriptRasterizer();

        _rasterizer.Open(inputPdfPath, localDllInfo, false);

        for (int pageNumber = 1; pageNumber <= _rasterizer.PageCount; pageNumber++)
        {
            string pageFilePath = Path.Combine(outputPath, "Page-" + pageNumber.ToString() + ".png");

            Image img = _rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
            img.Save(pageFilePath, ImageFormat.Png);
        }
HABJAN
  • 9,212
  • 3
  • 35
  • 59
Morne
  • 1,623
  • 2
  • 18
  • 33

2 Answers2

5

In 1.1.9 the GhostscriptRasterizer has -dDOINTERPOLATE set by default. The only parameters you can control via GhostscriptRasterizer class are TextAlphaBits and GraphicsAlphaBits.

I would recommend you to try to use other classes from the Ghostscript.NET if you want more control over the parameters.

Take a look at this samples: Image devices usage samples

You can add custom parameters (switches) this way:

GhostscriptPngDevice dev = new GhostscriptPngDevice(GhostscriptPngDeviceType.Png16m);
dev.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
dev.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
dev.ResolutionXY = new GhostscriptImageDeviceResolution(96, 96);
dev.InputFiles.Add(@"E:\gss_test\indispensable.pdf");
dev.Pdf.FirstPage = 2;
dev.Pdf.LastPage = 4;
dev.CustomSwitches.Add("-dDOINTERPOLATE"); // custom parameter
dev.OutputPath = @"E:\gss_test\output\indispensable_color_page_%03d.png";
dev.Process();

When I catch some time, I will extend GhostscriptRasterizer to accept custom parameters in the Open method for the Ghostscript.NET v.1.2.0 release.

HABJAN
  • 9,212
  • 3
  • 35
  • 59
  • One more thing, if you have a local dll, you can pass your "localDllInfo" to the dev.Process(localDllInfo,....) method. – HABJAN Aug 22 '14 at 09:00
0

Got same problem. Fixed by adding CustomSwitches with resolution to GhostscriptRasterizer:

using (var rasterizer = new GhostscriptRasterizer())
{
    rasterizer.CustomSwitches.Add("-r500x500");
    ...other code here
}
Nick Suslov
  • 124
  • 1
  • 12