How to convert pdf to image in memorystream with Ghostscript?

326 views Asked by At

I used GhostscriptWrapper for convert pdf to image
but how can convert pdf to image in memorystream instead save it
i need to design pdf viewer and need to convert pdf to image in memory stream

        GhostscriptSettings gs = new GhostscriptSettings();
        gs.Device = GhostscriptDevices.png16m;
        gs.Page = new GhostscriptPages { Start = page, End = page };
        gs.Resolution = new Size { Height = 150, Width = 150 };
        gs.Size = new GhostscriptPageSize { Native = GhostscriptPageSizes.a4 };
        GhostscriptWrapper.GenerateOutput("C:\\PDFViewer\\my\\test.pdf", "C:\\PDFViewer\\my\\test.png", gs);
1

There are 1 answers

0
Albert D. Kallal On BEST ANSWER

GhostScript.net has support for memory streams (non physical files).

However, I am not sure if you could do this in real time, since GS takes a nice "chunk" of CPU and processing to convert a PDF to an image, and this is especially for larger PDF's. And in some cases, even for a one page PDF, if it is a larger page, then again, it can take a few seconds for that PDF to be converted to a raw byte image.

The above thus suggests that you may well want to process the PDF files BEFORE you attempt to display such preview images on your site. I do this for PDF thumbnails, and save the image (raw byte jpeg image) to a database.

If just one image to be displayed on the page, then I suppose it becomes practical to do this in real time. For multiple images on a page, then I suggest preprocessing the PDF to image previews.

However, let's assume a PDF file, and then we want to display the PDF (1st page) as an image, and do so WITHOUT CREATING an image file on disk.

So, we have this markup:

        <asp:Button ID="Button1" runat="server" Text="Get PDF Image" 
            CssClass="btn"
            OnClick="Button1_Click"                
            />
        <br />
        <br />

        <asp:Image ID="Image1" runat="server" Height="215px" Width="334px" />

So, a simple button and image on the web page.

So, for a given pdf file, let's display a thumbnail (image), and do so without creating a file (memory stream).

So, code behind is this:

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        string strF = @"c:\test\CONTROL.pdf";

        byte[] MyBytePic = GetPdfThumb(strF);

        string sMineType = MimeMapping.GetMimeMapping("abc.jpeg");

        Image1.ImageUrl =
                $@"data:{sMineType};base64,{Convert.ToBase64String(MyBytePic)}";


    }

    public byte[] GetPdfThumb(string strPDF)
    {
        Ghostscript.NET.GhostscriptVersionInfo gVER;
        string strgDLL = System.Web.HttpContext.Current.Server.MapPath(@"~\MyDLL") + @"\gsdll64.dll";
        gVER = new Ghostscript.NET.GhostscriptVersionInfo(new Version(0, 0, 0), 
                    strgDLL, "", Ghostscript.NET.GhostscriptLicense.GPL);

        using (GhostscriptRasterizer gPDF = new GhostscriptRasterizer())
        {
            using (MemoryStream fs = new MemoryStream())
            {
                try
                {
                    gPDF.Open(strPDF, gVER, true);
                    gPDF.GetPage(18, 1).Save(fs, ImageFormat.Jpeg);
                    gPDF.Close();
                    fs.Close();

                    return fs.ToArray();
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.Message);
                    return new byte[0];
                }
            }
        }
    }

And the result is thus this:

enter image description here

So, above shows proof of concept of how a PDF image preview can be created, and created 100% in memory.

I should also point out that except for rather small preview images, you don't want to use the above base64 string and send that directly to the web page. While this is easy, such images are not cached, and worse they will post-back to the server, and that can result in REALLY slow pages if you have a lot of such images on one page. So, say for 1-5 small preview images, then the above base64 string being directly sent to the image control is fine. However, if you have more images, then I suggest you create a generic web handler, and this will allow the browser to cache such images.