MemoryStream (pdf) to Ghostscript to MemoryStream (jpg)
Asked Answered
L

2

7

I did see "PDF to Image using GhostScript. No image file has to be created", but that only (sort of) answered half my question. Is it possible to use GhostScriptSharp (or the regular GhostScript dll) to convert a pdf in a MemoryStream to a jpg in a MemoryStream? 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.

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. Does anyone have a good resource that could help me on this journey?

Languet answered 3/1, 2014 at 22:5 Comment(7)
Try printing pdf to image. I wish to give an answer, but I was working with PDF quite a bit, even dynamically filling with images, but since each operation is quite of coding, giving an answer here may take not only coding effort but understanding where you are with PDF. " speak of a dynamically filled in pdf form with iTextSharp" did you flatten the PDF? even this may change the solution...Fondness
Try #10125617Fondness
"did you flatten the PDF" Yes.Languet
I have seen that other question you referenced, which is why I'm trying to use GhostScript to do it. I already know that iTextSharp can't render a pdf to an image.Languet
How about this? codeproject.com/Articles/32274/…Fondness
As far as I know there is no way in Ghostscript to set its input as a memory stream or to get its output directly into a memory stream. You will have to modify Ghostscript's source code for this.Foremost
Also take into account that it might not be legal for you to use Ghostscript for free in a commercial closed-source application.Foremost
B
4

The thing is that the PDF language, unlike the PostScript language, inherently requires random access to the file. If you provide PDF directly to Standard Input or via PIPE, Ghostscript will copy it to a temporary file before interpreting the 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.

Take a look at the Ghostscript.NET and it's GhostscriptRasterizer sample for the 'in-memory' output.

Benzo answered 4/1, 2014 at 15:34 Comment(1)
In my opinion random access has nothing to do with this issue, since you do have random access on a memory stream. As much as I acknowledge that Ghostscript is a great piece of software, its lack of an API that allows using a memory buffer as in/out has always been a shortcomming.Foremost
E
3

Ghostscript.Net is a wrapper to the 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. I haven't completely figured out the best way to handle the ghostscript dll and where to locate it on the server.

 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();
        }
    }
Etherealize answered 7/5, 2015 at 10:38 Comment(1)
What if not to separate pages?Keening

© 2022 - 2024 — McMap. All rights reserved.