简介和参考文章:

 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, 303055);

定义了一个A4纸张的pdf.页面显示距左30,距右30,距上5,距下5

iTextSharp学习

打开当前Document
document.Open();
为当前Document添加内容:
 document.Add(new Paragraph("Hello World"));
关闭Document
document.Close();
 
Chunk()是能被添加到(Document文档的文本的最小单位,块可以用于构建其他基础元素如(Paragraph)段落。
创建了一个内容为“hello World”、红色、斜体、COURIER字体、尺寸20的一个块:
iTextSharp学习Chunk chunk = new Chunk("Hello world", FontFactory.GetFont(FontFactory.COURIER, 20, Font.ITALIC, new Color(25500)));
iTextSharp学习
iTextSharp学习document.Add(
new Paragraph(chunk));
iTextSharp学习
 
Paragraph:(段落)段落是一系列块构成,段落有确定的间距。段落可以左对齐、右对齐和居中对齐。添加到文档中的每一个段落将自动另起一行。
iTextSharp学习    //构建一个段落实例
iTextSharp学习
                Paragraph ph1 = new Paragraph();
iTextSharp学习                
//构建块"chunk1"
iTextSharp学习
                Chunk chunk1 = new Chunk("Hello world", FontFactory.GetFont(FontFactory.COURIER, 20, Font.ITALIC, new Color(25500)));
iTextSharp学习                
//向块中追加内容
iTextSharp学习
                chunk1.Append("  Hello Mi");
iTextSharp学习                
//构建块"chunk2"
iTextSharp学习
                Chunk chunk2 = new Chunk(" Word hello", FontFactory.GetFont(FontFactory.COURIER, 20, Font.ITALIC, new Color(2552550)));
iTextSharp学习                
//向块中追加内容
iTextSharp学习
                chunk2.Append("  Hello Mi");
iTextSharp学习                
//将块加入到段落中
iTextSharp学习
                ph1.Add(chunk1);
iTextSharp学习                ph1.Add(chunk2);
iTextSharp学习                
//构建一个图片对像实例
iTextSharp学习
                Image jpg1 = Image.GetInstance("myKids.jpg");
iTextSharp学习                jpg1.Alignment 
= Element.ALIGN_CENTER;
iTextSharp学习                ph1.Add(jpg1);
iTextSharp学习                
//设定段落的间距
iTextSharp学习
                ph1.Leading = 14;
iTextSharp学习                
//段落左对其
iTextSharp学习
                ph1.Alignment = Element.ALIGN_CENTER;
iTextSharp学习                
//将段落添加到pdf文档中显示出来
iTextSharp学习
                document.Add(ph1);
iTextSharp学习
 
 显示效果:
iTextSharp学习
Image:图片对象
       根据媒体文件地址获取Image对象。
       
iTextSharp学习   Image gif = Image.getInstance("vonnegut.gif");
iTextSharp学习                Image jpeg 
= Image.getInstance("myKids.jpg");
iTextSharp学习                Image png 
= Image.getInstance("hitchcock.png");
iTextSharp学习                jpeg.ScalePercent(
10);//将图片按%大小显示。
iTextSharp学习
                jpeg.SetAbsolutePosition(00);// 图片放要页面上一个绝对位置(0,0)
iTextSharp学习
           jpeg.Alignment = Image.ALIGN_TOP;//设置图片的对其方式。
iTextSharp学习

iTextSharp学习
  
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>
iTextSharp学习
iTextSharp学习     //定一个3行,列的表格
iTextSharp学习                
//创建一个表格最通用的办法是预先知道有几行几列:
iTextSharp学习                
//public Table(int columns, int rows); 
iTextSharp学习
                Table tb1 = new Table(33);
iTextSharp学习                
//设定表格的总体宽度
iTextSharp学习
                tb1.AbsWidth = "595";
iTextSharp学习                tb1.Cellpadding 
= 2;
iTextSharp学习                tb1.Cellspacing 
= 3;
iTextSharp学习                
//定义表格各个列的宽度
pdf:
iTextSharp学习
 
PdfPTable:Table对象可以转化成PdfPTable,因为现在的类库的PdfPTable不支持rowspan大于1,所以转化的tablerowspan不能大于1PdfPTable可以浮动在pdf页的任意位置。
iTextSharp学习   PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap0103.pdf", FileMode.Create));
iTextSharp学习                document.Open();
iTextSharp学习            
//定义一个1列2行的table
iTextSharp学习
                Table tb1 = new Table(12);
iTextSharp学习                
//设定表格的总体宽度
iTextSharp学习
                tb1.AbsWidth = "595";
iTextSharp学习                tb1.Cellpadding 
= 2;
iTextSharp学习                tb1.Cellspacing 
= 3;
iTextSharp学习                
//定义表格各个列的宽度
iTextSharp学习
               
iTextSharp学习                tb1.Border 
= Rectangle.NO_BORDER;
iTextSharp学习                tb1.DefaultCellBorder 
= Rectangle.NO_BORDER;
iTextSharp学习                Cell cell 
= new Cell(new Paragraph("kkkkkkkkk"));
iTextSharp学习                System.Drawing.ColorConverter htmColor 
= new System.Drawing.ColorConverter();
iTextSharp学习                Color pdfColor 
= new Color((System.Drawing.Color)htmColor.ConvertFromString("#00CC99"));
iTextSharp学习                cell.BackgroundColor 
= pdfColor;
iTextSharp学习                cell.Leading 
= 14;
iTextSharp学习                tb1.AddCell(cell);
iTextSharp学习                cell 
= new Cell(new Paragraph("bbbbb"));
iTextSharp学习                cell.Leading 
= 14;
iTextSharp学习                tb1.AddCell(cell);
iTextSharp学习                 
//允许转换PdfPTable
iTextSharp学习
                tb1.Convert2pdfptable = true;
iTextSharp学习                
//转换为PdfPTable
iTextSharp学习
                PdfPTable ptbm = tb1.CreatePdfPTable();
IPdfPageEvent:这是一个重要的接口,它定义了的方法有
设定生成pageEvent
PdfWriter writer=PdfWriter.GetInstance(document, new FileStream(FileUrl, FileMode.Create));
       
//页面事件指向IPdfPageEvent接口
        writer.PageEvent = this;

相关文章: