Insertion of page breaks at each page

Hi ,
I want to insert page breaks code in java for each and every page for which the repeating section table is being iterated. Can you send the code ?

@Sri_Harsha Could you please elaborate your requirements in more details? If possible please attach your input, current and expected output documents here for our reference. This will help us to better understand your requirements. We will check the documents and provide you more information.

I want to add the code for page breaks in my functionality so that the repeating section table template which I am attaching below will get the page breaks for all the pages till the repeating section table is being iterated
MyPageTotal12 - Copy.docx (21.4 KB)

@Sri_Harsha Thank you for additional information. Could you please also attach your current and expected output documents (you can create expected output in MS Word)?

Sorry I can’t provide the expected output but I want to add the code for adding page breaks at the end of the page for all the pages until repeating section table content control is being iterated

@Sri_Harsha Unfortunately, your requirements are still not quite clear so the expected output would be a great help for us to understand your requirements properly.
If you need to split the final table into parts so that each part of the table was placed on a separate page and there was a page break between these parts, then you can consider using an approach suggested in the following thread:
https://forum.aspose.com/t/split-tables-running-over-pages/258671/12

RetrieveAutosum (3).pdf (27.5 KB)
This is expected output, now I want to add code for generating page breaks at the end of each page for this repeating section table control

@Sri_Harsha Thank you for additional information. Do you mean adding a blank row at the end of each page and a sub-summary row at the beginning of the next page?

If so, you can use the approach I have suggested in my previous reply, i.e. you can use LayoutCollector to detect where table flows the the next page and insert an empty and sub-summary row at the detected position.

@alexey.noskov No I want to insert page break not as a row I think page breaks will be invisible so we can’t show them but when ever a row comes near page break then it should automatically shift to next page this is what I mean

@Sri_Harsha MS Word documents are flow documents by their nature so if there is no space for content, it is moved to the next page automatically so there is no need to explicitly insert page break to move content to the next page.

@alexey.noskov I know that information but in my case I am fixing a row before the page break due to some functionality to do that I need to insert page break but not as a row I can’t explain the entire use case here

@alexey.noskov Can I get help ?

@Sri_Harsha You can use the approach suggested in this answer to split the table into parts so that each part of the table was placed on a separate page:
https://forum.aspose.com/t/insertion-of-page-breaks-at-each-page/275065/6

@alexey.noskov Hi , My use case is like If in a page there is a space for 26 rows to fit I want to fit only 25 rows in that page and at 26th row I need to insert page break so that 27th row starts from next page and only 25 rows will be displayed in first page and instead of a row I insert page break at 26th row position.Can I get the code for this in java aspose words?

@Sri_Harsha To force a table row to be moved to the next page you can use ParagraphFormat.PageBreakBefore property. For example see the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Generate a test document.
builder.startTable();
for (int i = 0; i < 200; i++)
{
    for (int j = 0; j < 5; j++)
    {
        builder.insertCell();
        builder.write("row_" + i);
    }
    builder.endRow();
}
Table table = builder.endTable();

// Insert a page break before each 27th row.
int rowIndex = 0;
for (Row r : table.getRows())
{
    if ((rowIndex + 1) % 27 == 0)
        r.getFirstCell().getFirstParagraph().getParagraphFormat().setPageBreakBefore(true);

    rowIndex++;
}

doc.save("C:\\Temp\\out.docx");

While appending rows to a table in word document my requirement is to keep the cursor at the end of the last row of the table,
can I get the code for this requirement? I also want to insert page section break → next page as attached in screenshot can I get code for this in aspose words java

@Sri_Harsha You can move DocumentBuilder cursor using moveTo... methods. Please see our documentation for more information.
https://docs.aspose.com/words/java/navigation-with-cursor/

You can use DocumentBuilder.insertBreak to insert page break.

When I try to insert page section break → next page using the below code :slight_smile:

package com.infor.distribution.docgen.api;
import com.aspose.words.BreakType;
import com.aspose.words.Cell;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.License;
import com.aspose.words.NodeType;
import com.aspose.words.Paragraph;
import com.aspose.words.Row;
import com.aspose.words.Run;
import com.aspose.words.Section;
import com.aspose.words.Table;
public class InsertionOfPageBreak {
//Inserton of rows to a page in word doc java

		
	public void calculateRepeatingSectionElements() throws Exception {

			
		Document doc = new Document();
		DocumentBuilder builder = new DocumentBuilder(doc);
			
		Table tb = (Table) doc.getChild(NodeType.TABLE,0, true);
		for(int i=0;i<10;i++) {
			for (int j = 0; j < 5; j++) {
				builder.insertCell();
				builder.write("row_" + i);
			}
			builder.endRow(); 
		if(i == 5) {
			builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
		}  
		}
		Table table = builder.endTable();
		doc.updateTableLayout();
		doc.save("C:\\Temp\\out1.docx");
	}

