准备工作:
创建Web项目
从Nuget中搜索并添加iTextSharp
添加如下两个引用
using iTextSharp.text;
using iTextSharp.text.pdf;
功能实现实例代码
/// <summary>
/// 创建PDF
/// </summary>
/// <param name="dtSource">数据源</param>
/// <returns></returns>
private bool CreatePDF( DataTable dtSource)
{
try
{
Document doc = new Document(PageSize.A4);//定义一个Document,并设置页面大小为A4,竖向
MemoryStream pdfMS = new MemoryStream();
PdfWriter.GetInstance(doc, pdfMS);//PDF内容放入到流中
doc.Open();
float[] colSet = { 40, 80, 70, 70, 70, 70, 70, 70, 70, 50 };//设定每列宽度
PdfPTable pdfTable = new PdfPTable(colSet.Length); //初始化列数
pdfTable.SetWidthPercentage(colSet, PageSize.A4);
pdfTable.NormalizeHeadersFooters();
pdfTable.SplitLate = false;
PdfPCell pdfPCell;//创建单元格
BaseFont baseFont = BaseFont.CreateFont("C://WINDOWS//Fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//宋体
BaseFont titleBaseFont = BaseFont.CreateFont("C://WINDOWS//Fonts//simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//黑体
Font titleFont = new Font(titleBaseFont, 7);//标题字体
Font contentFont = new Font(baseFont, 6);//正文字体
int horizontalAlignment = Element.ALIGN_CENTER; //水平居中(单元格文字位置)
int verticalAlignment = Element.ALIGN_MIDDLE; //垂直居中(单元格文字位置)
int leftHAlignment = Element.ALIGN_LEFT;//水平居左
#region 创建Table
//标题
pdfPCell = new PdfPCell(new Phrase("创建PDF测试", titleFont));
pdfPCell.Colspan = 10;//合并单元格,默认为1
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
//Table列标题
pdfPCell = new PdfPCell(new Phrase("序号", titleFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("报修编号", titleFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("报修标题", titleFont));
pdfPCell.Colspan = 2;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("提交时间", titleFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("闭环时间", titleFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("报修人", titleFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("故障系统", titleFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("故障等级", titleFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("报修进度", titleFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
int sortNo = 0;//序号
//表格行数据
foreach (DataRow dr in dtSource.Rows)
{
//序号
sortNo++;
pdfPCell = new PdfPCell(new Phrase(sortNo.ToString(), contentFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = leftHAlignment;
pdfTable.AddCell(pdfPCell);
//报修编号
pdfPCell = new PdfPCell(new Phrase(dr[0].ToString(), contentFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = leftHAlignment;
pdfTable.AddCell(pdfPCell);
//报修标题
pdfPCell = new PdfPCell(new Phrase(dr[1].ToString(), contentFont));
pdfPCell.Colspan = 2;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = leftHAlignment;
pdfTable.AddCell(pdfPCell);
//提交时间
pdfPCell = new PdfPCell(new Phrase(dr[2].ToString(), contentFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = leftHAlignment;
pdfTable.AddCell(pdfPCell);
//闭环时间
pdfPCell = new PdfPCell(new Phrase(dr[3].ToString(), contentFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = leftHAlignment;
pdfTable.AddCell(pdfPCell);
//报修人
pdfPCell = new PdfPCell(new Phrase(dr[4].ToString(), contentFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = leftHAlignment;
pdfTable.AddCell(pdfPCell);
//故障系统
pdfPCell = new PdfPCell(new Phrase(dr[5].ToString(), contentFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = leftHAlignment;
pdfTable.AddCell(pdfPCell);
//故障等级
pdfPCell = new PdfPCell(new Phrase(dr[6].ToString(), contentFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = leftHAlignment;
pdfTable.AddCell(pdfPCell);
//报修进度
pdfPCell = new PdfPCell(new Phrase(dr[7].ToString(), contentFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = leftHAlignment;
pdfTable.AddCell(pdfPCell);
}
#endregion
#region 插入图片
pdfPCell = new PdfPCell();
pdfPCell.Border = 0;
pdfPCell.Colspan = 10;
pdfPCell.HorizontalAlignment = leftHAlignment;
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("D:\\test.jpg");
pdfPCell.Image = img; //图片
pdfTable.AddCell(pdfPCell);
#endregion
doc.Add(pdfTable);
doc.NewPage();//分页
doc.Close();
//保存pdf文件
byte[] fileByte = pdfMS.GetBuffer();
string fileName = "D:\\" + Guid.NewGuid().ToString() + ".pdf";
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(fileByte);
bw.Close();
fs.Close();
return true;
}
catch
{
return false;
}
}
---------------------
作者:蓝晶之心
来源:CSDN
原文:https://blog.csdn.net/liwan09/article/details/87367884
版权声明:本文为博主原创文章,转载请附上博文链接!