Wednesday, October 1, 2008

C# convert PDF to image format

The solution generally is provided by 3rd party component. In many cases it is not free. E.g., PDFRasterizer.NET component, PDF4NET etc.

Here's one solution: http://www.mobileread.com/forums/showthread.php?t=10137

The GFL SDK/GFLAx (http://www.xnview.com/en/gfl.html) free library component can be used to convert PDF to image format. It works for ASP, VB, C# etc. GhostScript (http://sourceforge.net/projects/ghostscript/) is required for it to work.

Example code in C#:

{
string file = "D:/test.pdf";
string image = "D:\\test.png";

try
{
GflAx.GflAxClass g = new GflAx.GflAxClass();
g.EpsDpi = 150;
g.Page = 1;
g.LoadBitmap(file);
g.SaveFormat = GflAx.AX_SaveFormats.AX_PNG;
g.SaveBitmap(image);
MessageBox.Show(this, "PDF to PNG conversion ended");
}
catch (Exception ex) {
MessageBox.Show(this, "GflAx error: " + ex.Message);
}
}

56 comments:

Unknown said...

i need some help with this, i am trying to use this assembly on a .net active x control i have written. It works fine when i run the winform, but once i try to run this inside my browser it says it cant find the assembly

Tom said...

Sorry I didn't see this comment last December. If you're using it in a .NET web application I think you need to add it to the references list of the web application.

Chris said...

Hi~

I try to use the GflAx, but once it load a pdf it return "Error HRESULT E_FAIL has been returned from a call to a COM component.".

It run fine if I just load a bmp file and it can save a bmp file too. Is it something about the ghostscript? I don't know how to use it. Can you please give me some advice?

Ashish said...

hi, i had download files from GhostScript (http://sourceforge.net/projects/ghostscript/) & GFL SDK/GFLAx (http://perso.orange.fr/pierre.g/xnview/engfl.html),
and installed in my desktop. after i create new window project in visual studio 2005. and put this c# coding. but i am not found GflAx.GflAxClass assembly(.dll), will you give me correct way(path), how to get GflAx.GflAxClass assembly(.dll) in my application. or give me sample source code program.

Tom said...

Ashish,

Before compile the above code, you need to add GflAx.GflAxClass assembly to your project reference: right click on References -> Add Reference -> COM, then scroll down the list to find the GflAxObject component and add it.

Hope this helps.

faraz said...

i use this code to convert my pdf to png everything is ok wid this code except the color in png file differs from original pdf please tell me how to set color attributes using gflax class object.....
thanx in advance

Tom said...

faraz, I worked with black and white pdf only, and thus can't answer your question. What you can do is to check the http://www.xnview.com/en/gfl.html website to see if there is any API on color setting. My impression is there should be because they work with a lot of image types and color is an essential part of manipulating images. Please post here if you find any solution. Good luck and thanks.

Anonymous said...

how convert password protected pdf to image c#?

Tom said...

There are commercial products on this, which you can find by searching google.

M.M.M.R.B. Mudannayake said...

Hey Tom,
I am developing a C# .NET application which needs to convert a .docx file to any image file. i searched and searched all over the net but couldn't find a solution on that.
So what im trying to do is convert the .docx file to a pdf and make it an image file.
So can you please upload your whole code for the class on how to convert PDF to an image using GFLAx.
Thanks in advance. :)

Tom said...

M.M.M.R.B. Mudannayake,

The above is the whole code. The rest is just to add component reference to the project.

Good luck.

M.M.M.R.B. Mudannayake said...

Hey Tom,
Thank you very much for the quick reply. Really appreciated.
I was a Java guy,:D where i had to jump to .NET since nowadays the company im working is focusing on .NET. So im a very newbie for .NET and DLLs.
So can u please give me some advice on what DLLs and using statements that i should used in the class. :(
Sorry for bugging u like this. And thanx again.

M.M.M.R.B. Mudannayake said...

Hi Tom,
I added the GflAx libraries to the project as you have mentioned earlier. And I have installed the Ghost Script 8.6.3 (gs863w32.exe) also. But it seems like things not working for me. :'(
Program just hangs out and nothing prints out nor conversion is happening. :( Code is working well according to faraz, so can i get some help on this.
Thanks Tom.

Tom said...

Check if you have write permission on the output folder.

Anonymous said...

Hi Tom

Really good forum. I have been googling about pdf converter and I am really interested in this one.

But before trying to install anything, I was wondering how this assembly deal with PDF with multiple pages?

Will generate a image with all the pages?

Looking forward to hearing about you.....

Tom said...

Yes, it works with multiple pages. See the example code, it has a "page" attribute.

Anonymous said...

Hi Tom, Many thanks for replying about the pages.

I am posting again because i cannot see the COM reference flAx.GflAxClass assembly. I have Visual Studio2008 and my project is in C#.

I was looking all the deployed files (*.dll) and I have found a file with the name GflAx.dll, but VS2008 does not accepted as a valid assembly.

What am i missing? Which dll should I considered? Why my visual studio has not registered in the Add Reference dialog option?

Thanks Tom

Looking Forward to hearing about you...

Tom said...

Hi, I don't know exactly what you did and what happened. Usually "add reference/COM" should work. This kind of solution can be found by trying again and again. Just do it.

Anonymous said...

Hi Tom..

Thanks for repliyin so quicly. I have found the reason. Apparentely is not registered in Windows Vista hence you have to do it manually by using regsvr32.exe.

Now is working fine and I am generating the pictures.

I have another question regarding with the pages. I do not know how many pages my pdf files have so I would like to know how can i generate the image file with all the pages included?

I have commented the statement g.Page=1, but is still generating the picture with only one page.

Looking forward to hearing
Thanks Tom

Tom said...

Hi, I don't know exactly. But maybe you can try to increase .Page = 1,2,3,... until you get an exception thrown.

Karl said...

I got this working by finding the GflaX file, using regsvr32.exe to register the DLL, then found and included it as a reference from the COM tab of "Add a reference". Then I included the "using GflAx;" using statement, and used the code provided here. Works absolutely fine. :)

Anonymous said...

To anyone who finds that it hangs when you try to open the file, this might be because of what appears to be a bug if you run this in a console application:

It seems that ghostscript expects further commands on stdin after having loaded the file. If you switch to the console window, type "quit" and press enter, you may find that it loads the bitmap and then carries on as normal.

This is clearly a problem - I don't know how to fix it yet.

Unknown said...

When I try to use the code sample, I get this error (VS2010):
Error 1 Interop type 'GflAx.GflAxClass' cannot be embedded. Use the applicable interface instead.

What am I missing?

Unknown said...

Got it... I had to take Class off of GFLaxClass

private void testGFLax()
{
string file = "D:/test.pdf";
string image = "D:\\test.png";

try
{
GflAx.GflAx gflAx = new GflAx.GflAx();

gflAx.EpsDpi = 150;
gflAx.Page = 1;
gflAx.LoadBitmap(file);
gflAx.SaveFormat = GflAx.AX_SaveFormats.AX_PNG;
gflAx.SaveBitmap(image);

MessageBox.Show("PDF to PNG conversion ended");
}
catch (Exception ex)
{
MessageBox.Show("GflAx error: " + ex.Message);
}
}

Anonymous said...
This comment has been removed by the author.
Anonymous said...

Hi,

I'm using this converter to convert from PDF to PNG as well. However, the PNG is losing clearity upon conversion. I changed the EpsDpi property of GflAx object to 700 and I was able to get a better quality. Could anyone provide some help on this?

Much appreciated!

Anonymous said...

Please try to use this component! It can convert PDF files to word, images!
Example on web-site: http://www.sautinsoft.com/products/pdf-focus/index.php

Dartraf said...

Hi,

i'd like know if there is a way to convert pdf in image without install gostscript (just install gfl library)

Tom said...

Dartraf, I think you will have to install ghostscript. That's how gfl is designed to work.

Lee said...

Hi Tom,

Great post, it has helped me a lot. I am just having one issue. I am trying to create the thumbnails on the fly when a web page is loaded. The actual pdfs sit in a web location eg http://www.test.com/path/test.pdf

When I pass this path as the filename to g.Loadbitmap(file) it returns file Unknown, do you know if g.LoadBitmap works with pdf paths sent in URL format?

Many Thanks

joe keyrouz said...

Hey Tom,
Thank you for this post,
Actually i am receiving an error message Unknown format
when the code arrives to
g.LoadBitmap(file);
do you have any idea why?

Anonymous said...

Hello Tom,

I am trying to use your code to iterate through all the pages in a pdf and create corresponding png(s). The 1st page is created fine. But the 2nd page fails stating 'Error to create file'. I know that I am doign something wrong here but dont know what. Please take a look and let me know.

try
{
GflAx.GflAxClass gf1 = new GflAx.GflAxClass();
gf1.EpsDpi = 150;
gf1.LoadBitmap(file);
for (int page = 1; page <= gf1.NumberOfPages; page++)
{
gf1.Page = page;
gf1.SaveFormat = GflAx.AX_SaveFormats.AX_PNG;
image = image + @"\" + name + "_" + page + ".png";
gf1.SaveBitmap(image);
}
MessageBox.Show("done");
}

Tom said...

Lee:

From your experience it might not support URL. You can first download it and then do the conversion.

Joe:

I can't tell without knowing more. But probably by this time you've solved it already.

Anonymous:

Is this a trivial bug on the line:
image = image + @"\" + name + "_" + page + ".png";

You may need to define image_base and rewrite it as:

image = image_base + @"\" + name + "_" + page + ".png";

Sam said...

Hi

Thanks for the script. Unfortunately, it's not working as expected. It does the conversion to PNG, but I'm only getting a little less 50% of the page, with extra white space. I tried using the ResizeCanvas function, but no good result either there.

Any idea why it's not converting the WHOLE page ?

try
{
GflAx.GflAxClass g = new GflAx.GflAxClass();
g.EpsDpi = 300;
g.Page = 1;
g.LoadBitmap(sourceFilePath);
g.Resize(548, 708);
g.SaveFormat = GflAx.AX_SaveFormats.AX_PNG;
g.SaveBitmap(destinationFilePath);

}
catch (Exception ex)
{

}

Sam said...

Regarding my last post, I have discovered that it's only some PDFs that have this problem. It's actually PDF generated from a scanner. I will try to resolve the problem. If you have any idea how/why exactly it's not rendering the whole page, please share your thoughts. Thank you

Marko said...

Try to use PDF Focus- It's .Net component for converting PDF to Images and PDF to Word. http://www.sautinsoft.com/products/pdf-focus/convert_pdf_to_image_jpg_tiff_jpeg_bmp_png_asp_net.php

Anonymous said...

Hi...I am using GflAxObject for converting pdf to image. It is working fine on 32 bit PC but not working on 64 bit. I have tried both the dlls i.e., ghostscript32 and ghostscript64. It is showing exception "Unknown Format" at the line gflAx.LoadBitmap(Inputfilepath).

Please help.

Thanks and Regards,
Vivek

Anonymous said...

Please send me the solution at vivekpro26@gmail.com

aakash said...

Hi Tom, this is a nice post but i am getting one exception saying unknown format on line g.loadbitmap(file).Please provide me more details on this.

Thanks...

Anonymous said...

Hi Tom, this is a nice post but i am getting one exception saying unknown format on line g.loadbitmap(file).Please provide me more details on this.

Thanks...

aakash said...

Hi Tom, this is a nice post but i am getting one exception saying unknown format on line g.loadbitmap(file).Please provide me more details on this.

Thanks...

Семён said...

Can i take link to the project?

Anonymous said...

some sample codes of C# converting pdf to image.

public static void SavePageToImageDemo()
{
string fileName = FolderName + "Sample.pdf";

REDocument doc = REFile.OpenDocumentFile(fileName, new PDFDecoder());//use PDFDecoder open a pdf file

int pageCount = doc.GetPageCount();//get pdf's page count

for (int pgIdx = 0; pgIdx < pageCount; pgIdx++)
{
BasePage aPage = doc.GetPage(pgIdx);//get page from REDocument

try
{
REImage img = (REImage)aPage.ToImage();//translate page to image
REFile.SaveImageFile(img, FolderName + "output" + pgIdx + ".bmp");//save image
}
catch (Exception)
{
Debug.WriteLine("Fail to display page " + pgIdx);
}
}
}

ketty said...

You can also convert PDF to any image format with Aspose.PDF for .NET Library.

Anonymous said...

I've ever tried pqScan .NET PDF to Image SDK for C# convert PDF to Gif image format.

Unknown said...

this useful c# .bmp to pdf converter online can be download on rasteredge, and we will provide you the best and fast free vip service on page http://www.rasteredge.com/how-to/csharp-imaging/pdf-html5-feature-create-pdf/

Unknown said...

c# .net convert pdf to jpg is what you are trying to do is impossible, what you could try to do is show c# .pdf to .jpg converter online was that what you were trying?, if so, try something like this: http://www.rasteredge.com/how-to/csharp-imaging/pdf-convert-raster/

Unknown said...

I'm not a developer, i always use this free online pdf to image converter to convert pdf to image online http://www.rasteredge.com/how-to/csharp-imaging/pdf-convert-raster/

DomFilk said...

you can use this c# library to convert pdf to png in c#

johan said...

Convert PDF to Imagesusing mobile app dowmload mobile PDF to JPG converter, PNG and TIFF image converter.Free online service to convert a PDF file to a set of optimized JPG images. It offers mass conversion in mobile android Convert all pages in a PDF to JPG or extract all images in a PDF to JPG. Convert or extract PDF to JPG online, easily and free.
pdf to jpg converter

online pdf to jpg converter pdf to jpg online free pdf to jpg converter jpg to pdf converter online image to pdf converter
change pdf to jpg
image to pdf

Anonymous said...

C# convert PDF to image format with .net pdf converter

John Deepolos said...

ZetPDF Library for .NET offers you a modern, simple, fast, flexible and powerful tool to create complex and stylish PDF documents in your applications with just a few lines of code. Using the high-quality PDF conversion engine you can easily design a document & modify the document.

Unknown said...

I recommend iDiTect.Converter, one professional document converter component for .net, can convert pdf to image format in c#, support jpeg, tiff and png format

Jim Green said...

I recommend a C# pdf to image SDK for everyone. It can set the dip, length and width of the image after PDF to image. You must use it.

Warnerhill said...

Good content and nice blog. Thanks for sharing

such great information. hope you keep sharing such

kind of information Word to Image Converter

Jim Green said...

C# pdf to image has conversion between many formats, such as png, jpg, jpeg, bpm, etc.

Blog Archive

Followers