iTextSharp PdfPtable multipage

2019-07-29 07:34发布

问题:

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

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.

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. 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.

EDIT: I want to split the pages vertically

回答1:

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.

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.

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

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.

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

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

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

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."

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.