Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to convert .pdf file to thumbnail view in asp.net with c#
Posted

private void generatePNGfromPDF(PdfDecoder decoder, int PageNumber, double width, double height, String formname){
//int size = 100;
try
{
//decoder.decodePage(PageNumber);
//decoder.setPageParameters(1,PageNumber); //values scaling (1=100%). page number
decoder.useHiResScreenDisplay(true);

// the below line sets the multiple of 72 dpi ie: 72 * 4.166 = 300dpi
decoder.setExtractionMode(32, 72, 4.166f);
BufferedImage PDF = decoder.getPageAsImage(PageNumber);
//width = PDF.getWidth() * .75;
//height = PDF.getHeight() * .75;
width = PDF.getWidth();
height = PDF.getHeight();

GraphicsConfiguration gc = getDefaultConfiguration();
final BufferedImage PNG = getScaledInstance(PDF, (int)Math.round(width), (int)Math.round(height), gc);
String fileName = jTextAreaTargetDirectoryPath.getText() + "\\" + formname + "-" + PageNumber + ".png";
ImageIO.write(PNG, "png", new File( fileName ));
decoder.flushObjectValues(true);
}
catch(Exception ex){
ex.printStackTrace();
//flogger.log(Level.SEVERE, "Cannot generate image from page.", ex);
}

}
 
Share this answer
 
Comments
Member 9018012 20-Jul-12 5:20am    
Please could you post the full solution?
Plz click the below link to see the exapmle

http://amitpatriwala.wordpress.com/2009/08/28/pdf-viewer-in-asp-net/[^]
 
Share this answer
 
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);
}
}
 
Share this answer
 
Comments
Member 9018012 20-Jul-12 6:00am    
I have installed the ghostscript and please explain how to import it into c#.net
Sample code
The following code shows how the PDFThumbnail control is used on an ASP.NET web form.

<%@ Page language="c#" %>
<%@ Register TagPrefix="tc"
Assembly="TallComponents.PDFThumbnail"
Namespace="TallComponents.Web.PDF" %>
<html>
<body>
<form method="post" runat="server">
<tc:thumbnail path="PDFThumbnail.pdf"
index="1"
runat="server"
url="www.tallcomponents.com"
borderwidth="1"
bordercolor="gray"
dpi="12" />
</form>
</body>
</html>
 
Share this answer
 
Comments
Member 9018012 20-Jul-12 5:21am    
I am getting the following error:

Could not load file or assembly 'TallComponents.PDFThumbnail' or one of its dependencies. The system cannot find the file specified.

Thanks,

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