短语文字对于一页来说太大了

时间:2015-06-28 03:43:27

标签: c# pdf itext

我有一个像下面这样的PDfTable,我的问题是如果短语文本太大而在标题和短语之间留空。我如何绑定在一起,所以如果短语文本对于一个页面来说太大而不适合你在同一页面上的文本和标题文本的其余部分你可以从新页面开始。

问题:

.............................
. header1 : header2 :header3: Page1 // here I want to fit the phrase as much as can if phrase needs new page than put rest of the text on the next page
.                           :
. phrase too big to fit here:
............................: 

.............................
. phrase starts here        . Page2  
.                           .
.............................

    private String WritePDF(DataTable dt)
    {
        String fileName = "";  

        //Creating iTextSharp Table from the DataTable data
        PdfPTable pdfTable = new PdfPTable(m_PDFColumnCount);

        pdfTable.DefaultCell.Padding = 1;
        pdfTable.WidthPercentage = 100;
        pdfTable.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
        pdfTable.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(194, 214, 155);
       //pdfTable.DefaultCell.BorderWidth = 1;




        this.BuildPDFHeader(pdfTable, "DATE");
        this.BuildPDFHeader(pdfTable, "TIME");
        this.BuildPDFHeader(pdfTable, "RESULT");
        this.BuildPDFHeader(pdfTable, "FULLNAME");
        this.BuildPDFHeader(pdfTable, "REGARDING");            




        //Adding DataRow
            for (int intIndex = 0; intIndex < dt.Rows.Count; intIndex++)
            {

              dt.Rows[intIndex]["details"] = getplaintext(dt.Rows[intIndex]["details"].ToString());

              pdfTable.AddCell(dt.Rows[intIndex]["date"].ToString());
              pdfTable.AddCell(dt.Rows[intIndex]["time"].ToString());
              pdfTable.AddCell(dt.Rows[intIndex]["result"].ToString());
              pdfTable.AddCell(dt.Rows[intIndex]["fullname"].ToString());
              pdfTable.AddCell(dt.Rows[intIndex]["regarding"].ToString());

              PdfPCell cell = new PdfPCell(new Phrase(dt.Rows[intIndex]["details"].ToString()));
              cell.BackgroundColor = new iTextSharp.text.BaseColor(227, 234, 235);
              cell.Colspan = 5;
              pdfTable.AddCell(cell);                 

            }


        //String folderPath = ConfigurationManager.AppSettings["Path"].ToString();

        String folderPath = "C:\\PDFs\\";

        fileName =  String.Format("{0}{1}{2}",folderPath, dt.Rows[0]["id"].ToString(),".pdf" );

        //Exporting to PDF

        if (!Directory.Exists(folderPath))
        {
            Directory.CreateDirectory(folderPath);
        }
        using (FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate ))
        {
            Document pdfDoc = new Document(PageSize.A4, 20, 20, 20, 20);               
            PdfWriter.GetInstance(pdfDoc, stream);
            pdfDoc.Open();
            pdfDoc.Add(pdfTable);
            pdfDoc.Close();
            stream.Close();
        }

        return fileName;

    }

    private void BuildPDFHeader( PdfPTable pdfTable, String strText)
    {
          PdfPCell cell = new PdfPCell(new Phrase(strText)); 
                cell.BackgroundColor = new iTextSharp.text.BaseColor(51, 102,102);
                pdfTable.AddCell(cell);
    }

1 个答案:

答案 0 :(得分:1)

在定义表格后立即引入以下行:

{{1}}

这将提前拆分行。在这种情况下,只有当一行占用整个页面的空间时才会拆分行。