好库网 好库网首页 | 我的好库
好饱的专栏

用itextsharp将ASP.NET页面导出到PDF中

发布者:好饱
发布日期:2011/5/5 17:10:48   更新日期:2011/5/5 17:11:25
阅读次数:7766
评分:4.80
介绍:本文演示了利用itextsharp库,将ASP.NET页面导出到PDF中的方法。
正文:

 

首先你需要下载itextsharp dll

http://sourceforge.net/projects/itextsharp/

 

下面是示例代码:

 

using iTextSharp.text;
using iTextSharp.text.pdf;

protected void Page_Load(object sender, EventArgs e)
{
ExportToPDF();
}

private void ExportToPDF()
{
Document document = new Document(PageSize.A4, 0, 0, 50, 50);
System.IO.MemoryStream msReport = new System.IO.MemoryStream();

try {
// creation of the different writers
PdfWriter writer = PdfWriter.GetInstance(document, msReport);

// 向文档添加一些元信息
document.AddAuthor("eJuly");
document.AddSubject("Export to PDF");

document.Open();

iTextSharp.text.Table datatable = new iTextSharp.text.Table(7);

datatable.Padding = 2;
datatable.Spacing = 0;

float[] headerwidths = { 6, 20, 32, 18, 8, 8, 8 };
datatable.Widths = headerwidths;

// 第一个单元格横跨7列
Cell cell = new Cell(new Phrase("System Users Report", FontFactory.GetFont(FontFactory.HELVETICA, 16, Font.BOLD)));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Leading = 30;
cell.Colspan = 7;
cell.Border = Rectangle.NO_BORDER;
cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.Gray);
datatable.AddCell(cell);

// 这些单元格横跨2行
datatable.DefaultCellBorderWidth = 1;
datatable.DefaultHorizontalAlignment = 1;
datatable.DefaultRowspan = 2;
datatable.AddCell("No.");
datatable.AddCell(new Phrase("Full Name", FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.NORMAL)));
datatable.AddCell("Address");
datatable.AddCell("Telephone No.");

// This cell spans the remaining 3 columns in 1 row
datatable.DefaultRowspan = 1;
datatable.DefaultColspan = 3;
datatable.AddCell("Just Put Anything");

// These cells span 1 row and 1 column
datatable.DefaultColspan = 1;
datatable.AddCell("Col 1");
datatable.AddCell("Col 2");
datatable.AddCell("Col 3");

datatable.DefaultCellBorderWidth = 1;
datatable.DefaultRowspan = 1;

for (int i = 1; i < 20; i++) {
datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT; 
datatable.AddCell(i.ToString()); 
datatable.AddCell("This is my name."); 
datatable.AddCell("I have a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long address."); 
datatable.AddCell("0123456789"); 

datatable.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
datatable.AddCell("No"); 
datatable.AddCell("Yes"); 
datatable.AddCell("No"); 
} 

document.Add(datatable); 
} 
catch (Exception e) { 
Console.Error.WriteLine(e.Message); 
} 

// 关闭文档
document.Close(); 

Response.Clear(); 
Response.AddHeader("content-disposition", "attachment;filename=Export.pdf"); 
Response.ContentType = "application/pdf"; 
Response.BinaryWrite(msReport.ToArray()); 
Response.End(); 
}

 

 

 


评论 [发表评论]
  • 动点脑子OK?  这特么叫把页面导出PDF?

    gobbs123

    2015/6/10 16:34:16


  • 更多评论
账号 密码 还没帐号呢,现在注册一个?

免责声明:好库网所展示的信息由买卖双方自行提供,其真实性、准确性和合法性由信息发布人负责。好库网不提供任何保证,并不承担任何法律责任。