簡介和參考文章:
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)));
//向塊中追加內容
chunk1.Append(" Hello Mi");
//構建塊"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);
cell.Colspan = 3;
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.SetTotalWidth(new float[] ...{595});
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
}
public void OnParagraphEnd(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;