用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();
}
相关文章推荐
- 用C#打开一个pdf文件
- C#.NET常见问题(FAQ)-想要另存一个项目,sln文件丢了怎么办,如何打开高版本的项目
- C#之WinForm基础 得到文件对话框(一次只能打开一个文件)打开文件的绝对路径
- C# 打开pdf、doc。xls.文件
- 用C#代码生成一个简单的PDF文件
- 请问C#程序中又激发一个exe文件的api?如打开记事本。
- 如何在程序中打开PDF文件 -C#文章(.net)
- c#开发一个“记事本”程序,要求能够实现文件的新建、打开、编辑、保存功能
- C#双击文件只让同一个程序打开文件
- [导入]用c#打开一个外部文件
- C#将制定文件夹下的PDF文件合并成一个并输出至指定路径
- C#将制定文件夹下的PDF文件合并成一个并输出至指定路径
- C# FileStream 同时打开一个文件的方法
- C#初学者--建立一个button打开并显示dbf文件
- C#将制定文件夹下的PDF文件合并成一个并输出至指定路径
- C#将制定文件夹下的PDF文件合并成一个并输出至指定路径
- WinForm/C#中打开一个文件,主要是用到进程的知识。
- C#如何打开一个文件夹并定位到某个文件
- 用C#代码生成一个简单的PDF文件
- 用C#代码生成一个简单的PDF文件