Click here to Skip to main content
15,879,068 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am looking for a simple solution for converting a PDF file into any image format. I am trying to avoid using paid libraries.

The goal is to, after converion, show a preview of the first PDF page inside a panel. To accomplish that, I need to convert at least the first page of the file into an image format. I will have to stamp a code bar into the file, then convert it back to PDF (although this part is not the problem).

I would greatly appreciate if any of you could point me in the right direction.

Cheers!
Posted
Comments
Wandaboiii 27-Oct-14 14:19pm    
Hi. this is Andile here is a very simplest way to convert PDF file with Apitron PDF Rasterizer for .NET

I generally use GhostScript[^] for this type of conversion.

It's possible to load the DLL in-process and use P/Invoke to call it, but I usually shell out to the command-line version. That way, a corrupt or very large PDF won't affect my application.

How To Convert PDF to Image Using Ghostscript API[^]
 
Share this answer
 
// open and load the file
using (FileStream fs = new FileStream(@"Documents\TestFile.pdf", FileMode.Open))
{
// this object represents a PDF document
Document document = new Document(fs);

// default rendering settings
RenderingSettings settings = new RenderingSettings();

// process and save pages one by one
for (int i = 0; i < document.Pages.Count; i++)
{
Page currentPage = document.Pages[i];

// we use original page's width and height for image as well as default rendering settings
using (Bitmap bitmap = currentPage.Render((int)currentPage.Width, (int)currentPage.Height,settings))
{
bitmap.Save(string.Format("{0}.png", i), ImageFormat.Png);
}
}
// preview first rendered page
Process.Start("0.png");
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Oct-14 15:31pm    
It would be a very good solution if you provided all essential detail. Does it use Apitron software? (You only mentioned it in a comment; nothing tells us that it is related to your answer.) If it does, reference the product (provide a link), explain what assembly should be referenced, write exact full type name of the "Document" type, probably some other important detail. Just a few extra words could eliminate the confusion. (I did not vote.)
—SA
BMicka 19-Dec-19 5:07am    
Apitron - bad output quality even paid and watermark. No solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900