Splitting a Table Horizontally

Hi,

I am currently testing out the trial version of aspose to see if it suits the purposes of my company.

I was looking at the documentation / code examples and could not figure out of the feature I need is supported:

I was wondering how I could split a very wide table (20+ columns) over two pages -- essentially a horizontal page break or if a workaround is needed, how I can determine that the table is too big for one page, and needs to be recreated in the next page.

I checked the "Isbroken" property of Row/Table, but I have a feeling that's only dealing with the vertical aspect.

A code example would be appreciated.

Hi invincibo,

I think the code list as below can fullfill your requirement, you can use the attribute IsVerticalBroken to control the table whether it is broken. The result PDF file is attached. If you still have some questions, please let me know.

Thanks.

[C#]

Pdf pdf = new Pdf();

Aspose.Pdf.Section sec1 = pdf.Sections.Add();

//Instantiate a table object

//Instantiate a table object

Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();

tab1.IsVerticalBroken = true;

//Add the table in paragraphs collection of the desired section
sec1.Paragraphs.Add(tab1);


//Set default cell border using BorderInfo object
tab1.DefaultCellBorder = new BorderInfo((int)BorderSide.All, 0.1F);

//Set table border using another customized BorderInfo object

tab1.Border = new BorderInfo((int)BorderSide.All, 1F);

//Create MarginInfo object and set its left, bottom, right and top margins

MarginInfo margin = new MarginInfo();

margin.Top = 5f;

margin.Left = 5f;

margin.Right = 5f;

margin.Bottom = 5f;



//Set the default cell padding to the MarginInfo object

tab1.DefaultCellPadding = margin;

//Create rows in the table and then cells in the rows

Aspose.Pdf.Row row1 = tab1.Rows.Add();

for (int i = 0; i <= 20; i++)
{


row1.Cells.Add("col" + i.ToString());
}

pdf.Save(@"d:/table_split.pdf");

awesome. this was exactly what I was looking for. Thanks.

However, I did run to one problem-- I am running this on a local machine with 2 gig memory (in production this will be many times this)-- but for generating tables with 50,000+ cells, i found that i get a systen out of memory exception when I try to create it. Without the feature turned on, it works fine (albeit it doesnt span across multiple pages).

I was wondering if you knew why it takes up so much memory when using this feature?

Thanks

-ian

Hello Ian,

I've afraid I'm unable to reproduce the problem using the code snippet that Alex shared. In fact I'm getting "Index was outside the bounds of the array." issue and we are looking into this matter as well.

Can you please share the code that you are using, so that we can test the scenario at our end.

We apologize for your inconvenience.

Hi,

I didn't use alex's snippet of code -- i merely added the page break = true to my existing code (which uses my company's dataset) to generate the table

essentially, heres the code that does the bulk of the work: -- all i did was create a pdf that adds this to the first section/paragraph of a page and flipped the property to true as he discussed. If you want to reproduce, I would try to populate 30 columns by 3000 = arouind 90,000 cells with some of the dataset being wider so it's forced to be distributed over a few pages.

int indexRow;

foreach (IRowObject rowObject in visibleCols.Rows)

{

indexRow = dataRw + 2;

int col = 0;

row = new Row(table);

row.Border = bi;

if (indexRow % 2 == 0)

row.BackgroundColor = new Aspose.Pdf.Color("LightGray");

table.Rows.Add(row);

foreach (IColumnObject column in visibleCols)

{

Aspose.Pdf.Cell cell = new Aspose.Pdf.Cell(row);

cell.Border = bi;

row.Cells.Add(cell);

Text textCell = new Text();

textCell.TextInfo = ti;

cell.Paragraphs = new Paragraphs();

cell.Paragraphs.Add(textCell);

Segment segTextCell = new Segment(textCell);

textCell.Segments.Add(segTextCell);

segTextCell.Content = rowObject[column.Name];

col++;

}

dataRw++;

}

Hi,

Thanks for the information.

I’ve tested the scenario using the following code snippet and I’m afraid I’m unable to reproduce the problem. Can you please take a look at the following matter and make necessary changes so that we can reproduce the issue.

[C#]

// Instantiate an object PDF class
Pdf pdf = new Pdf();
Aspose.Pdf.Section sec1 = pdf.Sections.Add();

//Instantiate a table object
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
tab1.IsVerticalBroken = true;

//Add the table in paragraphs collection of the desired section
sec1.Paragraphs.Add(tab1);

//Set default cell border using BorderInfo object
tab1.DefaultCellBorder = new BorderInfo((int)BorderSide.All, 0.1F);

//Set table border using another customized BorderInfo object
tab1.Border = new BorderInfo((int)BorderSide.All, 1F);
int indexRow;
  for(int i=0;i<=10;i++)// each (IRowObject rowObject in visibleCols.Rows)
{
   indexRow = i + 2;
   int col = 0;
   Aspose.Pdf.Row row = new Aspose.Pdf.Row(tab1);
   if (indexRow % 2 == 0)
      row.BackgroundColor = new Aspose.Pdf.Color("LightGray");
      tab1.Rows.Add(row);
       for( int j=0; j<=1022;j++)//each (IColumnObject column in visibleCols)
      {
      Aspose.Pdf.Cell cell = new Aspose.Pdf.Cell(row);
      row.Cells.Add(cell);
      Text textCell = new Text();
      cell.Paragraphs = new Paragraphs();
      cell.Paragraphs.Add(textCell);
      Segment segTextCell = new Segment(textCell);
      textCell.Segments.Add(segTextCell);
      segTextCell.Content = "Hello World ("+i.ToString()+","+j.ToString()+")";
      col++;
      }
}
pdf.Save(@"/d:/pdftest/table_split.pdf");

In fact when I increase the value for ‘j’ in the second loop greater than 1022, I’m getting “Index was outside the bounds of the array.” because the maximum number of columns allowed is 1022.

  • The goal is identify what is causing the index out of bounds error.
    • Check the code. Data structure used for columns may be causing the issue.
    • Verify whether data column exceeding 1022 columns limit as index out of bound error often occurs due to exceeding the max defined limit, you can also adjust this setting as well.

Let me now if you need any further assistance.