简体   繁体   English

MemoryStream(pdf)到Ghostscript到MemoryStream(jpg)

[英]MemoryStream (pdf) to Ghostscript to MemoryStream (jpg)

I did see " PDF to Image using GhostScript. No image file has to be created ", but that only (sort of) answered half my question. 我确实看到“ 使用GhostScript的PDF到图像。不需要创建图像文件 ”,但只有(有点)回答了我的一半问题。 Is it possible to use GhostScriptSharp (or the regular GhostScript dll) to convert a pdf in a MemoryStream to a jpg in a MemoryStream? 是否可以使用GhostScriptSharp(或常规GhostScript DLL)将MemoryStream中的pdf转换为MemoryStream中的jpg? I speak of a dynamically filled in pdf form with iTextSharp which I am already directing to a MemoryStream to save to a database or stream to a http response, and I'd really love to avoid saving to a file (and subsequent cleanup) if I can. 我说的是用iTextSharp动态填写的pdf表单,我已经指向MemoryStream保存到数据库或流式传输到http响应,我真的很想避免保存到文件(以及随后的清理)如果我能够。

The sole answer in the answer I referenced claimed that one has to go down to the GhostScript dll to do the latter part, but it was obvious I would need to do a good bit of leg-work to figure out what that meant. 在我引用的答案中唯一的答案声称,必须要使用GhostScript dll来完成后一部分,但很明显我需要做一些好的腿部工作来弄清楚这意味着什么。 Does anyone have a good resource that could help me on this journey? 有没有人有这么好的资源可以帮助我完成这个旅程?

The thing is that the PDF language, unlike the PostScript language, inherently requires random access to the file. 问题在于,与PostScript语言不同,PDF语言本身就需要随机访问文件。 If you provide PDF directly to Standard Input or via PIPE, Ghostscript will copy it to a temporary file before interpreting the PDF. 如果您直接向标准输入或PIPE提供PDF,Ghostscript会在解释PDF之前将其复制到临时文件中。 So, there is no point of passing PDF as MemoryStream (or byte array) as it will anyway end up on the disk before it is interpreted. 因此,没有必要将PDF作为MemoryStream(或字节数组)传递,因为它在解释之前最终会在磁盘上结束。

Take a look at the Ghostscript.NET and it's GhostscriptRasterizer sample for the 'in-memory' output. 看一下Ghostscript.NET和它的GhostscriptRasterizer样本,用于'内存中'输出。

Ghostscript.Net is a wrapper to the Ghostscript dll. Ghostscript.Net是Ghostscript dll的包装器。 It now can take a stream object and can return an image that can be saved to an stream. 它现在可以采用流对象,并可以返回可以保存到流中的图像。 Here is an example that I used on as ASP page to generate PDF's from a memory stream. 这是我用作ASP页面从内存流生成PDF的示例。 I haven't completely figured out the best way to handle the ghostscript dll and where to locate it on the server. 我还没有完全找到处理ghostscript dll的最佳方法以及在服务器上找到它的位置。

 void PDFToImage(MemoryStream inputMS, int dpi)
    {
        GhostscriptRasterizer rasterizer = null;
        GhostscriptVersionInfo version = new GhostscriptVersionInfo(
                                                                new Version(0, 0, 0), @"C:\PathToDll\gsdll32.dll", 
                                                                string.Empty, GhostscriptLicense.GPL);

        using (rasterizer = new GhostscriptRasterizer())
        {
            rasterizer.Open(inputMS, version, false);

            for (int i = 1; i <= rasterizer.PageCount; i++)
            {

                using (MemoryStream ms = new MemoryStream())
                {
                    Image img = rasterizer.GetPage(dpi, dpi, i);
                    img.Save(ms, ImageFormat.Jpeg);
                    ms.Close();

                    AspImage newPage = new AspImage();
                    newPage.ImageUrl = "data:image/png;base64," + Convert.ToBase64String((byte[])ms.ToArray());

                    Document1Image.Controls.Add(newPage);
                }

            }

            rasterizer.Close();
        }
    }

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

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