2

我在下面有这段代码,请帮我在每一页中重复表格标题。我在网上尝试了几个可用的选项,但它们不起作用。我是第一次使用 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();
4

2 回答 2

15

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

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

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

于 2013-02-11T06:49:05.517 回答
2

像这样:

        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);
于 2017-04-01T16:04:51.770 回答