iTextSharp学习

 

简介和参考文章:

 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/

·  英文APIhttp://itext.ugent.be/library/api/

iTextSharp常用对象:

Document:(文档)生成pdf必备的一个对象。

生成一个Document示例。

Document document = new Document(PageSize.A4, 303055);

定义了一个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 :(表格) P df 里面重要的布局对象。
<!-- -下面演示如何根据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:T able 对象可以转化成 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

设定生成pageEvent
PdfWriter writer = PdfWriter.GetInstance(document,  new  FileStream(FileUrl, FileMode.Create));
       
// 页面事件指向IPdfPageEvent接口
        writer.PageEvent  =   this ;

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值