Thursday, March 29, 2018

Generate Image from PDF in .net core

Generating image from PDF is one of the general requirements that we come across during our project development. PDF being most accepted document format worldwide, and showing thumbnail image of the first page is expected.

Similar requirement I stumbled upon, and paid components such as Aspose.PDF comes to your rescue, but they do  provide many other features along with  PDF to Image converter which, we might not need and off course you need to shell out $900 plus for such library.

Step back and think: 
- First, how does windows 10 shows image of PDF document page. There might be some executable that generated it. BINGO!! you are right. 
- Now next question, can we save it as PDF from our word processor? Oh my god, you are getting it all, YES!!
- Right, so we need to find an executable that does so. YES. 
- Next question is can we call the shell command from c# .net core. Oh come on this is a silly question, YES of course.

Alright, so we have all the answers to our questions. 
Now with such a big open source community, determined to provide everything we need free of cost, there must be some executable to do such work.
YES it is. 
"Where there is will there is way."

Kool (Cool), just browse the following link:

http://blog.alivate.com.au/poppler-windows/

Here you will find executable's that will help you in doing lot of PDF related operations, that you can integrate in your code.

The executable that is of interest to us for generating image is pdftoppm.exe
The below line will generate image from PDF.

var process = System.Diagnostics.Process.Start(execuatble, arguments);

command line sample to generate image is:

pdftoppm -f 1 -l-1 -png scale-to 200 "filetoconvert.pdf" "image_file.png"

-f is for first page which I have give 1
-l is for last page which is again 1 as I just want one page to be generated as image.
-png is the image format to generate
-scale-to is the parameter where you can define size of the box.



followed by two parameters : first one is the PDF file you want to convert and second one is the image file to generate.

You can see all the parameters required by the following command.
pdftoppm -help

The above will list all the arguments acceptable.

Hope you guys liked my post.

Happy Coding!!
















No comments:

Post a Comment