er ser
er ser

Reputation: 41

phrase text too big for one page

I have a PDfTable like below, my problem is if Phrase text too big than it makes blank line between header and phrase. How can I bound together so if phrase text too big for one page than fit what ever text you can on the same page with headers and rest of the phrase text you can start with new page.

problem:

.............................
. 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  
.                           .
.............................

code:

    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);
    }

Upvotes: 0

Views: 323

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

Introduce the following line right after you define the table:

pdfTable.SplitLate = false;

This will split rows early. In you case, the rows are split only if a row takes up the space of an entire page.

Upvotes: 1

Related Questions