itextsharp:在所有页面中获取表头

时间:2013-02-09 15:33:45

标签: c# .net 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();

2 个答案:

答案 0 :(得分:11)

您需要使用HeaderRows对象的PdfPTable属性。

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

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

答案 1 :(得分: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);