0

I need to convert a pdf file into jpeg using c#.

The solution (dll library) have to be free.

I have searched a lot of information on the web but seems that the library pdfiumviewer might be helpful here. It is also available as nuget.

I have tried this code without success, because the new file in jpg format is not saved.

How to do resolve this?

using PdfiumViewer;
using System;
using System.Drawing.Imaging;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            try
            {
                using (var document = PdfDocument.Load(@"sample.pdf"))
                {
                    var image = document.Render(0, 300, 300, true);
                    image.Save(@"output.png", ImageFormat.Png);
                }
            }
            catch (Exception ex)
            {
                // handle exception here;
            }
        }
    }
}

Edit #01

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        try
        {
            using (var document = PdfDocument.Load(@"C:\\Users\\aaa\\Documents\\Visual Studio 2013\\WebSites\\GroupDocsConversion\\sample.pdf"))
            {
                var image = document.Render(0, 300, 300, true);
                image.Save(@"C:\\Users\\aaa\\Documents\\Visual Studio 2013\\WebSites\\GroupDocsConversion\\output.png", ImageFormat.Png);
            }
        }
        catch (Exception ex)
        {
            Response.Write("Error: " + ex.Message);
        }
    }
}
  • Remove the empty `catch` block and inspect the excpetion details. – Uwe Keim Apr 09 '20 at 08:11
  • And/or specify an absolute path to save the image to on your server. Also specify an absolute path to load the PDF from on your server. – Uwe Keim Apr 09 '20 at 08:12
  • "[How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)" – Uwe Keim Apr 09 '20 at 08:13
  • @UweKeim thanks for reply, please see **Edit 01** in my first question. No error but no output.png –  Apr 09 '20 at 08:47
  • https://stackoverflow.com/a/60642706/4122889 aren't there already solutions on google? – sommmen Apr 09 '20 at 08:53

1 Answers1

0

Try this combined solution with iTextSharp and Ghostscript.NET ( available on Nuget ).

If necessary install Ghostscript 9.52 for Windows (32 bit)

I hope I was helpful.

   <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:FileUpload ID="flupload" runat="server" />
                <br />
                <br />
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
            </div>
        </form>
    </body>
    </html>

    using Ghostscript.NET;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.Hosting;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class Default4 : System.Web.UI.Page
    {
            protected void btnSubmit_Click(object sender, EventArgs e)
            {
                String PdfFolderPath = HostingEnvironment.MapPath("~/PdfFolder/" + flupload.FileName);
                HttpPostedFile file = HttpContext.Current.Request.Files[0];
                file.SaveAs(PdfFolderPath);
                Pdf2ImageConversion(flupload.FileName, PdfFolderPath);
            }

            private void Pdf2ImageConversion(string FileName, string PdfFolderPath)
            {
                String FileNameWithoutExtension = Path.GetFileNameWithoutExtension(FileName);
                String ImgFolderPath = HostingEnvironment.MapPath("~/ImgFolder/"
                    + FileNameWithoutExtension + ".jpeg");
                var info = new System.IO.FileInfo(ImgFolderPath);
                if (info.Exists.Equals(false))
                {
                    GhostscriptPngDevice img = new GhostscriptPngDevice(GhostscriptPngDeviceType.Png16m);
                    img.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
                    img.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
                    img.ResolutionXY = new GhostscriptImageDeviceResolution(200, 200);
                    img.InputFiles.Add(PdfFolderPath);
                    img.Pdf.FirstPage = 1;
                    img.Pdf.LastPage = 1;
                    img.PostScript = string.Empty;
                    img.OutputPath = ImgFolderPath;
                    img.Process();
                }
            }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
Hamamelis
  • 1,983
  • 8
  • 27
  • 41