Published by Cole Francis, on Friday, January 12, 2018
By: Cole Francis, Senior Architect at The PSC Group, LLC.
Let’s say you’re working on a hypothetical project, and you run across a requirement for creating an image from the first page of a client-provided PDF document. Let’s say the PDF document is named MyPDF.pdf, and your client wants you to produce a .PNG image output file named MyPDF.png.
Furthermore, the client states that you absolutely cannot read the contents of the PDF file, and you’ll only know if you’re successful if you can read the output that your code generates inside the image file. So that’s it, those are the only requirements. What do you do?
Thankfully, there are a number of solutions to address this problem, and I’m going to use a lesser known .NET NuGet package to handle this problem. Why? Well, for one I want to demonstrate what an easy problem this is to solve. So, I’ll start off by searching in the .NET NuGet Package Manager Library for something describing what I want to do. Voila, I run across a lesser known package named “Pdf2Png”. I install it in less than 5 seconds.
So, is the Pdf2Png package thread-safe and server-side compliant? I don’t know, but I’m not concerned about it because it wasn’t listed as a functional requirement. So, this is something that will show up as an assumption in the Statement-of-Work document and will be quickly addressed if my assumption is incorrect.
Next, I create a very simple console application, although this could be just about any .NET file type, as long as it has rights to the file system. The process to create the console application takes me another 10 seconds.
Next, I drop in the following three lines of code and execute the application, taking another 5 seconds. This would actually be one line of code if I was passing in the source and target file locations and names.
string pdf_filename = @"c:\cole\PdfToPng\MyPDF.pdf";
string png_filename = @"c:\cole\PdfToPng\MyPDF.png";
List errors = cs_pdf_to_image.Pdf2Image.Convert(pdf_filename, png_filename);
Although my work isn’t overwhelmingly complex, the output is extraordinary for a mere 20 seconds worth of work! Alas, I have not one, but two files in my source folder. One’s my source PDF document, and the other one’s the image that was produced from my console application using the Pdf2Png package.
Finally, when I open the .PNG image file, it reveals the mysterious content that was originally inside the source PDF document:
Before I end, I have to mention that the Pdf2Png component is not only simple, but it’s also somewhat sophisticated. The library is a subset of Mark Redman’s work on PDFConvert using Ghostscript gsdll32.dll, and it automatically makes the Ghostscript gsdll32 accessible on a client machine that may not have it physically installed.
Thanks for reading, and keep on coding!
Categories: .NET,.NET Architecture,C#,Pdf to Png,PSC