截断pdf中的数据table

Cut off data in pdf table

我试图在 PDF 打印输出中删除数据的时间部分。它打印的 table 来自 datagridview table 并且是名为 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();

如果您想要 "drop" 不适合的内容,您可以像 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 的字体大小].