In the previous article, I have provided a sample article to read pdf file in C#, and read pdf file using javacript, but in this article, I have provided example to convert pdf into image using C# using PDFium library using Console application (Without using Ghostscript).
Step 1: Create a new console application in your Visual Studio, by navigating to File-> New -> Project -> Select "Windows Desktop" from left pane and "Console App" from right-pane.
Step 2: Now, we will have to install PDFium dlls in our console application using Nuget, so navigate to Tools -> Nuget Package Manager -> Manage Nuget package for this solution
In the browse tab, search for "PDFium"
Install the below-highlighted Nuget Packages.
Also, you need to install another Nuget Package which is shown below
So basically, we have installed below Nuget packages.
- PDFium.x86
- PdfiumViewer
- PdfiumViewer.Native.x86.v8-xfa
Once you have referenced these files, you need to add two folders in your application, which is "x64" for 64-bit architecture and "x86" for 32-bit architecture, and add reference of pdfium.dll in those folders, as shown below
Once you are done with these we are almost done with the set-up.
Step 3: Add a reference to "System.Drawing" in your application, by right-clicking on "Reference" and then select "Add-Reference" -> Search "System.Drawing" in Assemblies, and add it reference.
Step 4: Add the below code in Program.cs, which will convert PDF file into images.
using PdfiumViewer;
using System;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
namespace PdfToImage
{
class Program
{
static void Main(string[] args)
{
var projectDir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
try
{
using (var document = PdfDocument.Load(projectDir + @"\Samples\input.pdf"))
{
var pageCount = document.PageCount;
for (int i = 0; i < pageCount; i++)
{
var dpi = 300;
using (var image = document.Render(i, dpi, dpi, PdfRenderFlags.CorrectFromDpi))
{
var encoder = ImageCodecInfo.GetImageEncoders()
.First(c => c.FormatID == ImageFormat.Jpeg.Guid);
var encParams = new EncoderParameters(1);
encParams.Param[0] = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, 100L);
image.Save(projectDir + @"\Samples\output_" + i + ".jpg", encoder, encParams);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("Press enter to continue...");
}
}
}
In the above code, we are loading the sample pdf file, by passing it's location
using (var document = PdfDocument.Load(projectDir + @"\Samples\input.pdf"))
and once we have a PDF file, we are looping pdf file pages, using below "for" loop
var pageCount = document.PageCount;
for (int i = 0; i < pageCount; i++)
{
//converting and saving page into image here
}
and then saving it as image.
Below are the images of sample PDF file which we have used.
As you can see from the above image, the sample PDF has 5 pages.
So, once you will build and execute your console application above code, you can see 5 images as output in your application's "Sample" folder
That's it, we are done, so in this way you can convert pdf into images without using Ghostscript code.
If you are getting an error "Unable to find an entry point named 'FPDF_AddRef' in DLL 'pdfium.dll'.", then right-click on your project, select "Build" and select target platform as "x86"
As we have already installed PDFium dll's for this platform, then your project should work after targeting 32-bit architecture.
There is another Nuget Package also, which you can try https://www.nuget.org/packages/Pdf2Png/ (PDF2PNG), but we haven't tested it, as PDFium looked perfect to use.
You may also like to read:
Export HTML table to Excel or PDF using jQuery
Convert Text to HTML using Javascript
Best .NET APM tools to consider for your .NET application
Model Validation in ASP.NET Core MVC (With Custom validation example)
vikas_jk
Yes, PdfiumViewer is free
https://github.com/pvginkel/PdfiumViewer
Take a look at above link.