切断pdf表中的数据

乱扔垃圾

我正在尝试截断PDF打印输出中数据的时间部分。它打印的表来自datagridview表,并且是名为Datecolumn[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();
布鲁诺·洛瓦吉(Bruno Lowagie)

如果您想“删除”不适合的内容,则可以FixedHeightCellHeights.cs示例中定义“完成”

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

cell.FixedHeight = 72f;

在这种情况下,文本将被截断,有关示例,请参见cell_heights.pdf的第4行

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

其他选择是提供一个较短的字体string(但我相信使用时已经做到了ToShortDateString()),或者可以减小的字体大小Paragraph

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章