Keep tables together in a page

Hi,


I am using aspose pdf 4.5 for java.I have table1 and table2 .
Scenario: Table1 overflows to next page.But I need to keep these two tables in a single page so that these two table fit in a single page.

1) Do you an API to find out if table breaks in a current page ?.
The solution here is not working for me .How to determine if table will break in current page
2) Do you have any API to restrict these two tables in a single page. The API table.isKeptWithNext(true) is not working.
3) Do you have any API method to insert a text at the top of overflowed pages,for example continued at the top of page before heading
Please help.

Thanks

Hi Basil,

We can determine /get table height information once the table has been rendered and PDF is saved over system. But I am afraid we cannot get the table height information before the file has been saved. And until we are able to get the table height information, we cannot compare it with page height, so that we can have an idea if it will be accommodated in current page.

  1. Do you have any API to restrict these two tables in a single page. The API table.isKeptWithNext(true) is not working.
    Can you please share the current code which you are using so that we can test the scenario at our end.
  1. Do you have any API method to insert a text at the top of overflowed pages,for example continued at the top of page before heading
    You may consider adding Text Stamp of placing Header inside document. For further details, please visit

Issue 3: Text stamp will not work as this is bug in Aspose

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.aspose.pdf.CellBorderStyle;
import com.aspose.pdf.Color;
import com.aspose.pdf.Document;
import com.aspose.pdf.Image;
import com.aspose.pdf.License;
import com.aspose.pdf.MarginInfo;
import com.aspose.pdf.Page;
import com.aspose.pdf.PageCollection;
import com.aspose.pdf.PageInfo;
import com.aspose.pdf.Paragraphs;


public class TestKeepTablesNextPage {
/**

* @param args

* @throws FileNotFoundException

*/
public static void main(String[] args) throws FileNotFoundException {
java.util.Locale locale1 = new java.util.Locale("en");
java.util.Locale.setDefault(locale1);
License lic = new License();
lic.setLicense(new FileInputStream(
"D:\\Workspaces\\Examples\\Aspose\\src\\conf\\Aspose.Total.Java.lic"));
// Added document
Document doc = new com.aspose.pdf.Document();
PageInfo pageInfo = doc.getPageInfo();
MarginInfo marginInfo = pageInfo.getMargin();
marginInfo.setLeft(37);
marginInfo.setRight(37);
marginInfo.setTop(37);
marginInfo.setBottom(37);
pageInfo.isLandscape(true);
com.aspose.pdf.Table table = new com.aspose.pdf.Table();
table.setColumnWidths("50 100");

// added page.
Page curPage = doc.getPages().add();
for(int i=1;i<=120;i++){
CellBorderStyle style = new CellBorderStyle(Color.getRed());
style.setLineWidth(2f);
com.aspose.pdf.Row row = table.getRows().add();
row.setFixedRowHeight(15);

com.aspose.pdf.Cell cell1 = row.getCells().add();
//cell1.setBorder(new BorderInfo(BorderSide.All,style));
cell1.getParagraphs().add(new com.aspose.pdf.TextFragment("Content 1"));
com.aspose.pdf.Cell cell2 = row.getCells().add();
//cell2.setBorder(new BorderInfo(BorderSide.All,style));
cell2.getParagraphs().add(new com.aspose.pdf.TextFragment("HHHHH"));
}
Paragraphs paragraphs = curPage.getParagraphs();
paragraphs.add(table);
/********************************************/
com.aspose.pdf.Table table1 = new com.aspose.pdf.Table();
table.setColumnWidths("100 100");
for(int i=1;i<=10;i++){
CellBorderStyle style = new CellBorderStyle(Color.getRed());
style.setLineWidth(2f);

com.aspose.pdf.Row row = table1.getRows().add();
com.aspose.pdf.Cell cell1 = row.getCells().add();
//cell1.setBorder(new BorderInfo(BorderSide.All,style));
cell1.getParagraphs().add(new com.aspose.pdf.TextFragment("LAAAAAAA"));

com.aspose.pdf.Cell cell2 = row.getCells().add();
//cell2.setBorder(new BorderInfo(BorderSide.All,style));
cell2.getParagraphs().add(new com.aspose.pdf.TextFragment("LAAGGGGGG"));
}

com.aspose.pdf.Table table2 = new com.aspose.pdf.Table();

table.setColumnWidths("100 100");

for(int i=1;i<=22;i++){

CellBorderStyle style = new CellBorderStyle(Color.getRed());
style.setLineWidth(2f);
com.aspose.pdf.Row row = table2.getRows().add();
com.aspose.pdf.Cell cell1 = row.getCells().add();
//cell1.setBorder(new BorderInfo(BorderSide.All,style));
cell1.getParagraphs().add(new com.aspose.pdf.TextFragment("ffffff"));
com.aspose.pdf.Cell cell2 = row.getCells().add();
//cell2.setBorder(new BorderInfo(BorderSide.All,style));
cell2.getParagraphs().add(new com.aspose.pdf.TextFragment("vvvvvvvvvvvvvv"));
}
// i want to keep table 1 to next page please...
paragraphs.add(table1);
table1.isKeptWithNext(true);
paragraphs.add(table2);
doc.save("tableOP.pdf");
}

private static com.aspose.pdf.Table getSampleTable(String content1,
String content2) {
com.aspose.pdf.Table table = new com.aspose.pdf.Table();
com.aspose.pdf.Row row = table.getRows().add();
com.aspose.pdf.Cell cell1 = row.getCells().add();
cell1.getParagraphs().add(new com.aspose.pdf.TextFragment(content1));
com.aspose.pdf.Cell cell2 = row.getCells().add();
cell2.getParagraphs().add(new com.aspose.pdf.TextFragment(content2));
return table;
}
}

