0

大家好,我正在尝试使用 itext sharp.i 创建一个 pdf以防止最后一个单元格的边框被拖到底部。下面是我的代码

      PdfPTable MainTable = new PdfPTable(2);
      MainTable.ElementComplete = false;
      MainTable.ExtendLastRow = false;
      MainTable.WidthPercentage = 105f;
      float[] widthh = new float[] { 50f, 55f };
      MainTable.SetWidths(widthh);
      MainTable.HorizontalAlignment = Element.ALIGN_CENTER;
     MainTable.SpacingBefore = 5f;

    PdfPTable Itms = new PdfPTable(2);
    float[] width = new float[] { 80f, 20f };
    Itms.SetWidths(width);
    Itms.HorizontalAlignment = iTextSharp.text.Image.ALIGN_TOP;
    Itms.ElementComplete = false;
    Itms.ExtendLastRow = false;
    Itms.DefaultCell.BorderColor = iTextSharp.text.BaseColor.BLACK;
    Itms.DefaultCell.BorderWidthBottom = 1;
    Itms.DefaultCell.BorderWidthTop = 1; 
    PdfPCell PdfPCell = null;

    //Add Header of the pdf table
    PdfPCell = new PdfPCell(new Phrase(new Chunk("Items", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8, iTextSharp.text.BaseColor.BLACK))));
    PdfPCell.Border = iTextSharp.text.Rectangle.BOTTOM_BORDER;
    Itms.AddCell(PdfPCell);

    PdfPCell = new PdfPCell(new Phrase(new Chunk("Orderderd before", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8, iTextSharp.text.BaseColor.BLACK))));
    PdfPCell.Border = iTextSharp.text.Rectangle.BOTTOM_BORDER;
    Itms.AddCell(PdfPCell);



    DataTable Excep = GetDatatable();
    //How add the data from datatable to pdf table
    for (int rows = 0; rows < Excep.Rows.Count; rows++)
    {
        for (int column = 0; column < Excep.Columns.Count; column++)
        {

            if ((column == 0) || (column == 2) || (column == 3))
            {
                PdfPCell = new PdfPCell(new Phrase(new Chunk(Excep.Rows[rows][column].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 6, iTextSharp.text.BaseColor.BLACK))));

                if (rows % 2 != 0)
                    PdfPCell.BackgroundColor = iTextSharp.text.BaseColor.LIGHT_GRAY;    
                Itms.AddCell(PdfPCell);
            }
        }
    }

     MainTable.AddCell(Itms);

在此处输入图像描述

4

0 回答 0