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

Free Support Forum - aspose.com

Can I set the number of times a repeating region repeats?

Hi,


I’m evaluating Aspose words. If I have a repeating set of data in a word table (using a region), is it possible to force a page break every X items. For example, if I have 50 order items, I only want to display 10 order item rows per page, then force a page break, and then display the next 10 on the second page…

Is the FieldMergingCallback the key to doing this?

Any help appreciated.

Hi

Thanks for your request. Yes, FieldMergingCallback is the key to doing this. I created a simple code and template for you to demonstrate the technique:

[Test]

public void Test001()

{

// Create a simple datasource.

DataTable data = new DataTable("data");

data.Columns.Add("Id");

data.Columns.Add("Name");

data.Columns.Add("Description");

for (int i = 0; i < 100; i++)

data.Rows.Add(new object[] {i, string.Format("item_{0}", i), string.Format("Description of item_{0}", i)});

// Open tempalte.

Document doc = new Document(@"Test001\in.doc");

// Specify FieldMergingCallback.

doc.MailMerge.FieldMergingCallback = new FieldMergingCallback(10);

// Execute mail merge.

doc.MailMerge.ExecuteWithRegions(data);

// Save output.

doc.Save(@"Test001\out.doc");

}

private class FieldMergingCallback: IFieldMergingCallback

{

public FieldMergingCallback(int recordsPerPage)

{

mRecordsPerPage = recordsPerPage;

}

public void FieldMerging(FieldMergingArgs args)

{

// We know that each row in our table starts with ID mergfield.

// So we can use it as couter.

if (args.FieldName == "Id")

{

// Get paragraphs where the mergefidl is located.

Paragraph paragraph = (Paragraph)args.Field.Start.GetAncestor(NodeType.Paragraph);

// If record index equels or greater than number of records per page we specified,

// We should force page break (specify "Page break before" option of this paragraph).

if (mRecordIndex >= mRecordsPerPage)

{

paragraph.ParagraphFormat.PageBreakBefore = true;

mRecordIndex = 0;

}

mRecordIndex++;

}

}

public void ImageFieldMerging(ImageFieldMergingArgs args)

{

// do nothing.

}

private int mRecordIndex = 0;

private int mRecordsPerPage;

}

Hope this helps.

Best regards,

Thanks, that worked a treat