Hi Basil,


Thanks for sharing the code snippet.

Please note that by default, the contents are added in Flow layout (Top-Left to Bottom-Right) in PDF document. However as per my understanding, your requirement is to render the table1 in new page rather than it appears with table object on page 4. If so is the case, then I am afraid currently Aspose.Pdf for Java does not support the feature to force break the table and render it in new page. For the sake of correction, I have logged this problem
as PDFNEWJAVA-34070 in our issue tracking system. We will further
look into the details of this problem and will keep you updated on the status
of correction. Please be patient and spare us little time. We are sorry for
this inconvenience.

Hi,

I have not go a reply till now for issue 1.I am not able to determine if table breaks in a current page.I had followed the code here [How
to determine if table will break in current page]

Please see the code below and provide an answer.

I have come across eight issues in aspose pdf java till now and I am bit worried ,doubts your release (aspose 4.5 ) is production ready

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import com.aspose.pdf.CellBorderStyle;

import com.aspose.pdf.Color;

import com.aspose.pdf.Document;

import com.aspose.pdf.Image;

import com.aspose.pdf.License;

import com.aspose.pdf.MarginInfo;

import com.aspose.pdf.Page;

import com.aspose.pdf.PageCollection;

import com.aspose.pdf.PageInfo;

import com.aspose.pdf.Paragraphs;

