简体   繁体   English

切断pdf表中的数据

[英]Cut off data in pdf table

I'm trying to cut off the time portion of my data in a PDF printout. 我正在尝试截断PDF打印输出中数据的时间部分。 The table it prints is from a datagridview table and is the first column called Date ( column[0] ). 它打印的表来自datagridview表,并且是名为Datecolumn[0] )的第一列。 Since removing the time out of the data has proven to be such a pain, I wondered if there was a way to just cut the data that doesn't fit the column. 由于事实证明,从数据中删除时间非常麻烦,我想知道是否有一种方法可以只剪切不适合该列的数据。 I tried a fixed width and no text wrap but it still wraps the time underneath it. 我尝试了固定的宽度,没有文字换行,但是它仍然包裹着它下面的时间。 Below is the current code. 下面是当前代码。

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

If you want to "drop" content that doesn't fit, you could define a FixedHeight as done in the CellHeights.cs example. 如果要“删除”不合适的内容,则可以像CellHeights.cs示例中那样定义FixedHeight

In that example, we have the text "Dr. iText or:\\nHow I Learned to Stop Worrying\\nand Love PDF." 在该示例中,文本为"Dr. iText or:\\nHow I Learned to Stop Worrying\\nand Love PDF." that we add several times. 我们添加了几次。 As this text takes multiple lines, the height of the cell adapts by default. 由于此文本需要多行,因此默认情况下,单元格的高度会调整。 However, in one case, we define a fixed height like this: 但是,在一种情况下,我们定义了一个固定的高度,如下所示:

cell.FixedHeight = 72f;

In that case, the text is cut off, see row 4 of cell_heights.pdf for an example. 在这种情况下,文本将被截断,有关示例,请参见cell_heights.pdf的第4行。

In the code snippet above, 72f is a size in user units. 在上面的代码段中, 72f是用户单位的大小。 One user unit by default corresponds with a point (although they are not exactly the same). 默认情况下,一个用户单位与一个点相对应(尽管它们并不完全相同)。

Other options would be to provide a shorter string (but I believe that's already what you do when you use ToShortDateString() ), or you can reduce the font size of your Paragraph . 其他选择是提供较短的string (但我相信使用ToShortDateString()时已经这样做了),或者可以减小Paragraph的字体大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM