本文给出使用iTextSharp来生成pdf文档的方法。
面是我根据实际情况编写的代码,包含如下:
1、段落
2、表格
3、字体格式化
运行此代码需要的类库:
itextsharp.dll,itext-hyph-xml.dll,iTextAsian.dll和ICSharpCode.SharpZipLib.dll
itextsharp项目地址:http://sourceforge.net/projects/itextsharp/
示例代码,/Files/hudonglin/PDFMake.rar示例代码也贴在下面了
本代码支持中文字符,下面直接上代码
Code [http://www.xueit.com]
namespace PDFMake
{
using System;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
public class PDFITEXTSharp
{
private string _pdfSavePath;
/// <summary>
/// pdf文件保存地址
/// </summary>
public string PDFSavePath
{
get
{
if (string.IsNullOrEmpty(_pdfSavePath))
{
return AppDomain.CurrentDomain.BaseDirectory Guid.NewGuid().ToString() ".pdf";
}
return _pdfSavePath;
}
set { _pdfSavePath = value; }
}
/// <summary>
/// 通过函数深圳pdf保存地址
/// </summary>
/// <param name="pdfSavePath"></param>
public void SetPDFSavePath(string pdfSavePath) { this.PDFSavePath = pdfSavePath; }
#region Demo region
/// <summary>
/// HelloWorld示例程序
/// </summary>
public void HelloWorld()
{
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(PDFSavePath, FileMode.Create));
document.Open();
document.Add(new Paragraph("Hello World"));
document.Close();
}
/// <summary>
/// PDF生成(重载)
/// </summary>
/// <param name="pdfSavePath"></param>
public void MakePDF(string pdfSavePath) { this.PDFSavePath = pdfSavePath; MakePDF(); }
/// <summary>
/// PDF生成
/// </summary>
public void MakePDF()
{
Document document = new Document(PageSize.A4);//创建一个Document实例
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PDFSavePath, FileMode.Create));//创建Writer实例
document.Open();
#region 写入一些数据,包括:作者、标题、摘要、关键词、创建者、生产者、创建时间、头信息
document.AddAuthor("你的名字");
document.AddCreationDate();
document.AddCreator("公司名称");
document.AddHeader("QQ", "你的QQ");
document.AddHeader("Email", "您的邮箱");
document.AddKeywords("物联网");
document.AddProducer();
document.AddSubject("VIP观众入场凭券!");
document.AddTitle("标题");
#endregion
BaseFont baseFont = CreateChineseFont();
iTextSharp.text.Font titleFont = new iTextSharp.text.Font(baseFont, 22, Font.BOLD);
iTextSharp.text.Font fontUnderLine = new iTextSharp.text.Font(baseFont, 12, Font.UNDERLINE);
iTextSharp.text.Font normalFont = new Font(baseFont, 12);
iTextSharp.text.Font normalRedFont = new Font(baseFont, 12, Font.NORMAL | Font.BOLD, BaseColor.RED);
float titleLineHeight = 45f, normalLineHeight = 25f;
Paragraph pBlank = new Paragraph(" ", normalFont);
pBlank.Leading = normalLineHeight;
Image jpeg = Image.GetInstance(AppDomain.CurrentDomain.BaseDirectory "Data\\logo.bmp");
jpeg.Alignment = Element.ALIGN_CENTER;
document.Add(jpeg);
Paragraph titleP = new Paragraph("VIP观众入场凭券", titleFont);
titleP.Leading = titleLineHeight; titleP.Alignment = Element.ALIGN_CENTER;
document.Add(titleP);
Chunk chunk1 = new Chunk("尊敬的", normalFont), chunk2 = new Chunk(" $uName$ ", fontUnderLine)
, chunk3 = new Chunk(":", normalFont);
document.Add(GetPTxt(new Chunk[] { chunk1, chunk2, chunk3 }, normalLineHeight));
document.Add(pBlank);
document.Add(CreateTable());
document.Add(pBlank);//空行
document.Add(GetPTxt("其实生成PDF就是这么简单!!", normalFont, normalLineHeight));
document.Close();
}
#endregion
#region 文档元素生成区域
/// <summary>
/// 生成段落文本
/// </summary>
/// <param name="txt">文本</param>
/// <param name="txtFont">字体</param>
/// <param name="lineHeight">行高</param>
/// <returns>段落文本</returns>
static Paragraph GetPTxt(string txt, Font txtFont, float lineHeight)
{
Paragraph p = new Paragraph(lineHeight, txt, txtFont);
return p;
}
/// <summary>
/// 生成段落文本
/// </summary>
/// <param name="txt">文本</param>
/// <param name="txtFont">字体</param>
/// <param name="lineHeight">行高</param>
/// <param name="elementAlign">对齐方式</param>
/// <returns>段落文本</returns>
static Paragraph GetPTxt(string txt, Font txtFont, float lineHeight, int elementAlign)
{
Paragraph p = new Paragraph(lineHeight, txt, txtFont);
p.Alignment = elementAlign;
return p;
}
/// <summary>
/// 生成段落文本
/// </summary>
/// <param name="chnkArr">Chunk数组</param>
/// <param name="lineHeight">行高</param>
/// <returns>段落文本</returns>
static Paragraph GetPTxt(Chunk[] chnkArr, float lineHeight)
{
if (chnkArr == null || chnkArr.Length == 0) { return new Paragraph(""); }
Paragraph p = new Paragraph();
foreach (Chunk chnkTxt in chnkArr) { p.Add(chnkTxt); }
p.Leading = lineHeight;
return p;
}
/// <summary>
/// 创建Table行
/// </summary>
/// <param name="txt">文本</param>
/// <param name="txtFont">字体</param>
/// <param name="align">对齐方式</param>
/// <param name="colSpan">跨行数</param>
/// <param name="padTop">顶部padding</param>
/// <param name="padBottom">底部padding</param>
/// <returns>Table行</returns>
static PdfPCell CreateCell(string txt, Font txtFont, int align, int colSpan, float padTop, float padBottom)
{
PdfPCell cell = new PdfPCell(new Phrase(txt, txtFont));
if (padTop > 0) { cell.PaddingTop = padTop; }
if (padBottom > 0) { cell.PaddingBottom = padBottom; }
if (colSpan > 0) { cell.Colspan = colSpan; }
cell.HorizontalAlignment = align;
return cell;
}
/// <summary>
/// 创建Table行(RowSpan)
/// </summary>
/// <param name="txt">文本</param>
/// <param name="txtFont">字体</param>
/// <returns>Table行(RowSpan)</returns>
static PdfPCell CreateRowSpanCell(string txt, Font txtFont)
{
PdfPCell cell = new PdfPCell(new Phrase(60f, txt, txtFont));
cell.Rowspan = 6;
cell.PaddingTop = 6f;
cell.PaddingBottom = 6f;
cell.PaddingLeft = 10f;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
return cell;
}
/// <summary>
/// 创建Table行
/// </summary>
/// <param name="txt">文本</param>
/// <param name="txtFont">字体</param>
/// <param name="colSpan">跨行数</param>
/// <param name="padTop">顶部padding</param>
/// <param name="padBottom">底部padding</param>
/// <param name="bgColor">背景色</param>
/// <returns>Table行</returns>
static PdfPCell CreateCellHeader(string txt, Font txtFont, int colSpan, float padTop, float padBottom, BaseColor bgColor)
{
PdfPCell cell = new PdfPCell(new Phrase(txt, txtFont));
if (padTop > 0) { cell.PaddingTop = padTop; }
if (padBottom > 0) { cell.PaddingBottom = padBottom; }
if (colSpan > 0) { cell.Colspan = colSpan; }
cell.HorizontalAlignment = Element.ALIGN_CENTER; //0=Left, 1=Centre, 2=Right
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.BackgroundColor = bgColor;
return cell;
}
/// <summary>
/// 创建表格(PdfPTable)
/// </summary>
/// <returns></returns>
static PdfPTable CreateTable()
{
PdfPTable table = new PdfPTable(6);
table.TotalWidth = 470f;
table.LockedWidth = true;
table.HorizontalAlignment = 1;
float[] widths = new float[] { 95f, 95f, 55f, 40f, 95f, 95f };
table.SetWidths(widths);
BaseFont baseFont = CreateChineseFont();
iTextSharp.text.Font titleFont = new iTextSharp.text.Font(baseFont, 12, Font.BOLD);
iTextSharp.text.Font normalFont = new Font(baseFont, 10), normalBoldFont = new Font(baseFont, 10, Font.BOLD);
iTextSharp.text.Font normalRedFont = new Font(baseFont, 10, Font.NORMAL | Font.BOLD, BaseColor.RED);
float padding = 6f; BaseColor bgColor = new BaseColor(153, 204, 255);
table.AddCell(CreateCellHeader("2010 年深圳物联网技术和应用展览会VIP观众信息", titleFont, 6, 8f, 8f, bgColor));
table.AddCell(CreateCell("姓名", normalBoldFont, 1, 0, padding, padding));
table.AddCell(CreateCell("$uName$", normalFont, 0, 5, padding, padding));
table.AddCell(CreateCell("单位", normalBoldFont, 1, 0, padding, padding));
table.AddCell(CreateCell("$cName$", normalFont, 0, 5, padding, padding));
table.AddCell(CreateCell("联系电话", normalBoldFont, 1, 0, padding, padding));
table.AddCell(CreateCell("$uTel$", normalFont, 0, 2, padding, padding));
table.AddCell(CreateCell("职务", normalBoldFont, 1, 0, padding, padding));
table.AddCell(CreateCell("$uHeadShip$", normalFont, 0, 2, padding, padding));
table.AddCell(CreateCell("VIP编号", normalBoldFont, 1, 0, padding, padding));
table.AddCell(CreateCell("SZRFID-VIP-0001", normalRedFont, 0, 5, padding, padding));
table.AddCell(CreateRowSpanCell("其它参观人员 (如贵公司新增其它参观人员请在此栏填写姓名和联系方式,否则必须在现场排队登记)", normalFont));
table.AddCell(CreateCellHeader("姓名", normalBoldFont, 0, padding, padding, bgColor));
table.AddCell(CreateCellHeader("职务", normalBoldFont, 2, padding, padding, bgColor));
table.AddCell(CreateCellHeader("联系电话", normalBoldFont, 0, padding, padding, bgColor));
table.AddCell(CreateCellHeader("电子邮件", normalBoldFont, 0, padding, padding, bgColor));
for (int i = 0; i < 5; i )
{
table.AddCell(CreateCell(" ", normalFont, 0, 0, padding, padding));
table.AddCell(CreateCell(" ", normalFont, 0, 2, padding, padding));
table.AddCell(CreateCell(" ", normalFont, 0, 0, padding, padding));
table.AddCell(CreateCell(" ", normalFont, 0, 0, padding, padding));
}
return table;
}
#endregion
#region 公共函数区域
/// <summary>
/// 创建中文字体(实现中文)
/// </summary>
/// <returns></returns>
public static BaseFont CreateChineseFont()
{
BaseFont.AddToResourceSearch("iTextAsian.dll");
BaseFont.AddToResourceSearch("iTextAsianCmaps.dll"); //"STSong-Light", "UniGB-UCS2-H",
BaseFont baseFT = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
//iTextSharp.text.Font font = new iTextSharp.text.Font(baseFT);
return baseFT;
}
#endregion
}
}
相关随笔