关于 asp.net在线生成pdf文档 或 在线doc转换pdf
因为需求需要需要生产pdf 文档,来存档。不知道有没有现成的好用的. net pdf dll组件可以直接调用呢?
我知道一个是itextsharp.dll 但使用起来复杂,有没有其他的呢。各位讨论下!
谢谢。 --------------------编程问答-------------------- 首先你这功能可以实现,但是但是如果在线转换的话,word要是页数多,时间会很长 --------------------编程问答-------------------- 页数不多,只要功能实现 --------------------编程问答-------------------- 自己顶下 --------------------编程问答-------------------- 我以前在codeproject上收藏了一篇文章,就是在web server转化word到pdf的。先上传一word文档然后转化为pdf。
http://www.codeproject.com/KB/aspnet/word2pdf_serverconvert.aspx
看看吧。 --------------------编程问答-------------------- 绘制pdf,这个简单,网上有很多资料,有兴趣的朋友可以研究pdf的文件格式,安装二进制组装pdf。我有兴趣,然而没有时间,我觉得软件从业者时刻都应该关注最有价值的事情。软件从业者要提高效率的第一法门便是重用,网上有一个叫itextsharp的东西是用来绘制pdf的,可以免费使用而且开源。
下载itextsharp,试着用itextsharp绘制htm看看效果,如您所料,绘制出的是htm的源代码。因为第一步的事情我们还没有解决,下面来解决第一步的事情。
记得很久以前见过一个.net写的网页snap工具,大概思路是利用webbrowser的DrawToBitmap方法将ie的显示结果输出到Sytem.Drawing.Bitmap对象。大概代码如下:
//WebBrowser wb=null;
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(w, h);
wb.DrawToBitmap(bmp, new System.Drawing.Rectangle(0,0, w, h));ok,htm可以解析了,现在重组刚才的代码,思路如下:
使用webbrowser将htm解析并转换为图片,使用itextsharp将刚才的图片绘制成pdf。
有用是给公司开发的功能,暂时不便公开源码,提供我编译后的工具供下载使用,您也可以根据上面的思路定制:
使用方法,
1.将单个url转换为pdf:PageToPDF.exe "http://www.g.cn/" "google.jpg"
2.将多个url转换为pdf:pagetopdf.exe task.txt "C:\pdfdir\"
task.txt是任务里表,里面提供多行url,每个url以#文件名为后缀,如:http://www.baidu.com/#b表示将http://www.baidu.com/转换为pdf文件名为b(扩展名系统自己会追加)
在asp.net环境下使用
将pagetopdf上传至网站中,设定好目录权限,示例代码:
Code
public static bool CreatePPDF(string url,string path)
{
try
{
if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(path))
return false;
Process p = new Process();
string str = System.Web.HttpContext.Current.Server.MapPath("~/afafafasf/PageToPDF.exe ");
if (!System.IO.File.Exists(str))
return false;
p.StartInfo.FileName = str;
p.StartInfo.Arguments = " \"" + url + "\" " + path;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
System.Threading.Thread.Sleep(500);
return true;
}
catch(Exception ex)
{
Sys.Log.error("Pdf create err.",ex);
}
return false;
}
--------------------编程问答-------------------- 有很简单的,就是安装pdf虚拟打印机,然后把文档打印出来就ok了!然后监视输出路径,把pdf移走到指定目录,我们用delphi实现过,并用了n年了! --------------------编程问答-------------------- 支持一下 --------------------编程问答-------------------- http://blog.csdn.net/ajaxtop/archive/2009/10/26/4730174.aspx
——将GridView导出为PDF 通过itextsharp
主要介绍将GridView显示的内容转换为PDF文档,当用户访问并想将页面显示(GridView)的内容保存为PDF时即可通过本程序先将转换后的PDF文件保存到服务器中指定的文件夹下,再自动提示用户是否将得到的PDF文档保存到本地。
转换后的PDF文档每页都会有GridView的表头。
1. 得到itextsharp.dll (从网上可以得到)
2. 将这个dll添加引用
3. 下面介绍转换的类GridViewToPdf.cs里面就写了一个转换方法ConvertGridViewToPdf()
类GridViewToPdf.cs如下:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//******************************************
//引入的命名空间
using System.Text;
using System.IO;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
//******************************************
/// <summary>
///GridViewToPdf 的摘要说明
/// </summary>
public class GridViewToPdf
{
public GridViewToPdf()
{ }
#region ConvertGrdiViewToPdf 换GridView为PDF文档,每一页都有表头
/// <summary>
/// 转换GridView为PDF文档
/// </summary>
/// <param name="pobjGrdv">要转换的GridView</param>
/// <param name="PDFFileName">在服务器端保存PDF时的文件名</param>
/// <param name="FontPath">PDF甩用字体所在的物理路径</param>
/// <param name="FontSize">字体大小</param>
/// <returns>返回调用是否成功</returns>
public static void ConvertGrdiViewToPdf(GridView pobjGrdv, string PDFFileName, string FontPath, float FontSize)
{
//在服务器端保存PDF时的文件名
string strFileName = PDFFileName + ".pdf";
// GridView的所有数据全输出
pobjGrdv.AllowPaging = false;
//**************************
int countColumns = pobjGrdv.Columns.Count;
int countRows = pobjGrdv.Rows.Count;
if (countColumns != 0 && countRows != 0)
{
//初始化一个目标文档类
//Document document = new Document();
//竖排模式,大小为A4,四周边距均为25
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
//横排模式,大小为A4,四周边距均为50
//Document doc = new Document(PageSize.A4.rotate(),50,50,50,50);
//调用PDF的写入方法流
//注意FileMode-Create表示如果目标文件不存在,则创建,如果已存在,则覆盖。
PdfWriter writer = PdfWriter.GetInstance(document,
new FileStream(HttpContext.Current.Server.MapPath(strFileName), FileMode.Create));
try
{
//创建PDF文档中的字体
BaseFont baseFont = BaseFont.CreateFont(
FontPath,
BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);
//根据字体路径和字体大小属性创建字体
Font font = new Font(baseFont, FontSize);
// 添加页脚
//HeaderFooter footer = new HeaderFooter(new Phrase(footertxt), true);
HeaderFooter footer = new HeaderFooter(new Phrase("-- ", font), new Phrase(" --", font));
footer.Border = Rectangle.NO_BORDER; // 不显示两条横线
footer.Alignment = Rectangle.ALIGN_CENTER; // 让页码居中
document.Footer = footer;
//打开目标文档对象
document.Open();
//**************************
//根据数据表内容创建一个PDF格式的表
PdfPTable table = new PdfPTable(countColumns);
//iTextSharp.text.Table table = new iTextSharp.text.Table(pobjGrdv.Columns.Count);
// 添加表头
// 设置表头背景色
//table.DefaultCell.BackgroundColor = Color.GRAY; // OK
//table.DefaultCell.BackgroundColor =
//(iTextSharp.text.Color)System.Drawing.Color.FromName("#3399FF"); // NG
table.DefaultCell.BackgroundColor = iTextSharp.text.Color.LIGHT_GRAY;
//table.DefaultCell.BackgroundColor = System.Drawing.Color.DodgerBlue;
// 添加表头
for (int j = 0; j < countColumns; j++)
{
table.AddCell(new Phrase(pobjGrdv.HeaderRow.Cells[j].Text, font)); // OK
}
// 告诉程序这行是表头,这样页数大于1时程序会自动为你加上表头。
table.HeaderRows = 1;
// 添加数据
// 设置表体背景色
table.DefaultCell.BackgroundColor = Color.WHITE;
//遍历原gridview的数据行
for (int i = 0; i < countRows; i++)
{
for (int j = 0; j < countColumns; j++)
{
table.AddCell(new Phrase(pobjGrdv.Rows[i].Cells[j].Text, font));
}
}
//在目标文档中添加转化后的表数据
document.Add(table);
}
catch (Exception)
{
throw;
}
finally
{
//关闭目标文件
document.Close();
//关闭写入流
writer.Close();
}
// 弹出提示框,提示用户是否下载保存到本地
try
{
//这里是你文件在项目中的位置,根目录下就这么写
String FullFileName = System.Web.HttpContext.Current.Server.MapPath(strFileName);
FileInfo DownloadFile = new FileInfo(FullFileName);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = false;
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename="
+ System.Web.HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
}
catch (Exception)
{
throw;
}
finally
{
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
}
else
{
System.Web.HttpContext.Current.Response.Write
("<script type='text/javascript'>alert('数据为空,不值得导出pdf!');</script>");
}
}
//然后,在要调用转换的按钮的事件代码中调用就可以了
//假设传进去的GridView的名字为GridView1
//假设要保存的文件名为GridView的ID
//假设字体使用simsun (请注意根据你电脑的实际情况来选择目录)
//假设字号选择14
//GridViewToPdf.ConvertGridViewToPdf(this.GridView1,
//this.GridView1.ID.ToString(), "c:\\winnt\\FONTS\\simsun.ttc,1", 14);
#endregion
}
1. 请把上述代码放入编辑器中,由于格式混乱引起问题请自行调整。
2. 外部使用时调用方法:
using System.IO;
using iTextSharp.text;
/// <summary>
/// 导出为pdf
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button_ExportPdf_Click(object sender, EventArgs e)
{
try
{
//假定GridView的ID为GridView_CheckStat
GridView_CheckStat.AllowPaging = false;
GridView_CheckStat.AllowSorting = false;
//从页面取到查询条件
string materialType = this.txtMaterialType.Text;
string depotType = this.DropDownList_DepotType.SelectedValue;
string depotId = this.DropDownList_Depot.SelectedValue;
string goodsName = this.txtGoodsName.Text.Trim();
//填充数据源
GridView_CheckStat.DataSource = CheckStatBll.getCheckStatByCondition(materialType, depotId, depotType,goodsName);
//绑定数据源
GridView_CheckStat.DataBind();
//调用上面写好的转换方法
//将绑定好的GridView传入下面方法
GridViewToPdf.ConvertGridViewToPdf(this.GridView_CheckStat, this.GridView_CheckStat.ID.ToString(), @"c:\WINDOWS\Fonts\msyh.ttf", 8);
}
catch (DocumentException de)
{
Response.Write(de.ToString());
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ajaxtop/archive/2009/10/26/4730174.aspx --------------------编程问答-------------------- 第三方的控件,就是Devexpress的aspxgridview里有一个导出控件,只要用了他的gridview,在加一个他自己的导出控件,就可以导出多种格式文件,不用写代码,就加他的属性就行,
还有就是用水晶报表打印,可以导出简单的报表
itextsharp,不是很难,有一个转换类(有一个转换方法),在页面类里调用一下就行了,都差不多了
啥也不用改 --------------------编程问答-------------------- 用第三方的类库itextSharp
使用 pdfbox
pdfbox
http://topic.csdn.net/u/20091018/15/a71e2dff-85da-4b7a-a38d-09fd9a6f6657.html --------------------编程问答-------------------- www.4ula.com 在线转换还可以插入水印 --------------------编程问答-------------------- 你们项目有钱的话,或者可以用盗版的话,有个第三方组件:
Aspose.Words,去下一个看看。可以直接把word save as pdf。 --------------------编程问答-------------------- 学习。。。 --------------------编程问答-------------------- 高手好多啊 !学习学习! --------------------编程问答-------------------- 没看懂! --------------------编程问答-------------------- 学习了 ~~· --------------------编程问答-------------------- 很有价值,学习了 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 將這個控件部署在你服務器,pdf瀏覽就輕鬆自然
網站 --------------------编程问答-------------------- 服务器上安装一个Office2007 ,然后直接调用word转pdf的方法,很简单。。。
Word.WdSaveFormat.wdFormatPDF; --------------------编程问答-------------------- iTextSharp --------------------编程问答-------------------- abcpdf
可以直接根据URL生成PDF文档. --------------------编程问答-------------------- 如果有钱的话还是考虑ABCPdf,没钱的话可以考虑免费的PDF库 itextsharp,
参考这几篇文章:
http://www.okbase.net/doc/details/83
http://www.okbase.net/doc/details/84
http://www.okbase.net/doc/details/85
补充:.NET技术 , ASP.NET