Aspose.PDF java table keep together in one page

Is there a way to ensure that a Table is kept together in one page and not split up in two pages? Table.setBroken(false) and Table.setKeptWithNext(true) does not work. I am looking something similar to iText.PdfPTable.keeptogether feature (com.lowagie.text.pdf.PdfPTable.setKeepTogether java code examples | Tabnine)

if this is not possible, is there an option to find out the current position of the cursor so i can at least determine whether i am at the end of the page before printing a table?

@bgonzalez

You can please try using Table.getHeight() method in order to determine whether the table has reached the page bottom or not. You can compare this value with the Page Height.

there are other text/paragraphs before the table so basically i need to know when the paragraph position ends then add it to the table height and compare with page height. Is it possible to find the the paragraph position ends?

I assume your answer confirms that there is no feature in Aspose that is similar to iText.PdfPTable.keeptogether feature.

@bgonzalez

Aspose.PDF for Java does offer a property that forces the table to render on a new Page. The property usage can be found in the below article:

In case you issue still does not resolve, we request you please share your sample PDF document along with complete code snippet that you are using. We will further proceed to assist you accordingly.

I found something in Aspose.Words that explains exactly what i am looking for in Aspose.PDF. See " Keeping a Table from Breaking across Pages" section on the URL below.
https://docs.aspose.com/words/java/keeping-tables-and-rows-from-breaking-across-pages/

I am hoping that there is something similar in Aspose.PDF. I will try playing with the keepwithnext function.

@bgonzalez

You can please try playing around with properties but meanwhile, could you kindly share a source PDF and complete code snippet that you are using to add table inside PDF? It would help us in investigating the scenario in our environment and address it accordingly.

test_out.pdf (8.3 KB)

Asad,

Here is a code snippet. This code generate 10 tables with dynamic number of rows. The goal is to have all tables generated in one page instead of split across two pages. The rows are randomly generated. In the attached test_out.pdf, Table 2 and Table 6 are split. Ideally, Table 2 and Table 6 should be kept together and printed on the next page. I cannot force Tables 2 and 6 to render on the new page because the number of rows is dynamic.

public static void main(String[] args) {
	
	try {
		License license = new License();

		license.setLicense(DocumentHelper.class.getResourceAsStream("Aspose.Total.Java.lic"));
		String s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.\n"+
				"Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus.\n" +
				"Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci.\n" +
				"Aenean nec lorem. In porttitor. Donec laoreet nonummy augue.\n" +
				"Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc. Mauris eget neque at sem venenatis eleifend. Ut nonummy"+
				"\r\n\r\nLorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.\n"+
				"Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus.\n" +
				"Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci.\n" +
				"Aenean nec lorem. In porttitor. Donec laoreet nonummy augue.\n" +
				"Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc. Mauris eget neque at sem venenatis eleifend. Ut nonummy";

		Document document = new Document();
		 
        Page page = document.getPages().add();
         
        page.getParagraphs().add(new TextFragment(s));
        
        TextFragment fragment = new TextFragment("\r\n**** The goal is to have all ROWS BELONGING TO A TABLE TOGETHER IN ONE PAGE AND NOT SPLIT ACROSS TWO PAGES.");
		fragment.getTextState().setFontStyle(FontStyles.Bold);
        page.getParagraphs().add(fragment);
        MarginInfo margin = new MarginInfo();
        margin.setTop (5f);
        margin.setLeft (5f);
        margin.setRight (5f);
        margin.setBottom (20f);
        for (int i = 0; i < 10; i++) {
        	Table table = new Table();
	        table.setDefaultCellPadding(margin);
	        table.setDefaultCellBorder(new BorderInfo(BorderSide.All, 0.1F));
        	table.setColumnWidths("200 200");
        	int rowCnt = new Random().nextInt(9)+1;
        	for (int j = 0; j < rowCnt; j++) {
        		Row row = table.getRows().add();
        		row.getCells().add("Table " + i + " : Row " + j + " : Col 1");
        		row.getCells().add("Table " + i + " : Row " + j + " : Col 2");
			}
        	fragment = new TextFragment("\r\nTABLE " + i);
			fragment.getTextState().setFontStyle(FontStyles.Bold);
			page.getParagraphs().add(fragment);
        	page.getParagraphs().add(table);
		}
        // Save updated PDF
        document.save("c:/temp/test_out.pdf");
	} catch (Exception e) {
		e.printStackTrace();
	}

}

@bgonzalez

We are checking it and will get back to you shortly.

I found a solution for this so we can let this go. The trick is to have all tables inside another main table.

@bgonzalez

It is nice to know that you were able to sort your issue out. Please keep using our API and feel free to let us know in case you face any issues.