iTextSharp PdfPtable多重 [英] iTextSharp PdfPtable multipage

查看:100
本文介绍了iTextSharp PdfPtable多重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iTextSharp的新手,想要将我的DataGridView转换为PdfPtable。

I am new to iTextSharp, and want to convert my DataGridView to a PdfPtable.

当我将表格写入PDF时,它显示得很好,除非是列数量相对较大。然后它可以通过使列宽变小来实现。

When i write the table to PDF, it shows up nicely, except when the amount of columns is relatively large. Then it accomodates by making the column width smaller.

相反,我想开始一个新页面,显示更多的表格,带有标题。

Instead, I would like to start on a new page, with more of the table shown, with headers.

以下是我的表格代码:

  PdfPTable table = new PdfPTable( view.Columns.Count );

  float[] widths = new float[view.Columns.Count];

  for ( int i = 0; i < view.Columns.Count; i++ ) {
    widths[i] = view.Columns[i].Width;
  }

  table.SetWidths( widths );
  table.HorizontalAlignment = 1; //Center

  PdfPCell cell = null;

  //Headers
  foreach ( DataGridViewColumn c in view.Columns ) {
    cell = new PdfPCell( new Phrase( new Chunk( c.HeaderText, _standardFont ) ) );
    cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
    cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
    table.AddCell( cell );
  }

  //Rest of table
  for ( int i = 1; i < view.Rows.Count; i++ ) {
    for ( int j = 0; j < view.Columns.Count; j++ ) {
      bool hasValue = view.Rows[i].Cells[j].Value == null ? false : true;

      if ( hasValue ) {
        cell = new PdfPCell( new Phrase( view.Rows[i].Cells[j].Value.ToString(), _standardFont ) );
      } else {
        cell = new PdfPCell( new Phrase( "", _standardFont ) );
      }
      cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
      cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
      table.AddCell( cell );
    }
  }

一个解决方案是以某种方式知道有多少列可以在页面上打印全尺寸,然后添加一个新页面,其中显示更多的网格,但我不确定如何检测新页面。这里的问题是似乎没有办法改变PdfPtable的列数,所以我必须能够在预先添加列时计算宽度效果。

One solution would be to somehow know how many columns could be printed fullsize on a page, and then add a new page, with more of the grid displayed there, but i am unsure how to detect a new page. The problem here is that there seems to be no way of changing the number of columns of a PdfPtable, so I have to be able to calculate the effect in width when adding columns beforehand.

编辑:我想垂直拆分页面

I want to split the pages vertically

推荐答案

您面临的问题是PDF不是不是HTML。 PDF中的页面具有固定大小。您没有滚动条,并且在调整查看器窗口大小时内容不会更改。

The problem you're facing, is the fact that PDF isn't HTML. A page in a PDF has a fixed size. You don't have a scrollbar, and the content doesn't change when you resize the viewer window.

我将使用第4章中的JAVA示例回答我的书。如果您需要这些示例的C#版本,则需要此URL: http://tinyurl.com/itextsharpIIA2C04

I'm going to answer using JAVA examples from Chapter 4 of my book. If you need the C# version of these examples, you'll need this URL: http://tinyurl.com/itextsharpIIA2C04

首先:表是否看起来不错是机器无法判断的。这只是人类可以做的事情。

First this: whether or not a table looks nice is something a machine can't judge. It's something only a human can do.

好像你想要控制列的宽度。 ColumnWidths 示例为您提供了有关如何执行此操作的示例。在您的情况下,您应该使用一个解决方案,该解决方案涉及对表的宽度使用绝对值。你会认识到这些解决方案,因为它们都有这个共同点:

Seems like you want to control the width of the columns. The example ColumnWidths gives you some examples on how to do that. In your case, you should use a solution that involves using absolute values for the width of the table. You'll recognize these solutions because they all have this line in common:

table.setLockedWidth(true);

如果要计算每行或整个表的高度,必须使用绝对宽度。一个常见问题是我创建了一个表格,当我要求它的总宽度时,它会返回零。它可以通过一个反问题回答:iText怎么样如果你没有先定义宽度,计算一个表的高度?这是一个修辞问题:在定义宽度之前不可能计算出高度。这显示在 TableHeight 示例中。

Using an absolute width is mandatory if you want to calculate the height of each row or of the complete table. A Frequently Asked Question is "I've created a table and when I ask for its total width, it returns zero." It can be answered by a counter-question: "How can iText calculate the height of a table, if you didn't define the width first?" This is a rhetorical question: it's impossible to calculate the height before the width has been defined. That's shown in the TableHeight example.

一旦定义了表格(及其列)的宽度,就可以计算出高度,并准备好将表格放在绝对位置。 示例展示了它是如何完成的。在此示例中,创建了一个包含4列的表。使用此命令绘制前两个列:

Once you've defined the widths of the table (and its columns), you can calculate the height, and you're ready to position the table at absolute positions. The Zhang example shows how it's done. In this example, a table with 4 columns is created. The first two colums are drawn using this commando:

table.writeSelectedRows(0, 2, 0, -1, 236, 806, canvas);

第一个 0 定义第一列需要绘制, 2 定义未绘制的第一列。其余列在下一页上绘制,使用此命令:

Where the first 0 defines the first column that needs to be drawn, and the 2 defines the first column that isn't drawn. The remaining columns are drawn on the next page, using this commando:

table.writeSelectedRows(2, -1, 0, -1, 36, 806, canvas);

其中 2 定义需要的第一列要绘制, -1 表示应该绘制所有剩余的列。

Where 2 defines the first column that needs to be drawn, and -1 indicates that all remaining columns should be drawn.

在这种情况下, 0 -1 用于行。如果您的表中包含的行数多于一页上的行数,则必须进行数学运算以确保没有任何从页面上掉落的行。

In this case, 0 and -1 are used for the rows. If your table contains more rows than fit on one page, you'll have to do the math to make sure you don't have any rows that "fall off the page."

请注意, 36 806 表示X和Y坐标。当您控制布局时,您有责任计算这些坐标。

Note that the 36 and 806 represent an X and a Y coordinate. As your taking control over the layout, you're responsible to calculate those coordinates.

这篇关于iTextSharp PdfPtable多重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