简体   繁体   English

iTextSharp PdfPtable多重

[英]iTextSharp PdfPtable multipage

I am new to iTextSharp, and want to convert my DataGridView to a PdfPtable. 我是iTextSharp的新手,想要将我的DataGridView转换为PdfPtable。

When i write the table to PDF, it shows up nicely, except when the amount of columns is relatively large. 当我将表格写入PDF时,它会很好地显示,除非列数量相对较大。 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. 相反,我想开始一个新的页面,显示更多的表格,带有标题。

Below is my code for the table: 下面是我的表格代码:

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

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

EDIT: I want to split the pages vertically 编辑:我想垂直拆分页面

The problem you're facing, is the fact that PDF isn't HTML. 您面临的问题是PDF不是HTML。 A page in a PDF has a fixed size. PDF中的页面具有固定大小。 You don't have a scrollbar, and the content doesn't change when you resize the viewer window. 您没有滚动条,并且在调整查看器窗口大小时内容不会更改。

I'm going to answer using JAVA examples from Chapter 4 of my book. 我将使用本书第4章中的JAVA示例回答。 If you need the C# version of these examples, you'll need this URL: http://tinyurl.com/itextsharpIIA2C04 如果您需要这些示例的C#版本,则需要以下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. 这是人类唯一能做的事情。

Seems like you want to control the width of the columns. 好像你想控制列的宽度。 The example ColumnWidths gives you some examples on how to do that. ColumnWidths示例为您提供了有关如何执行此操作的示例。 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);

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?" 它可以通过一个反问题回答: “如果你没有先定义宽度,iText如何计算表格的高度?” 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. 这在TableHeight示例中显示。

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. 在此示例中,将创建一个包含4列的表。 The first two colums are drawn using this commando: 使用此命令绘制前两个列:

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

Where the first 0 defines the first column that needs to be drawn, and the 2 defines the first column that isn't drawn. 第一个0定义了需要绘制的第一列,而第二2定义了未绘制的第一列。 The remaining columns are drawn on the next page, using this commando: 使用此命令将在下一页上绘制剩余的列:

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

Where 2 defines the first column that needs to be drawn, and -1 indicates that all remaining columns should be drawn. 其中2定义了需要绘制的第一列, -1表示应绘制所有剩余列。

In this case, 0 and -1 are used for the rows. 在这种情况下, 0-1用于行。 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." 如果您的表包含的行数多于一页上的行数,则必须进行数学运算以确保没有任何行“脱离页面”。

Note that the 36 and 806 represent an X and a Y coordinate. 注意, 36806表示X和Y坐标。 As your taking control over the layout, you're responsible to calculate those coordinates. 当您控制布局时,您有责任计算这些坐标。

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

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