C# PDF转 图片
简单示例:
public class Form1:Form
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
private void Page_Load ( object sender, System.EventArgs e )
{
this.Label1.Text = “”;
}
#region Web Form Designer generated code
override protected void OnInit ( EventArgs e )
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit( e );
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent ()
{
this.Button1.Click += new System.EventHandler( this.Button1_Click );
this.Load += new System.EventHandler( this.Page_Load );
}
#endregion
private string GetFilePath ()
{
//4/19/2005 5:44:28 PM
string time = System.DateTime.Now.ToString();
time = time.Replace( “/”, “” );
time = time.Replace( “ “, “” );
time = time.Replace( “:”, “” );
//time+=”.pdf”;
string path = Server.MapPath( “PDF2JPG.aspx” );
path = path.Replace( “PDF2JPG.aspx”, @”Files” + time );
return path;
}
private void Button1_Click ( object sender, System.EventArgs e )
{
//upload
int len = File1.PostedFile.FileName.Length;
string filetype = File1.PostedFile.FileName.Substring( len – 3, 3 );
if ( filetype.ToLower() != “pdf” )
{
this.Label1.Text = “Please select pdf file”;
return;
}
else
this.Label1.Text = “”;
string fileName = GetFilePath();
string pdffile = fileName + “.pdf”;
if ( File1.PostedFile != null )
{
try
{
File1.PostedFile.SaveAs( pdffile );
}
catch
{
Response.Write( “Upload fail…” );
}
}
//convert to jpg
double dpi = 120;
XpdfRasterizerNet.XpdfRasterizerClass rast = new XpdfRasterizerNet.XpdfRasterizerClass();
rast.loadFile( pdffile );
for ( int page = 1;page <= rast.numPages;page++ )
{
string jpgfile = fileName + page.ToString() + “.bmp”;
try
{
rast.writePageBitmap( page, dpi, rast.imageRGB, rast.imageFileBMP, jpgfile );
System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
image.ImageUrl = jpgfile;
this.PlaceHolder1.Controls.Add( image );
}
catch
{
Response.Write( “Convert fail…:” );
}
}
rast.closeFile();
}
}