C# GhostscriptRasterizer.PageCount始终返回零

C# GhostscriptRasterizer.PageCount始终返回零,c#,ghostscript.net,C#,Ghostscript.net,这个问题已经在这里讨论过了: 但是这个问题的答案并没有帮助我解决这个问题 在我的例子中,从kat到旧版本的Ghostscript没有任何帮助。26和25。我的PageCount始终为0,如果版本低于27,则会出现错误“未找到本机Ghostscript库” 我也遇到了同样的问题,最后用它来计算页数。下面是生产代码的一个片段: using (var reader = new PdfReader(pdfFile)) { // as a matter of fact we need iText

这个问题已经在这里讨论过了: 但是这个问题的答案并没有帮助我解决这个问题

在我的例子中,从kat到旧版本的Ghostscript没有任何帮助。26和25。我的PageCount始终为0,如果版本低于27,则会出现错误“未找到本机Ghostscript库”


我也遇到了同样的问题,最后用它来计算页数。下面是生产代码的一个片段:

using (var reader = new PdfReader(pdfFile))
{
    //  as a matter of fact we need iTextSharp PdfReader (and all of iTextSharp) only to get the page count of PDF document;
    //  unfortunately GhostScript itself doesn't know how to do it
    pageCount = reader.NumberOfPages;
}
这不是一个完美的解决方案,但正是它解决了我的问题。我留下这条评论是为了提醒自己,我必须找到一个更好的方法,但我从来没有费心回来,因为它只是工作得很好,因为它是

PdfReader
类在
iTextSharp.text.pdf
命名空间中定义

我正在使用
Ghostscript.NET.GhostscriptPngDevice
而不是
GhostscriptRasterizer
对PDF文档的特定页面进行光栅化

这里是我的方法,光栅化页面并将其保存到PNG文件

private static void PdfToPngWithGhostscriptPngDevice(string srcFile, int pageNo, int dpiX, int dpiY, string tgtFile)
{
    GhostscriptPngDevice dev = new GhostscriptPngDevice(GhostscriptPngDeviceType.PngGray);
    dev.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
    dev.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
    dev.ResolutionXY = new GhostscriptImageDeviceResolution(dpiX, dpiY);
    dev.InputFiles.Add(srcFile);
    dev.Pdf.FirstPage = pageNo;
    dev.Pdf.LastPage = pageNo;
    dev.CustomSwitches.Add("-dDOINTERPOLATE");
    dev.OutputPath = tgtFile;
    dev.Process();
}

希望这会有帮助……

事实是,当我自己指定pageNumber=1时,GetPage(xDpi、yDpi、pageNumber)方法不仅不能确定我的页数,而且对我来说也很有效。很抱歉,我不太确定我是否完全理解您。您能澄清一下吗?您是否对代码行
var pdf2PNG=rasterizer.GetPage(xDpi、yDpi、pageNumber)进行了编码是否使用
页码==1
?好吧,显然我误解了你最初的问题。我认为您的问题是您总是从rasterizer.PageCount中获取0,如参考主题()中所述。我刚刚检查了我的代码,似乎我在使用
rasterizer.GetPage时遇到了问题(xDpi,yDpi,pageNumber
调用。可能这就是我最终使用
Ghostscript.NET.GhostscriptPngDevice
而不是
GhostscriptRasterizer
的原因。我将更新我的答案,并在其中包含名为
PdfToPngWithGhostscriptPngDevice
的方法,它显然正是您想要实现的希望这会有所帮助…Alexander,我如何使用GhostscriptPngDevice将pdf文件的一页转换为内存,即不将生成的png文件保存到磁盘,例如,获取字节数组?
private static void PdfToPngWithGhostscriptPngDevice(string srcFile, int pageNo, int dpiX, int dpiY, string tgtFile)
{
    GhostscriptPngDevice dev = new GhostscriptPngDevice(GhostscriptPngDeviceType.PngGray);
    dev.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
    dev.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
    dev.ResolutionXY = new GhostscriptImageDeviceResolution(dpiX, dpiY);
    dev.InputFiles.Add(srcFile);
    dev.Pdf.FirstPage = pageNo;
    dev.Pdf.LastPage = pageNo;
    dev.CustomSwitches.Add("-dDOINTERPOLATE");
    dev.OutputPath = tgtFile;
    dev.Process();
}