简体   繁体   English

无法将 pdf 页面转换为图像

[英]cannot convert pdf page to image

I want to convert a pdf file's each page to a new image.我想将 pdf 文件的每一页转换为新图像。 To do this, i use GhostScript.Net.为此,我使用 GhostScript.Net。 The problem is i can't figure out why pageImage returns null in the System.Drawing.Image pageImage = rasterizer.GetPage(dpi, i);问题是我无法弄清楚为什么 pageImage 在System.Drawing.Image pageImage = rasterizer.GetPage(dpi, i);中返回 null line.线。 Here is the method i use:这是我使用的方法:

 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.");
        }
        
    }

Function parameter Stream pdfStream comes from the below code:函数参数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);
            }

I am already comfortable with the use of StreamCollection class because i used it before in a similar situation and it worked.我已经对StreamCollection类的使用感到满意,因为我之前在类似的情况下使用过它并且它有效。 I verified that the filepath is true and stream has the file correctly.我验证了文件路径是正确的,并且流具有正确的文件。 Also i tried using MemoryStream instead of FileStream and filename instead of stream just to see if the problem is related to them or not.我也尝试使用MemoryStream而不是FileStreamfilename而不是stream只是为了查看问题是否与它们有关。 Is there any suggestion you could suggest?你有什么建议可以提出吗? I would really appreciate that.我真的很感激。

Okay, i figured out why it didn't work.好吧,我知道为什么它不起作用了。 I use the latest version of Ghostscript (9.56.1) as KJ mentioned (thank you for the response) and it uses a new PDF interpreter as default PDF interpreter.我使用KJ提到的最新版本的 Ghostscript (9.56.1)(感谢您的回复),它使用新的 PDF 解释器作为默认 PDF 解释器。 I assume it didn't work properly for some reason because it is a really new tool and still may have little problems for now.我认为由于某种原因它不能正常工作,因为它是一个非常新的工具,现在可能仍然没有什么问题。 I added the following line to use good old PDF interpreter:我添加了以下行以使用良好的旧 PDF 解释器:

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

Also defined resolution for produced image by following line:还通过以下行定义了生成图像的分辨率:

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

Furthermore, i will share the structure of StreamCollection class, I used here as reference to implement this class.此外,我将分享StreamCollection类的结构,我在这里用作实现该类的参考。 Hope it helps someone.希望它可以帮助某人。

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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM