2013-02-09 50 views
1

我在下面有下面的代码,请帮我重复每个页面中的表头。我曾尝试在网上提供几个选项,但它们不起作用。我第一次使用ItextSharp。 感谢itextsharp:在所有页面中获取表头

int columns = gv.Columns.Count; 
    int rows = gv.Rows.Count; 
    int tableRows = rows + 3; 
    string flName = ""; 

    if (gv.ID == "gvShares") 
    { 
     flName = "Share_Statement.pdf"; 
    } 

    if (gv.ID == "gvSavingAccStmt") 
    { 
     flName = "Main_Saving_Account_Statement.pdf"; 
    } 

    Document Doc = new Document(); 
    PdfWriter.GetInstance(Doc, Response.OutputStream); 
    Doc.Open(); 

    BaseFont bfTimes = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false); 
    Font times = new Font(bfTimes, 9, Font.NORMAL, Color.BLACK); 

    //string imageFilePath = HttpContext.Current.Server.MapPath("/Image/Sacco_Logo.jpg"); 

    string test = System.Web.HttpContext.Current.Server.MapPath("."); 

    string substr = test.Remove(test.Length - 6); 

    //string imageFilePath = "\\app\\Image\\Sacco_Logo.jpg";//"\\app\\Image\\Sacco_Logo.jpg"; 
    iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(substr + "\\Image\\Sacco_Logo.jpg"); 

    //Resize image depend upon your need 
    jpg.ScaleToFit(75f, 75f); 

    jpg.SpacingAfter = 10f; 

    jpg.Alignment = Element.ALIGN_LEFT; 

    //Doc.Add(jpg); 

    PdfPTable table = new PdfPTable(1); 
    table.DefaultCell.Border = PdfPCell.NO_BORDER; 
    table.WidthPercentage = 100; 

    PdfPCell cell = new PdfPCell(table.DefaultCell); 
    cell.Border = PdfPCell.BOTTOM_BORDER; 
    cell.PaddingBottom = 5; 


    Phrase phrase = new Phrase(); 
    phrase.Add(new Chunk(jpg, 0, 0)); 
    phrase.Add(new Chunk("Kenya Diaspora Sacco"));   
    cell.AddElement(phrase); 
    table.AddCell(cell); 

    Doc.Add(table); 

    Phrase phrNewLine = new Phrase("\n"); 

    Doc.Add(phrNewLine); 

    Paragraph TitlePhrasePh = new Paragraph("Membership Savings Account Statement", times); 
    TitlePhrasePh.SetAlignment("Center");   
    Doc.Add(TitlePhrasePh); 

    Doc.Add(phrNewLine); 

    Paragraph PhUsername = new Paragraph("Name: " + Username, times); 
    PhUsername.SetAlignment("Left"); 
    Doc.Add(PhUsername); 

    Paragraph PhAccount = new Paragraph("Account Number: " + AccountNumber, times); 
    PhAccount.SetAlignment("Left"); 
    Doc.Add(PhAccount); 


    iTextSharp.text.Table gridViewTable = new iTextSharp.text.Table(columns, tableRows); 
    gridViewTable.BorderWidth = 1; 
    gridViewTable.BorderColor = new Color(0, 0, 255); 
    gridViewTable.Cellpadding = 1; 
    gridViewTable.Cellspacing = 1; 

    gridViewTable.AddCell(new Paragraph("Date", times)); 
    gridViewTable.AddCell(new Paragraph("Type", times)); 
    gridViewTable.AddCell(new Paragraph("Description", times)); 

    if (gv.ID == "gvShares") 
    { 
     gridViewTable.AddCell(new Paragraph("Sell[KHSH]", times)); 
    } 

    gridViewTable.AddCell(new Paragraph("Debit[KHSH]", times)); 

    if (gv.ID == "gvShares") 
    { 
     gridViewTable.AddCell(new Paragraph("Buy[KHSH]", times)); 
    } 

    gridViewTable.AddCell(new Paragraph("Credit[KHSH]", times)); 

    if (gv.ID == "gvShares") 
    { 
     gridViewTable.AddCell(new Paragraph("Balance", times)); 
    } 
    else if (gv.ID == "gvSavingAccStmt") 
    { 
     gridViewTable.AddCell(new Paragraph("Balance[KHSH]", times)); 
    } 

    for (int rowCounter = 0; rowCounter < rows; rowCounter++) 
    { 
     for (int columnCounter = 0; columnCounter < columns; columnCounter++) 
     { 
      string strValue = gv.Rows[rowCounter].Cells[columnCounter].Text; 
      gridViewTable.AddCell(new Paragraph(strValue, times)); 
     } 
    } 



    Doc.Add(gridViewTable); 
    Doc.Close(); 
    Response.ContentType = "application/pdf"; 
    Response.AddHeader("content-disposition", "attachment; filename=" + flName); 
    Response.End(); 
+0

[如何重复每一页上pdfpTable的报头(可能重复http://stackoverflow.com/questions/2062983/how-repeat-headers-of-pdfptable-on - 每页) – 2014-01-15 16:51:35

回答

10

您需要使用PdfPTable对象的HeaderRows财产。

PdfPTable table = new PdfPTable(1); 
table.HeaderRows = 1; 

HeaderRows属性设置为一个值n后,下一个n行添加到您的表将被视为标题行,并且将每个新页面上重复。

+0

我试图把相同..但给我奇怪的结果,你可以指出我可以包括代码...通过查看我的代码上面 – 2013-02-12 14:54:46

+0

你想要哪个特定的单元格或单元格是表头? – TimS 2013-02-12 21:56:48

+0

如果您在代码中看到其明确定义的列名称形成第一行... – 2013-02-13 11:50:22

1

像这样:

 PdfPTable table = new PdfPTable(12); 
     table.HeaderRows = 1; /*---->> this property repeats the headers of an iTextSharp PdfPTable on each page */ 
     table.TotalWidth = 900f; 
     table.LockedWidth = true; 
     //table.HorizontalAlignment = 0; 
     float[] widths = new float[] { 20f, 32f, 13f, 24f, 20f, 20f, 30f, 15f, 15f, 20f, 20f, 60f }; 
     table.SetWidths(widths);