Mukesh Kumar Mukesh Kumar - 1 month ago 30
C# Question

iTextSharp- How to create thumbnail image from first page of a pdf file

I want to create thumbnail image from first page of a Pdf file. The code which I am using is:

using (FileStream fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (Document doc = new Document())
{
using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
{
PdfReader r = new PdfReader(inputFile);
PdfImportedPage importedPage = w.GetImportedPage(r, 1);
iTextSharp.text.Image PdfImage = iTextSharp.text.Image.GetInstance(importedPage);
PdfImage.ScaleAbsolute(importedPage.Width / 2, importedPage.Height / 2);
System.Drawing.Image img = System.Drawing.Image.FromStream(new MemoryStream(PdfImage.RawData));
img.Save(thumbNailImagePath);
doc.Close();
r.Close();
}
}
}


Here PdfImage.RawData is returning null value. Can anyone tell me what is wrong here? I am new to iTextSharp, can anyone tell me if it is possible to create thumbnail image of first page of pdf content using iTextSharp. Thanks in advance.

Answer

Thanks Bruno and Amedee. Based on your comment, I have used GhostscriptSharp to create thumbnail. It has the method GhostscriptWrapper.GeneratePageThumb(inputFile, thumbnailPath, pageNo, width, height) to create thumbnail of particular page.