用iTextSharp制作pdf

这段时间在作一个澳洲项目,其中有用到iTextSharp生成pdf,以下是部分代码:

首先制作通用表格
1、表格样式

private void AddTableHeader(Table tPay,string[,] PayBody)
        
{
            
int lengthV2=PayBody.GetLength(1);
            Phrase phrase
=null;
            Cell cell
=null;
            
for(int i=0;i<lengthV2;i++)
            
{
                phrase
=new Phrase(PayBody[0,i],FontFactory.GetFont(FontFactory.HELVETICA, 11, Font.NORMAL));
                phrase.Leading
=8;
                cell
=new Cell();
                cell.Add(phrase);
                
if(i==0)
                    cell.HorizontalAlignment
=Element.ALIGN_LEFT;
                
else if(i==lengthV2-1)
                    cell.HorizontalAlignment
=Element.ALIGN_RIGHT;
                tPay.AddCell(cell);
            }

        }
2、表格的头
private void AddTableHeader(Table tPay,string[,] PayBody)
        
{
            
int lengthV2=PayBody.GetLength(1);
            Phrase phrase
=null;
            Cell cell
=null;
            
for(int i=0;i<lengthV2;i++)
            
{
                phrase
=new Phrase(PayBody[0,i],FontFactory.GetFont(FontFactory.HELVETICA, 11, Font.NORMAL));
                phrase.Leading
=8;
                cell
=new Cell();
                cell.Add(phrase);
                
if(i==0)
                    cell.HorizontalAlignment
=Element.ALIGN_LEFT;
                
else if(i==lengthV2-1)
                    cell.HorizontalAlignment
=Element.ALIGN_RIGHT;
                tPay.AddCell(cell);
            }

        }
3、表格的脚
private void AddTableFooter(Table tPay,string[,] PayBody)
        
{

            
int lengthV1=PayBody.GetLength(0);
            
int lengthV2=PayBody.GetLength(1);
            Phrase phrase
=null;
            Cell cell
=null;
            
for(int i=0;i<lengthV2;i++)
            
{
                phrase
=new Phrase(PayBody[lengthV1-1,i],FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.NORMAL));
                phrase.Leading
=8;
                cell
=new Cell();
                cell.Add(phrase);
                cell.Border
=Rectangle.TOP_BORDER | Rectangle.BOTTOM_BORDER;
                cell.BorderWidth
=1;
                
if(i==0)
                    cell.HorizontalAlignment
=Element.ALIGN_LEFT;
                
else if(i==lengthV2-1)
                    cell.HorizontalAlignment
=Element.ALIGN_RIGHT;
                tPay.AddCell(cell);
            }

        }
4、表格的行身
private void AddTableBody(Table tPay,string[,] PayBody)
        
{
            
int lengthV1=PayBody.GetLength(0);
            
int lengthV2=PayBody.GetLength(1);
            Phrase phrase
=null;
            Cell cell
=null;
            
for(int i=1;i<lengthV1-1;i++)
            
{
                
for(int j=0;j<lengthV2;j++)
                
{
                    cell
=new Cell();
                    phrase
=new Phrase(PayBody[i,j],FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.NORMAL));
                    phrase.Leading
=8;
                    cell.Add(phrase);
                    
if(j==0)
                        cell.HorizontalAlignment
=Element.ALIGN_LEFT;
                    
else if(j==lengthV2-1)
                        cell.HorizontalAlignment
=Element.ALIGN_RIGHT;
                
                    tPay.AddCell(cell);
                }

            }

        }
至此,于一段落
下面提供一个数组,以传入的DataTable实现以上代码中形参PayBody的传入值,实为表格的数组化体现
//tGSTBody Array
        private string[,] GSTBody
        
{
            
get
            
{
                
int len=dt.Rows.Count+2;
                
string[,] body=new string[len,6];
                body[
0,0]="Date";
                body[
0,1]="Request ID";
                body[
0,2]="Last name";
                body[
0,3]="Product Type";
                body[
0,4]="Qty";
                body[
0,5]="Rate($)";
                
for(int i=0;i<this.dt.Rows.Count;i++)
                
{
                    
for(int j=0;j<6;j++)
                    
{
                        body[i
+1,j]=dt.Rows[i][j].ToString();
                        
if(j==5)
                            body[i
+1,j]="$"+body[i+1,j];
                    }

                }

                body[len
-1,0]="Total(ex GST)";
                body[len
-1,1]=body[len-1,2]=body[len-1,3]=body[len-1,4]="";
                body[len
-1,5]="$"+this.totalEx;
                
return body;
            }

        }
最后,你只需要在需要调用的地方编写如下代码即可实现pdf生成多个表格,你可以设计多个数组,即可实现多个表格
Table tGST=new Table(6);
            SetTableStyle(tGST,
new int[]{5,3,5,5,2,3},100f);
            AddTableHeader(tGST,GSTBody);
            AddTableBody(tGST,GSTBody);
            AddTableFooter(tGST,GSTBody);
            AddTableExtraFooter(tGST);
            tGST.Offset
=30f;


            
//add body
            document.Add(tPay);
            document.Add(para);
            document.Add(tSum);
            document.Add(tGST);


posted @ 2006-12-08 13:55  山常青  阅读(557)  评论(0编辑  收藏  举报