0

我正在尝试使用ghostscript.net(1.2.1.0)将pdf转换为图像,gs版本是9.22 x86。

我的代码:

using (_rasterizer = new GhostscriptRasterizer())
{
    _rasterizer.Open(inputPdfPath, _lastInstalledVersion, false);

    //_rasterizer.CustomSwitches.Add("-sDEVICE=pngalpha");
    //_rasterizer.CustomSwitches.Add("-dTextAlphaBits=4");
    //_rasterizer.CustomSwitches.Add("-dGraphicsAlphaBits=4");

    for (int pageNumber = 1; pageNumber <= _rasterizer.PageCount; pageNumber++)
    {
        var desiredDPI = 102;
        using (System.Drawing.Image img = _rasterizer.GetPage(desiredDPI, desiredDPI, pageNumber))
        {
            img.Save(pageNumber + ".png", ImageFormat.Png);
        }
    }
}

它适用于某些页面,但对于某些图像,它会创建黑色边距和黑色背景。

示例文件: pdf => png

我用gs命令测试,没问题。我尝试了以下代码。图像很好,但文字质量很差。

public Image getImg(string inputFile, int pageNO, int resolution)
{
    GhostscriptPngDevice dev = new GhostscriptPngDevice(GhostscriptPngDeviceType.PngAlpha);
    dev.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
    dev.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
    dev.ResolutionXY = new GhostscriptImageDeviceResolution(resolution, resolution);
    dev.InputFiles.Add(inputFile);
    dev.Pdf.FirstPage = pageNO;
    dev.Pdf.LastPage = pageNO;
    dev.CustomSwitches.Add("-dDOINTERPOLATE");
    dev.OutputPath = pageNO + ".png";
    dev.Process();

    return Image.FromFile(pageNO + ".png");
}
4

1 回答 1

1
 GhostscriptPngDevice dev = new GhostscriptPngDevice(GhostscriptPngDeviceType.Png16m);
        dev.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
        dev.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
        dev.BackgroundColor = Color.White;
        dev.ResolutionXY = new GhostscriptImageDeviceResolution(desired_x_dpi, desired_y_dpi);
        dev.InputFiles.Add(inputPathAndFile);
        dev.Pdf.FirstPage = 1;
        dev.Pdf.LastPage = 1;
        dev.CustomSwitches.Add("-dDOINTERPOLATE");
        dev.OutputPath = outputPathAndFile;
        dev.Process();
于 2018-10-15T15:37:01.470 回答