Question:
How to convert PDF to image in a memory stream with Ghostscript?

Solution:

You can use GhostScript.net which supports memory streams (nonphysical files).

However, this will be a hard task to do in real time as GS takes a lot of chunks of your CPU. Plus, the process of converting PDF to image is also a huge task. This is especially true for larger PDFs. The conversion of a PDF to a raw byte image may occasionally take several seconds, even for a one-page document if the page is larger.


Therefore, it would be a good idea to process the PDF files before attempting to display such preview images on your website, as the above suggests. I save the image (raw byte jpeg image) to a database and use this for PDF thumbnails.


You can do this in real time if there is only one image to be shown on the page. Preprocessing the PDF to image previews is what I advise when there are several images on a page.


But let's say we have a PDF file. We want to show the first page of the PDF as an image, but we don't want to create an image file on disk for that.


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" />


Thus, the webpage only has a button and an image.

Thus, let's show a thumbnail (image) for a given PDF file without generating a file (memory stream).


So, the 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:


The above concept shows how you can create a PDF image preview 100% in memory.


Suggested blogs:

>>Step by Step guide to Deploy Terraform in Azure using GitHub Actions

>>Testing react components using Hooks and Mocks

>>How to destroy a custom object within the use Effect hook in TypeScript?

>>Create a function that convert a dynamic Json to a formula in Typescript

>>Narrow SomeType vs SomeType[]

>>Use Firebase Realtime Database with ASP.NET MVC App

>>Use of Singletons in .NET Core in AWS Lambda

>>How to do PHP Decryption from Node.js Encryption


Ritu Singh

Ritu Singh

Submit
0 Answers