public class TestKeepTablesTogether {

/

*** @param args**

*** @throws FileNotFoundException**

***/**

**public static void main(String[] args) throws FileNotFoundException {**

**java.util.Locale locale1 = new java.util.Locale(“en”);**

**java.util.Locale.setDefault(locale1);**

**License lic = new License();**

**lic.setLicense(new FileInputStream(**

**“D:\Workspaces\Examples\Aspose\src\conf\Aspose.Total.Java.lic”));**

**// Added document**

**Document doc = new com.aspose.pdf.Document();**

**PageInfo pageInfo = doc.getPageInfo();**

**MarginInfo marginInfo = pageInfo.getMargin();**

**marginInfo.setLeft(37);**

**marginInfo.setRight(37);**

**marginInfo.setTop(37);**

**marginInfo.setBottom(37);**

**pageInfo.isLandscape(true);**

**com.aspose.pdf.Table table = new com.aspose.pdf.Table();**

**table.setColumnWidths(“50 100”);**

**// added page.**

**Page curPage = doc.getPages().add();**

**for(int i=1;i<=120;i++){**

**CellBorderStyle style = new CellBorderStyle(Color.getRed());**

**style.setLineWidth(2f);**

**com.aspose.pdf.Row row = table.getRows().add();**

**row.setFixedRowHeight(15);**

**com.aspose.pdf.Cell cell1 = row.getCells().add();**

**[//cell1.setBorder](https://cell1.setborder/)(new BorderInfo(BorderSide.All,style));**

**cell1.getParagraphs().add(new com.aspose.pdf.TextFragment(“Content 1”));**

**com.aspose.pdf.Cell cell2 = row.getCells().add();**

**[//cell2.setBorder](https://cell2.setborder/)(new BorderInfo(BorderSide.All,style));**

**cell2.getParagraphs().add(new com.aspose.pdf.TextFragment(“HHHHH”));**

**}**

**Paragraphs paragraphs = curPage.getParagraphs();**

**paragraphs.add(table);**

**/********************************************/

com.aspose.pdf.Table table1 = new com.aspose.pdf.Table();

table.setColumnWidths(“100 100”);

for(int i=1;i<=10;i++){

CellBorderStyle style = new CellBorderStyle(Color.getRed());

style.setLineWidth(2f);

com.aspose.pdf.Row row = table1.getRows().add();

com.aspose.pdf.Cell cell1 = row.getCells().add();

[//cell1.setBorder](https://cell1.setborder/)(new BorderInfo(BorderSide.All,style));

cell1.getParagraphs().add(new com.aspose.pdf.TextFragment(“LAAAAAAA”));

com.aspose.pdf.Cell cell2 = row.getCells().add();

[//cell2.setBorder](https://cell2.setborder/)(new BorderInfo(BorderSide.All,style));

cell2.getParagraphs().add(new com.aspose.pdf.TextFragment(“LAAGGGGGG”));

}

com.aspose.pdf.Table table2 = new com.aspose.pdf.Table();

table.setColumnWidths(“100 100”);

for(int i=1;i<=22;i++){

CellBorderStyle style = new CellBorderStyle(Color.getRed());

style.setLineWidth(2f);

com.aspose.pdf.Row row = table2.getRows().add();

com.aspose.pdf.Cell cell1 = row.getCells().add();

[//cell1.setBorder](https://cell1.setborder/)(new BorderInfo(BorderSide.All,style));

cell1.getParagraphs().add(new com.aspose.pdf.TextFragment(“ffffff”));

com.aspose.pdf.Cell cell2 = row.getCells().add();

[//cell2.setBorder](https://cell2.setborder/)(new BorderInfo(BorderSide.All,style));

cell2.getParagraphs().add(new com.aspose.pdf.TextFragment(“vvvvvvvvvvvvvv”));

}

// get the Page Height information

double PageHeight = doc.getPageInfo().getHeight();

// get the total height information of Page Top & Bottom margin,

// table Top margin and table height.

double TotalObjectsHeight = doc.getPageInfo().getMargin().getTop() + doc.getPageInfo().getMargin().getBottom()

+ table1.getMargin().getTop() +table1.getMargin().getBottom() + table1.getHeight();

//+ table.getMargin().getTop() +table.getMargin().getBottom() + table.getHeight();

// Display Page Height, Table Height, table Top margin and Page Top

// and Bottom margin information

System.out.println("PDF document Height = " + doc.getPageInfo().getHeight()

+ "\nTop Margin Info = " + doc.getPageInfo().getMargin().getTop()

+ "\nBottom Margin Info = " + doc.getPageInfo().getMargin().getBottom()

+ "\n\nTable-Top Margin Info = " + table1.getMargin().getTop()

+ "\nAverage Row Height = " + table1.getRows().get_Item(0).getFixedRowHeight()

+ " \nTable height " + table1.getHeight() + “\n ----------------------------------------”

+ “\nTotal Page Height =” + PageHeight

+ “\nCumulative height including Table =” + TotalObjectsHeight);

// check if we deduct the sum of Page top margin + Page Bottom margin

// + Table Top margin and table height from Page height and its less

// than 10 (an average row can be greater than 10)

System.out.println("Calculated "+(PageHeight - TotalObjectsHeight));

if ((PageHeight - TotalObjectsHeight) < 10){

System.out.println(“Page is breaking”);

}

paragraphs.add(table1);

paragraphs.add(table2);

doc.save(“tableOP.pdf”);

}

}
Hi Basil,

I am testing the scenario and will share my findings shortly.

Hi Basil,


Thanks for your patience.

I have tested the above stated code with Aspose.Pdf for Java 4.6.0 and when providing larger value (i.e. 100) for loop inside table1, correct value is being returned (either the table exceeds page height or not). However as stated earlier, even the table exceeds page height, it prolongs to subsequent page and it does not break. Nevertheless, we already have logged a requirement to force break the table in current page so once this feature is implemented, the subsequent table will be force broken so it starts appearing from next page. We are sorry for your inconvenience.

Hi,


I think I’m having this same issue. I am looping around an array of objects and for each one creating a table, however if the table happens to render across two pages it doesn’t break and render the whole table on the next page.

I’ve tried setting isBroken to false but that hasn’t had any effect.

Is this still an issue with Aspose PDF for java? I’m using version 4.4

This is an issue in aspose (I am using aspose 4.6).I have to change my requirement to proceed with development.

You can ask aspose support team for a resolution

Please go through my recents posts too.It might be helpful for you
http://www.aspose.com/community/members/basil%20c%20paulose.aspx



cjh23:
I think I’m having this same issue. I am looping around an array of objects and for each one creating a table, however if the table happens to render across two pages it doesn’t break and render the whole table on the next page.

I’ve tried setting isBroken to false but that hasn’t had any effect.

Is this still an issue with Aspose PDF for java? I’m using version 4.4
Hi Chris,

Thanks for contacting support.

I have tested the above stated scenario using following code snippet where I have used Aspose.Pdf for Java 4.6.0 and I am unable to notice any problem. The table is properly appearing in two pages. For your reference, I have also attached the resultant PDF generated over my end. Can you please try using the latest release and in case the problem still persists, please share the code snippet. We are sorry for your inconvenience.

[Java]

// Added document<o:p></o:p>

Document doc = new com.aspose.pdf.Document();

com.aspose.pdf.PageInfo pageInfo = doc.getPageInfo();

com.aspose.pdf.MarginInfo marginInfo = pageInfo.getMargin();

marginInfo.setLeft(37);

marginInfo.setRight(37);

marginInfo.setTop(37);

marginInfo.setBottom(37);

pageInfo.isLandscape(true);

com.aspose.pdf.Table table = new com.aspose.pdf.Table();

table.setColumnWidths("50 100");

// added page.

Page curPage = doc.getPages().add();

for (int i = 1; i <= 50; i++)

{

com.aspose.pdf.Row row = table.getRows().add();

row.setFixedRowHeight(15);

com.aspose.pdf.Cell cell1 = row.getCells().add();

cell1.getParagraphs().add(new com.aspose.pdf.TextFragment("Content 1"));

com.aspose.pdf.Cell cell2 = row.getCells().add();

cell2.getParagraphs().add(new com.aspose.pdf.TextFragment("HHHHH"));

}

com.aspose.pdf.Paragraphs paragraphs = curPage.getParagraphs();

paragraphs.add(table);

doc.save(“c:/pdftest/tableOP.pdf”);

Hi,

Thanks for your reply. I have updated to 4.6 but I am still experiencing my issue, and I feel as though you haven’t fully understood it. The code you supplied does work, because it’s all one table with a load of rows in it and it does break over two pages. Fine.

The problem I am having that is different is that I am looping around an array of objects and creating lots of tables, of which each one’s information needs to be kept together and displayed on the same page.

Here’s a screenshot of what I mean followed by my code.

http://screencast.com/t/5qhe1xIVv

The block in yellow highlights one table, it currently spans over two pages, however I need Aspose to recognise this, and instead draw the whole table together on the second page.

{
final Document doc = new Document();

final com.aspose.pdf.PageInfo pageInfo = doc.getPageInfo();

final com.aspose.pdf.MarginInfo marginInfo = pageInfo.getMargin();



marginInfo.setLeft(30);

marginInfo.setRight(30);

marginInfo.setTop(30);

marginInfo.setBottom(30);



final Page curPage = doc.getPages().add();



final com.aspose.pdf.Table saleInfoHeaderTable = new com.aspose.pdf.Table();

saleInfoHeaderTable.setColumnWidths("70, 80, 80, 110, 30, 55, 55, 55");



final com.aspose.pdf.Row saleInfoHeaderRow = saleInfoHeaderTable.getRows().add();



final com.aspose.pdf.Cell idHeader = saleInfoHeaderRow.getCells().add();

idHeader.getParagraphs().add(new com.aspose.pdf.TextFragment("ID"));



final com.aspose.pdf.Cell refHeader = saleInfoHeaderRow.getCells().add();

refHeader.getParagraphs().add(new com.aspose.pdf.TextFragment("Reference"));



final com.aspose.pdf.Cell dateHeader = saleInfoHeaderRow.getCells().add();

dateHeader.getParagraphs().add(new com.aspose.pdf.TextFragment("Date"));



final com.aspose.pdf.Cell descHeader = saleInfoHeaderRow.getCells().add();

descHeader.getParagraphs().add(new com.aspose.pdf.TextFragment("Description"));



final com.aspose.pdf.Cell vatCodeHeader = saleInfoHeaderRow.getCells().add();

vatCodeHeader.getParagraphs().add(new com.aspose.pdf.TextFragment("VATCode"));



final com.aspose.pdf.Cell netHeader = saleInfoHeaderRow.getCells().add();

netHeader.getParagraphs().add(new com.aspose.pdf.TextFragment("Net"));



final com.aspose.pdf.Cell vatHeader = saleInfoHeaderRow.getCells().add();

vatHeader.getParagraphs().add(new com.aspose.pdf.TextFragment("VAT"));



final com.aspose.pdf.Cell totalHeader = saleInfoHeaderRow.getCells().add();

totalHeader.getParagraphs().add(new com.aspose.pdf.TextFragment("Total"));

curPage.getParagraphs().add(saleInfoHeaderTable);



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

final com.aspose.pdf.Table saleInfoTable = new com.aspose.pdf.Table();

saleInfoTable.setColumnWidths("70, 80, 80, 110, 30, 55, 55, 55");



final com.aspose.pdf.Row saleRow = saleInfoTable.getRows().add();

final com.aspose.pdf.Cell id = saleRow.getCells().add();

id.getParagraphs().add(new com.aspose.pdf.TextFragment(String.valueOf(i)));



final com.aspose.pdf.Cell externalRef = saleRow.getCells().add();

externalRef.getParagraphs().add(new com.aspose.pdf.TextFragment("test reference"));



final com.aspose.pdf.Cell saleDate = saleRow.getCells().add();

saleDate.getParagraphs().add(new com.aspose.pdf.TextFragment("01/01/2014"));



final com.aspose.pdf.Cell invoiceElementCell = saleRow.getCells().add();

invoiceElementCell.getParagraphs().add(new com.aspose.pdf.TextFragment("test description"));



final com.aspose.pdf.Cell taxCode = saleRow.getCells().add();

taxCode.getParagraphs().add(new com.aspose.pdf.TextFragment("Z"));



final BigDecimal netTotalOfSaleLines = new BigDecimal("0").setScale(2);

final BigDecimal vatTotalOfSaleLines = new BigDecimal("0").setScale(2);

final BigDecimal totalOfSaleLines = new BigDecimal("0").setScale(2);



final com.aspose.pdf.Cell net = saleRow.getCells().add();

net.getParagraphs().add(new com.aspose.pdf.TextFragment(netTotalOfSaleLines.toString()));



final com.aspose.pdf.Cell vat = saleRow.getCells().add();

vat.getParagraphs().add(new com.aspose.pdf.TextFragment(vatTotalOfSaleLines.toString()));



final com.aspose.pdf.Cell total = saleRow.getCells().add();

total.getParagraphs().add(new com.aspose.pdf.TextFragment(totalOfSaleLines.toString()));



for (int j = 0; j < 5; j++) {



final com.aspose.pdf.Row narrativeInfoRow = saleInfoTable.getRows().add();



final com.aspose.pdf.Cell details = narrativeInfoRow.getCells().add();

details.getParagraphs().add(new com.aspose.pdf.TextFragment(j == 0 ? "Details: " : ""));

details.setColSpan(1);



final com.aspose.pdf.Cell key = narrativeInfoRow.getCells().add();

key.getParagraphs().add(new com.aspose.pdf.TextFragment(" " + "key " + j));

key.setColSpan(2);



final com.aspose.pdf.Cell value = narrativeInfoRow.getCells().add();

value.getParagraphs().add(new com.aspose.pdf.TextFragment("value " + j));

value.setColSpan(3);



}



curPage.getParagraphs().add(saleInfoTable);

}



return doc;

}

I’ve removed a lot of my specific code and just put in tests strings so you can get this to work, but hopefully from my code and my screenshot it’s apparent what I’m striving to achieve.

Hi Chris,


Thanks for sharing the code snippet.

I
have tested the scenario and I am able to notice the same problem. For the sake
of correction, I have logged this problem as PDFNEWJAVA-34112 in
our issue tracking system. We will further look into the details of this
problem and will keep you updated on the status of correction. Please be
patient and spare us little time. We are sorry for this inconvenience.<o:p></o:p>

Hi,


Thanks for the update I’ll keep checking this post for updates on the fix for this as it’d be very useful for us to have this functionality.

Thanks,

Chris

Hi Basil,


Thanks for your patience.

I am pleased to share that the issue PDFNEWJAVA-34070 reported earlier is resolved and its fix will be included in upcoming release of Aspose.Pdf for Java 9.1.0 which is planned to release within current week. Please try using the following code snippet to generate correct output.

[Java]
import com.aspose.pdf.Cell;
import com.aspose.pdf.Document;
import com.aspose.pdf.MarginInfo;
import com.aspose.pdf.Page;
import com.aspose.pdf.PageInfo;
import com.aspose.pdf.Paragraphs;
import com.aspose.pdf.Row;
import com.aspose.pdf.Table;
import com.aspose.pdf.TextFragment;


Document doc = new Document();
PageInfo pageInfo = doc.getPageInfo();
MarginInfo marginInfo = pageInfo.getMargin();

marginInfo.setLeft(37);
marginInfo.setRight(37);
marginInfo.setTop(37);
marginInfo.setBottom(37);

pageInfo.isLandscape(true);

Table table = new Table();
table.setColumnWidths(“50 100”);
// added page.
Page curPage = doc.getPages().add();
for (int i = 1; i <= 120; i++) {
Row row = table.getRows().add();
row.setFixedRowHeight(15);
Cell cell1 = row.getCells().add();
cell1.getParagraphs().add(new TextFragment(“Content 1”));
Cell cell2 = row.getCells().add();
cell2.getParagraphs().add(new TextFragment(“HHHHH”));
}
Paragraphs paragraphs = curPage.getParagraphs();
paragraphs.add(table);
/**
* *****************************************
*/
Table table1 = new Table();
table.setColumnWidths(“100 100”);
for (int i = 1; i <= 10; i++) {
Row row = table1.getRows().add();
Cell cell1 = row.getCells().add();
cell1.getParagraphs().add(new TextFragment(“LAAAAAAA”));
Cell cell2 = row.getCells().add();
cell2.getParagraphs().add(new TextFragment(“LAAGGGGGG”));
}
table1.isInNewPage(true);
// i want to keep table 1 to next page please…
paragraphs.add(table1);

doc.save(“test.pdf”);

The issues you have found earlier (filed as PDFNEWJAVA-34070) have been fixed in Aspose.Pdf for Java 9.1.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.