您的位置:首页 > 编程语言 > C#

用C#打开一个pdf文件

2007-05-25 07:10 393 查看

http://www.o2sol.com/public/webui/samples.shtml
下载XpdfRasterizer.dll,XpdfRasterizerNet.dll,引用即可用

using XpdfRasterizerNet;

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();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: