将pdfTable的最后一列转换为新行

时间:2015-06-26 19:07:28

标签: c# winforms itextsharp

我想将pdfTable列的最后一列作为新行,并从第二列开始到列的末尾。最后一列太大,无法将所有内容放在同一行上。 我正在使用itexsharp.pdf

**column1 column2 column3
          column4........**

代码:

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

    //Creating iTextSharp Table from the DataTable data
    PdfPTable pdfTable = new PdfPTable(m_PDFColumnCount);
    pdfTable.DefaultCell.Padding = 3;
    pdfTable.WidthPercentage = 100;
    pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
    pdfTable.DefaultCell.BorderWidth = 1;


    this.BuildPDFHeader(pdfTable, "date");
    this.BuildPDFHeader(pdfTable, "time");
    this.BuildPDFHeader(pdfTable, "result");
    this.BuildPDFHeader(pdfTable, "fullname");
    this.BuildPDFHeader(pdfTable, "regarding");
    this.BuildPDFHeader(pdfTable, "datails");

    // add header row

    //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.Colspan = 5;
        cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        pdfTable.AddCell(cell);
    }

    String folderPath = "C:\\PDFs\\"; //should be in configfile.

    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.Rotate());               
        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.Color(240, 240, 240);
    pdfTable.AddCell(cell);
}

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题:

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());
cell = new PdfPCell(new Phrase(dt.Rows[intIndex]["details"].ToString()));
cell.Colspan = 6;
pdfTable.AddCell(cell);