简介和参考文章:
iTextSharp是一款开源的PDF操作类库,使用它可以快速的创建PDF文件。
中文参考网站:http://hardrock.cnblogs.com/
http://pdfhome.hope.com.cn/Article.aspx?CID=bf51a5b6-78a5-4fa3-9310-16e04aee8c78&AID=f5fe52dd-8419-4baa-ab1c-ea3f26952132
英文参考网站:http://itext.ugent.be/library/
· 技术文章(http://itext.ugent.be/articles/)
· 在线示例 (http://itextdocs.lowagie.com/tutorial/)
· 英文API(http://itext.ugent.be/library/api/)
iTextSharp常用对象:
Document:(文档)生成pdf必备的一个对象。
生成一个Document示例。
Document document = new Document(PageSize.A4, 30, 30, 5, 5);
定义了一个A4纸张的pdf.页面显示距左30,距右30,距上5,距下5。
打开当前Document
document.Open();
为当前Document添加内容:
document.Add(new Paragraph("Hello World"));
关闭Document
document.Close();
Chunk:(块)是能被添加到(Document)文档的文本的最小单位,块可以用于构建其他基础元素如(Paragraph)段落。
创建了一个内容为“hello World”、红色、斜体、COURIER字体、尺寸20的一个块:
Chunk chunk = new Chunk("Hello world", FontFactory.GetFont(FontFactory.COURIER, 20, Font.ITALIC, new Color(255, 0, 0)));
document.Add(new Paragraph(chunk));
Paragraph:(段落)段落是一系列块构成,段落有确定的间距。段落可以左对齐、右对齐和居中对齐。添加到文档中的每一个段落将自动另起一行。
//构建一个段落实例
Paragraph ph1 = new Paragraph();
//构建块"chunk1"
Chunk chunk1 = new Chunk("Hello world", FontFactory.GetFont(FontFactory.COURIER, 20, Font.ITALIC, new Color(255, 0, 0)));
//向块中追加内容
//构建块"chunk2"
Chunk chunk2 = new Chunk(" Word hello", FontFactory.GetFont(FontFactory.COURIER, 20, Font.ITALIC, new Color(255, 255, 0)));
//向块中追加内容
chunk2.Append(" Hello Mi");
//将块加入到段落中
ph1.Add(chunk1);
ph1.Add(chunk2);
//构建一个图片对像实例
Image jpg1 = Image.GetInstance("myKids.jpg");
jpg1.Alignment = Element.ALIGN_CENTER;
ph1.Add(jpg1);
//设定段落的间距
ph1.Leading = 14;
//段落左对其
ph1.Alignment = Element.ALIGN_CENTER;
//将段落添加到pdf文档中显示出来
document.Add(ph1);
显示效果:
Image:图片对象
根据媒体文件地址获取Image对象。
Image gif = Image.getInstance("vonnegut.gif");
Image jpeg = Image.getInstance("myKids.jpg");
Image png = Image.getInstance("hitchcock.png");
jpeg.ScalePercent(10);//将图片按%大小显示。
jpeg.SetAbsolutePosition(0, 0);// 图片放要页面上一个绝对位置(0,0)
jpeg.Alignment = Image.ALIGN_TOP;//设置图片的对其方式。
Table:(表格)Pdf里面重要的布局对象。
<!---下面演示如何根据htm的<table></table>生成对应的pdf。-->
<table width="595" border="0" cellpadding="3" cellspacing="2">
<tr>
<td colspan="3" ><img src="surfing.gif" /></td>
</tr>
<tr>
<td width="60%" rowspan="2" bgcolor="#00CC99">aaaaaa</td>
<td width="20%" height="48">bbbbb</td>
<td width="20%">cccccc</td>
</tr>
<tr>
<td >dddd</td>
<td>eeeeee</td>
</tr>
</table>
//定一个3行,列的表格
//创建一个表格最通用的办法是预先知道有几行几列:
//public Table(int columns, int rows);
Table tb1 = new Table(3, 3);
//设定表格的总体宽度
tb1.AbsWidth = "595";
tb1.Cellpadding = 2;
tb1.Cellspacing = 3;
//定义表格各个列的宽度
tb1.Widths=new float[]...{60,20,20};
//定义表格的bord的宽度为
//定义表格的bord的宽度为tb1.BorderWidth =1;
tb1.Border = Rectangle.NO_BORDER;
tb1.DefaultCellBorder = Rectangle.NO_BORDER;
Image top1 = Image.GetInstance("surfing.gif");
Cell cell = new Cell(top1);
tb1.AddCell(cell);
cell = new Cell(new Paragraph("aaaaaa"));
cell.Rowspan = 2;
System.Drawing.ColorConverter htmColor = new System.Drawing.ColorConverter();
Color pdfColor=new Color((System.Drawing.Color)htmColor.ConvertFromString("#00CC99"));
cell.BackgroundColor = pdfColor;
cell.HorizontalAlignment = Element.ALIGN_RIGHT;
tb1.AddCell(cell);
cell = new Cell(new Paragraph("bbbbb"));
cell.Leading = 48;
tb1.AddCell(cell);
cell = new Cell(new Paragraph("cccccc"));
tb1.AddCell(cell);
cell = new Cell(new Paragraph("dddd"));
tb1.AddCell(cell);
cell = new Cell(new Paragraph("eeeeee"));
tb1.AddCell(cell);
document.Add(tb1);
pdf:
PdfPTable:Table对象可以转化成PdfPTable,因为现在的类库的PdfPTable不支持rowspan大于1,所以转化的table的rowspan不能大于1,PdfPTable可以浮动在pdf页的任意
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap0103.pdf", FileMode.Create));
document.Open();
//定义一个1列2行的table
Table tb1 = new Table(1, 2);
//设定表格的总体宽度
tb1.AbsWidth = "595";
tb1.Cellpadding = 2;
tb1.Cellspacing = 3;
//定义表格各个列的宽度
tb1.Border = Rectangle.NO_BORDER;
tb1.DefaultCellBorder = Rectangle.NO_BORDER;
Cell cell = new Cell(new Paragraph("kkkkkkkkk"));
System.Drawing.ColorConverter htmColor = new System.Drawing.ColorConverter();
Color pdfColor = new Color((System.Drawing.Color)htmColor.ConvertFromString("#00CC99"));
cell.BackgroundColor = pdfColor;
cell.Leading = 14;
tb1.AddCell(cell);
cell = new Cell(new Paragraph("bbbbb"));
cell.Leading = 14;
tb1.AddCell(cell);
//允许转换PdfPTable
tb1.Convert2pdfptable = true;
//转换为PdfPTable
PdfPTable ptbm = tb1.CreatePdfPTable();
ptbm.WriteSelectedRows(0, -1, document.LeftMargin + 100, document.BottomMargin + 400, writer.DirectContent);
IPdfPageEvent:这是一个重要的接口,它定义了的方法有
IPdfPageEvent Members#region IPdfPageEvent Members
public void OnOpenDocument(PdfWriter writer, Document document)
...{
}
public void OnCloseDocument(PdfWriter writer, Document document)
...{
}
public void OnParagraph(PdfWriter writer, Document document, float paragraphPosition)
// TODO: Add PageNumbersWatermark.OnParagraph implementation
}
public void OnEndPage(PdfWriter writer, Document document)
...{
}
public void OnSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title)
...{
// TODO: Add PageNumbersWatermark.OnSection implementation
}
public void OnSectionEnd(PdfWriter writer, Document document, float paragraphPosition)
...{
// TODO: Add PageNumbersWatermark.OnSectionEnd implementation
}
(PdfWriter writer, Document document, float paragraphPosition)
...{
// TODO: Add PageNumbersWatermark.OnParagraphEnd implementation
}
public void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, string text)
...{
// TODO: Add PageNumbersWatermark.OnGenericTag implementation
}
public void OnChapterEnd(PdfWriter writer, Document document, float paragraphPosition)
...{
// TODO: Add PageNumbersWatermark.OnChapterEnd implementation
}
public void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title)
// TODO: Add PageNumbersWatermark.OnChapter implementation
}
public void OnStartPage(PdfWriter writer, Document document)
...{
}
#endregion
设定生成pageEvent
PdfWriter writer=PdfWriter.GetInstance(document, new FileStream(FileUrl, FileMode.Create));
//页面事件指向IPdfPageEvent接口
writer.PageEvent = this;