无法将pdf页面转换为图像

cannot convert pdf page to image

我想将 pdf 文件的每一页转换为新图像。为此,我使用 GhostScript.Net。 问题是我无法弄清楚为什么 System.Drawing.Image pageImage = rasterizer.GetPage(dpi, i); 行中的 pageImage returns 为空。这是我使用的方法:

 public static List<string> GetPDFPageText(Stream pdfStream, string dataPath)
    {

        try
        {
            int dpi = 100;
            GhostscriptVersionInfo lastInstalledVersion =
           GhostscriptVersionInfo.GetLastInstalledVersion(
                   GhostscriptLicense.GPL | GhostscriptLicense.AFPL,
                   GhostscriptLicense.GPL);
            List<string> textParagraphs = new List<string>();

            using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
            {
                rasterizer.Open(pdfStream, lastInstalledVersion,false);

                for (int i = 1; i <= rasterizer.PageCount; i++)
                {
                    // here is the problem, pageImage returns null
                    System.Drawing.Image pageImage = rasterizer.GetPage(dpi, i);

                    // rest of code is unrelated to problem..
                    
                }
            }

            return textParagraphs;
        }
        catch (Exception ex)
        {
            throw new Exception("An error occurred.");
        }
        
    }

函数参数Stream pdfStream来自以下代码:

            using (StreamCollection streamCollection = new StreamCollection())
            {
                FileStream imageStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
                // This is the parameter I used for "Stream pdfStream"
                FileStream pdfStream = new FileStream(pdfPath, FileMode.Open, FileAccess.Read);
                streamCollection.Streams.Add(imageStream);
                streamCollection.Streams.Add(pdfStream);
                PDFHelper.SavePDFByFilesTest(dataPath, streamCollection.Streams,mergedFilePath);
            }

我对 StreamCollection class 的使用已经很满意了,因为我以前在类似的情况下使用过它并且它有效。我验证了文件路径是真实的并且流中有正确的文件。我还尝试使用 MemoryStream 而不是 FileStreamfilename 而不是 stream 只是为了看看问题是否与它们有关。你有什么建议吗?非常感谢。

好的,我明白了为什么它不起作用。我使用最新版本的 Ghostscript (9.56.1) 作为 K J mentioned (thank you for the response) and it uses a new PDF interpreter 作为默认 PDF 解释器。我认为它由于某种原因不能正常工作,因为它是一个真正的新工具,目前可能仍然没有什么问题。我添加了以下行以使用良好的旧 PDF 解释器:

rasterizer.CustomSwitches.Add("-dNEWPDF=false");

还通过以下行定义了生成图像的分辨率:

rasterizer.CustomSwitches.Add("-r300x300");

此外,我将分享 StreamCollection class 的结构,我使用 here 作为实现此 class 的参考。希望对大家有帮助。

public class StreamCollection :  IDisposable
    {
        private bool disposedValue;
        
        public List<Stream> Streams { get; set; }

        public StreamCollection()
        {
            Streams = new List<Stream>();
        }
        
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects)
                    if (this.Streams != null && this.Streams.Count>0)
                    {
                        foreach (var stream in this.Streams)
                        {
                            if (stream != null)
                                stream.Dispose();
                        }
                    }
                }

                // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                // TODO: set large fields to null
                disposedValue = true;
            }
        }

        // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
        // ~StreamCollection()
        // {
        //     // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
        //     Dispose(disposing: false);
        // }

        public void Dispose()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }