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

.Net iTextSharp 导出pdf, 包含列表和图片

2018-03-20 17:56 435 查看
//引包iTextSharp,下载iTextSharp,项目引入itextsharp.dll

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

public void SimplePdf() //入口
{
Document doc = new Document(PageSize.A4); //图纸大小A4纸
byte[] bytes = null ;

try
{
BaseFont baseFont = BaseFont.CreateFont("C:\\WINDOWS\\FONTS\\simhei.ttf", BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);  //SIMSUN.TTC:宋体和新宋体
Font ChFont_black = new Font(baseFont, 12);

//PDF内容放入到流中
MemoryStream m = new MemoryStream();
PdfWriter.GetInstance(doc, m);

doc.Open();

//设定每列宽度
float[] w = { 150, 150, 150 };
PdfPTable table = new PdfPTable(w.Length); //初始化列数
table.SetWidthPercentage(w, PageSize.A4);
table.NormalizeHeadersFooters();
table.SplitLate = false;
PdfPCell cell;

#region  一、数据列表

#region 数据列表标题

cell = new PdfPCell(new Phrase("姓名", ChFont_black));  //new Phrase("姓名"); 多个重载方法,可以添加样式等。
cell.Colspan = 1;  //默认为1;可以做合并单元格处理,列合并例如:cell.Colspan = 2 合并两列;行合并例如:cell.Rowspan = 2 合并两行
cell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑
4000
色
cell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
cell.HorizontalAlignment = Element.ALIGN_CENTER;  //水平居中
cell.VerticalAlignment = Element.ALIGN_MIDDLE;   //垂直居中
table.AddCell(cell);

cell = new PdfPCell(new Phrase("性别", ChFont_black));
cell.BackgroundColor = new BaseColor(0, 102, 204);
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
table.AddCell(cell);

cell = new PdfPCell(new Phrase("年龄", ChFont_black));
cell.BackgroundColor = new BaseColor(0, 102, 204);
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
table.AddCell(cell);

#endregion

#region 列表数据

List<UserTest> userTestList = InitUserTestData(); //得到数据

foreach (UserTest info in userTestList)
{
//遍历数据后,第一列
cell = new PdfPCell(new Phrase(info.Name, ChFont_black));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
table.AddCell(cell);

//遍历数据后,第二列
cell = new PdfPCell(new Phrase(info.Sex, ChFont_black));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
table.AddCell(cell);

//遍历数据后,第三列
cell = new PdfPCell(new Phrase(info.Age, ChFont_black));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
table.AddCell(cell);
}

#endregion

//***cell.Table 可以进行table嵌套,适用于多个复杂table, 举例:外层嵌套框架table,  单元格内嵌套table,这样能够有效处理到处内容的结构。

#endregion

#region 二、嵌入图片

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("E:\\wandapos\\02_Project\\14_Web\\03_WandaPOS\\WandaWebSite\\TempDoc\\209054a4-fa46-4882-8c2a-e9918ded954e.jpg"); //图片存放路径

cell = new PdfPCell();
cell.Colspan = 3;
cell.Image = img; //图片
cell.HorizontalAlignment = Element.ALIGN_LEFT;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
table.AddCell(cell);

#endregion

doc.Add(table); //将table元素添加入文档对象
doc.Close();
bytes = m.GetBuffer();
}
catch
{
//异常
}

ExportPDFFile("pdf名称", bytes);

}

public void ExportPDFFile(string fileName, byte[] bytes)  //浏览器下载
{
Encoding encoding;
string outputFileName = null;

//兼容MS/IE,FIREFOX浏览器处理
string browser = HttpContext.Current.Request.UserAgent.ToUpper();
if (browser.Contains("MS") == true && browser.Contains("IE") == true)
{
outputFileName = HttpUtility.UrlEncode(fileName);
encoding = System.Text.Encoding.Default;
}
else if (browser.Contains("FIREFOX") == true)
{
outputFileName = fileName;
encoding = System.Text.Encoding.GetEncoding("GB2312");
}
else
{
outputFileName = HttpUtility.UrlEncode(fileName);
encoding = System.Text.Encoding.Default;
}

//输出到浏览器端。
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentEncoding = encoding;
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.pdf", string.IsNullOrEmpty(outputFileName) ? DateTime.Now.ToString("yyyyMMdd") : outputFileName));
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.End();

}

public List<UserTest> InitUserTestData() //构造数据
{
List<UserTest> userTestList = new List<UserTest>();
UserTest userTestInfo = null;

userTestInfo = new UserTest();
userTestInfo.Name = "张三";
userTestInfo.Sex = "女";
userTestInfo.Age = "18";
userTestList.Add(userTestInfo);

userTestInfo = new UserTest();
userTestInfo.Name = "李四";
userTestInfo.Sex = "男";
userTestInfo.Age = "19";
userTestList.Add(userTestInfo);

userTestInfo = new UserTest();
userTestInfo.Name = "王麻";
userTestInfo.Sex = "男";
userTestInfo.Age = "30";
userTestList.Add(userTestInfo);

return userTestList;
}

public class UserTest {
public string Name { get; set; }
public string Sex { get; set; }
public string Age { get; set; }
}

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