	public static void main(String[] args) throws Exception {
		// License license = new License();
		// license.setLicense("Aspose.Words.lic");
		InsertionOfPageBreak RepeatingSectionElements = new InsertionOfPageBreak();
		RepeatingSectionElements.calculateRepeatingSectionElements();
	}
}

I am getting this error

Exception in thread "main" java.lang.IllegalStateException: Cannot insert the requested break inside a table.
	at com.aspose.words.DocumentBuilder.zz2j(Unknown Source)
	at com.aspose.words.DocumentBuilder.zzXj6(Unknown Source)
	at com.aspose.words.DocumentBuilder.insertBreak(Unknown Source)
	at com.infor.distribution.docgen.api.InsertionOfPageBreak.calculateRepeatingSectionElements(InsertionOfPageBreak.java:31)
	at com.infor.distribution.docgen.api.InsertionOfPageBreak.main(InsertionOfPageBreak.java:43)

I also tried by already inserting a table with few rows and columns as shown in the attachment
Screenshot (1440).png (236.1 KB)

and then after appending another table with it by adding the below code :

package com.infor.distribution.docgen.api;
import com.aspose.words.BreakType;
import com.aspose.words.Cell;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.License;
import com.aspose.words.NodeType;
import com.aspose.words.Paragraph;
import com.aspose.words.Row;
import com.aspose.words.Run;
import com.aspose.words.Section;
import com.aspose.words.Table;
public class InsertionOfPageBreak {
//Inserton of rows to a page in word doc java

		
	public void calculateRepeatingSectionElements() throws Exception {

			
		//Document doc = new Document();
		Document doc = new Document("C:\\Temp\\out1.docx");
		DocumentBuilder builder = new DocumentBuilder(doc);
			
		Table tb = (Table) doc.getChild(NodeType.TABLE,0, true);
		Row r = null;
		for(int i=0;i<10;i++) {
			/*for (int j = 0; j < 5; j++) {
				builder.insertCell();
				builder.write("row_" + i);
			}
			builder.endRow(); 
		/*if(i == 5) {
			builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
		}   
		}
		Table table = builder.endTable();
		doc.updateTableLayout(); */
			if( i <= 5) {
					r= (Row) tb.getLastRow().deepClone(true);
				} else {
					r = (Row) tb.getFirstRow().deepClone(true);
				}
			if(i == 5) {	
			builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
					Cell lastCell = r.getLastCell();
		            if (lastCell != null && lastCell.hasChildNodes()) {
		                builder.moveTo(lastCell.getLastParagraph());
						
			}
		}
		doc.save("C:\\Temp\\out1.docx");
	}

	public static void main(String[] args) throws Exception {
		InsertionOfPageBreak RepeatingSectionElements = new InsertionOfPageBreak();
		RepeatingSectionElements.calculateRepeatingSectionElements();
	}
}

Is it correct code to insert page section break at row 6 for the appended table and moving the cursor to the end of last row of the table?If yes I am getting the page break before the inserted table and no appended table but the expected output should be the page break should be inserted at 6th row after the inserted table (i.e 6th row of the appended table which is appended at the end of existing table)
Can I get help for resolving this issue?I am also attaching the output :slight_smile:
out1.docx (17.6 KB)
The expected output is after row 9 the new table should be appended and at 6th row of new table the page break should be inserted so that they will be displayed in next page and cursor moves to end of the last row of the table

@Sri_Harsha Section or page break cannot be inserted in the middle of the table. You should split the table into parts and insert section break between the tables. Please see our documentation to learn more about Aspose.Words Document object Model:
https://docs.aspose.com/words/java/aspose-words-document-object-model/

Your code should look like this:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

for (int t = 0; t < 2; t++)
{
    builder.startTable();
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            builder.insertCell();
            builder.write("row_" + i);
        }
        builder.endRow();
    }
    builder.endTable();
    builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
}

doc.save("C:\\Temp\\out.docx");