Large Table Example

ABCpdf .net

 
   

This example shows how to draw a multi-page table. ABCpdf does not provide table drawing routines itself so this example uses a Table Class to position the table elements.

You can find the full project and classes under the ABCpdf menu item. The project includes code for laying out a small table, a large table spreading over more than one page, an invoice and a product list.

 

   
Setup  

We start by creating our document object and reading the data for our table. For the purposes of this example we'll assume that our data is in a standard tab delimited format.

[C#]
string theText = ReadDataFromFile(theRez + "text7.txt");
Doc theDoc = new Doc();
// set up document
theDoc.FontSize = 12;
theDoc.Rect.Inset(20, 20);

[Visual Basic]
Dim theText As String = ReadDataFromFile(theRez + "text7.txt")
Dim theDoc As Doc = New Doc()
' set up document
theDoc.FontSize = 12
theDoc.Rect.Inset(20, 20)

 

   

Rotate
 

We create a new table object passing in values to tell it what rectangle it can occupy (it takes the current document rectangle) and how many columns of data it should be prepared for.

Columns are assigned relative widths and expand horizontally to fit the table rectangle. Here we're specifying six columns and a number of relative widths. We're padding the cells so there are gaps between the rows and columns. Finally we specify a header which repeats as new pages are added.

[C#]
PDFTable theTable = new PDFTable(theDoc, 6);
// some columns extra width
theTable.SetColumnWidths(new double [] {2, 1, 3 , 2, 1, 4});
theTable.CellPadding = 5;
theTable.RepeatHeader = true;

[Visual Basic]
Dim theTable As PDFTable = New PDFTable(theDoc, 6)
' some columns extra width
theTable.SetColumnWidths(New Double() {2, 1, 3 , 2, 1, 4})
theTable.CellPadding = 5
theTable.RepeatHeader = True

 

   

Add
 

We iterate through the table data adding rows and columns as we go. Every time we add a row we check to see if the page number has changed and restart the shading if it has. This ensures the header is always unshaded. Finally we save the document.

[C#]
theText = theText.Replace("\r\n", "\r");
string[] theRows = theText.Split(new char[] {'\r'});
int thePage = 1;
bool theShade = false;
for (int i = 0; i < theRows.Length; i++) {
  theTable.NextRow();
  string[] theCols = theRows[i].Split(new char[] {'\t'});
  theTable.AddHtml(theCols);
  if (theDoc.PageNumber > thePage) {
    thePage = theDoc.PageNumber;
    theShade = true;
  }
  if (theShade)
    theTable.FillRow("200 200 200", theTable.Row);
  theShade = ! theShade;
}
theDoc.Flatten();
theDoc.Save(Server.MapPath("table2.pdf"));
theDoc.Clear();

[Visual Basic]
theText = theText.Replace(vbCrLf, vbCr)
Dim theRows() As String = theText.Split(New Char() {vbCr})
Dim thePage As Integer = 1
Dim theShade As Boolean = False
For i As Integer = 0 To theRows.Length - 1
  theTable.NextRow()
  Dim theCols As String() = theRows(i).Split(New Char() {vbTab})
  theTable.AddHtml(theCols)
  If theDoc.PageNumber > thePage Then
    thePage = theDoc.PageNumber
    theShade = true
  End If
  If theShade Then
    theTable.FillRow("200 200 200", theTable.Row)
  End If
  theShade = Not theShade
Next
theDoc.Flatten()
theDoc.Save(Server.MapPath("table2.pdf"))
theDoc.Clear()

 

   

Results
 

Using a large quantity of input data. We get the following output.


table2.pdf - [Page 1]


table2.pdf - [Page 2]

table2.pdf - [Page 3]

table2.pdf - [Page 4]

table2.pdf - [Page 5]