切断pdf表中的数据

时间:2015-04-20 14:50:42

标签: c# winforms pdf datagridview itextsharp

我正在尝试在PDF打印输出中切断数据的时间部分。它打印的表来自datagridview表,是第一个名为 Date column[0])的列。由于删除数据的时间已被证明是一种痛苦,我想知道是否有办法只删除不适合列的数据。我尝试了一个固定的宽度,没有文字换行,但它仍然包裹在它下面的时间。以下是当前的代码。

    private void run_btn_Click(object sender, EventArgs e)
    {          
        SaveFileDialog svg = new SaveFileDialog();
        svg.Filter = "PDF File|*.pdf";
        svg.ShowDialog();

        using (FileStream stream = new FileStream(svg.FileName, FileMode.Create))
        {
            Document doc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
            PdfWriter.GetInstance(doc, stream);
            doc.Open();

            Paragraph paragraph = new Paragraph("" + DateTime.Now.ToShortDateString() +"\n\n");
            doc.Add(paragraph);

            PdfPTable table = new PdfPTable(CK_QA_DataDataGridView.Columns.Count);
            table.DefaultCell.Padding = 5;

            table.WidthPercentage = 95;
            table.HorizontalAlignment = Element.ALIGN_CENTER;
            table.DefaultCell.BorderWidth = 1;
            //Adding Header row

            foreach (DataGridViewColumn column in CK_QA_DataDataGridView.Columns)
            {
                PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
                cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240);
                table.AddCell(cell);
            }
            //Adding DataRow
            foreach (DataGridViewRow row in CK_QA_DataDataGridView.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                    table.AddCell(cell.Value.ToString());
                }
            }
            //Add Table
            doc.Add(table);

            doc.Close();
            stream.Close();

1 个答案:

答案 0 :(得分:1)

如果你想"掉落"如果内容不合适,您可以在CellHeights.cs示例中定义FixedHeight

在该示例中,我们添加了几次我们添加的文本"Dr. iText or:\nHow I Learned to Stop Worrying\nand Love PDF."。由于此文本采用多行,因此单元格的高度默认适应。但是,在一种情况下,我们定义一个固定的高度,如下所示:

cell.FixedHeight = 72f;

在这种情况下,文本被截断,请参阅cell_heights.pdf的第4行作为示例。

在上面的代码段中,72f是用户单位的大小。默认情况下,一个用户单元对应一个点(虽然它们不完全相同)。

其他选项是提供更短的string(但我相信您已经使用ToShortDateString()时执行的操作),或者您可以减少Paragraph的字体大小1}}。

相关问题