簡介和參考文章:
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示例。
定義了一個A4紙張的pdf.頁面顯示距左30,距右30,距上5,距下5。

Chunk chunk = new Chunk("Hello world", FontFactory.GetFont(FontFactory.COURIER, 20, Font.ITALIC, new Color(255, 0, 0)));
document.Add(new Paragraph(chunk));
//構建一個段落實例
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 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;//設置圖片的對其方式。
//定一個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);

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 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
}

...{
// 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
