We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How can I insert a page break in a table?

The real problem is that the template is a catalog where you want a page break after a row if a certain condition occurs.
Unfortunately this doesn’t work at all.
That’s why I tried to insert a marker instead of the page break during the merge process, which I then want to replace afterwards.

The aim is to replace the text “{{PageBreak}}” with a page break.

Unfortunately, the page break comes after the table.

An example program and the document is attached.

Aspose.Word.Testanwendung.TX07749 - HFR.zip (75.9 KB)
Output_Expected.docx (103.8 KB)
Output_Aspose.docx (68.2 KB)
Input.docx (99.8 KB)

Of course, it would be best if it worked with the original template, without the detour of replacing it.
OriginalTemplate.docx (99.2 KB)

@Niebelschutz In your case you can use ParagraphFormat.PageBreakBefore property of Paragraph to make the table continue from the next page. Foe example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");

// Get the table
Table table = (Table)doc.FirstSection.Body.GetChild(NodeType.Table, 0, true);

bool isFirst = true;
// Loop through the rows in the table.
foreach (Row row in table.Rows)
    if (row.FirstCell.Range.Text == "1\a")
        if (isFirst)
            isFirst = false;

        row.FirstCell.FirstParagraph.ParagraphFormat.PageBreakBefore = true;



As I can see you are using mail merge to fill your document with data. in your case you can use mail merge with regions and IFieldMergingCallback to insert page break if the data matches some condition. For example see the following modified template and the code:

DataTable dt = new DataTable("Data");

dt.Rows.Add("1", "Antrago 9699", "Andrea", "OLG S");
dt.Rows.Add("2", "Antrago 9707", "Andrea", "OLG S");
dt.Rows.Add("3", "Antrago 9712", "Andrea", "OLG S");
dt.Rows.Add("4", "Antrago 9722", "Andrea", "OLG S");
dt.Rows.Add("5", "Antrago 9732", "Andrea", "OLG S");
dt.Rows.Add("1", "Antrago 9713", "Andrea", "OLG S");
dt.Rows.Add("2", "Antrago 9716", "Andrea", "OLG S");
dt.Rows.Add("3", "Antrago 9719", "Andrea", "OLG S");
dt.Rows.Add("4", "Antrago 9734", "Andrea", "OLG S");
dt.Rows.Add("5", "Antrago 9736", "Andrea", "OLG S");
dt.Rows.Add("1", "Antrago 9705", "Andrea", "OLG S");
dt.Rows.Add("2", "Antrago 9711", "Andrea", "OLG S");
dt.Rows.Add("3", "Antrago 9726", "Andrea", "OLG S");
dt.Rows.Add("4", "Antrago 9737", "Andrea", "OLG S");
dt.Rows.Add("5", "Antrago 9738", "Andrea", "OLG S");

Document doc = new Document(@"C:\Temp\ModifiedTemplate.docx");
doc.MailMerge.FieldMergingCallback = new FieldMergingCallback();
private class FieldMergingCallback : IFieldMergingCallback
    public void FieldMerging(FieldMergingArgs args)
        if (args.FieldName == "VDE_NUM" && args.FieldValue == "1")
                args.Field.Start.ParentParagraph.ParagraphFormat.PageBreakBefore = true;

            mIsFirst = false;

    public void ImageFieldMerging(ImageFieldMergingArgs args)
        // Do nothing

    private bool mIsFirst = true;

ModifiedTemplate.docx (99.3 KB)
out.docx (68.2 KB)

Unfortunately I still have a problem.

The page break in the table works so far.
Unfortunately, the placeholders in the header are not replaced. This means that the same data is output on each side.
If the whole thing is created using MS Word, the page break also changes the data in the header accordingly.

Do you have any suggestion how I can solve this problem?

Output___Aspose.docx (102.4 KB)
Output___MSWord.docx (118.8 KB)

@Niebelschutz Could you please provide your template, sample data for example in XML exported from DataSet and code you use to execute mail merge? So we can investigate the issue with your data and code. We will check the issue and provide you more information.

I have prepared an example program: Aspose.Word.Testanwendung.zip (164.8 KB)

For comparison I added the original template and the data.csv for MS Word.
If you connect the data.csv with the template in MS Word, you will get the desired result.

@Niebelschutz Thank you for additional information. In your case you have to execute mail merge several times, generate a separate document for each category and then concatenate the resulting documents into one document. I have modified your code:

//var template = "TemplateOriginal_works_with_MS_Word.docx";
var template = "TemplateModified_for_Aspose.docx";
var fileOut = "Output.docx";

using (var table = GetDataTable())
	// Select distinct values for header mergefields.
	DataTable distinct_vstg_kurz = table.DefaultView.ToTable(true, "VSTG_KURZ", "VSTG_ZF_101_TXT", "VSTG_ZF_103_TXT", "VSTG_ZF_102_TXT", "VSTG_ZF_105_TXT");

	Document mainDocument = null;
	foreach (DataRow r in distinct_vstg_kurz.Rows)
		// Open template.
		Document doc = new Document(template);

		// Get value of VSTG_KURZ column, it will be used to filter required rows.
		string vstg_kurz = r["VSTG_KURZ"].ToString();

		// Filter rows.
		DataView filtered = new DataView(table);
		filtered.RowFilter = string.Format("VSTG_KURZ = '{0}'", vstg_kurz);

		doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields | MailMergeCleanupOptions.RemoveContainingFields;
		// Fill the table with data.
		// Fill the header with data.
		doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;

		// Merge subdocuments into one.
		if (mainDocument == null)
			mainDocument = doc;
			mainDocument.AppendDocument(doc, ImportFormatMode.UseDestinationStyles);
	// Save the result.
	mainDocument.Save(fileOut, SaveFormat.Docx);

As you can see code does not use neither IFieldMergingCallback nor IReplacingCallback. Here is the result generated by this code:Output.docx (79.3 KB)

Thank you for your suggested solution. I was able to implement and generalize it as far as I could. This part works now :+1:.

I still have 2 small problems and hope you can help me with them:

  1. When I open the created document, I always get the message that an SQL query SQLAbfrage.png (12.6 KB) should be executed. I already tried it with “mainDocument.MailMergeSettings.Clear();” removed, but the message still comes up. How can I remove the message?

  2. When I open the created document, it says “5 of 8” with page. Word only correctly displays “4 of 4” when I scroll to the end of the document. I tried updating the page display with “mainDocument.UpdatePageLayout();” update before saving, but to no avail. Do you have another idea?


  1. mainDocument.MailMergeSettings.Clear(); should work in your case. I have tested with your original template with the following simple code:
Document doc = new Document(@"C:\Temp\TemplateOriginal_works_with_MS_Word.docx");

When I open the output document there is no SQL query execution request. Could you please attach your output document here for testing? I will check it and provide you more information.

  1. Does the problem occurs with the same document? The problem might occur because Aspose.Words improperly updates page layout, but is is difficult to say without the problematic document. Could you please attach the document, so I can check it?