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

Free Support Forum - aspose.com

MailMerge with region- insert break page inside table

Hi,
I generate a report with MailMerge.ExecuteWithRegions.
My document template looks like:

«TableStart:MASTER»

«TITLE»

-----------------------------------------------------------------------
| CODE | DESCRIPTION |
-----------------------------------------------------------------------
| «TableStart:ITEM»«CODE» | «DESCRIPTION»«TableEnd:ITEM» |
-----------------------------------------------------------------------

«TableEnd:MASTER»

I need to insert break page inside the table.
I tried to use InsertBreak(BreakType.PageBreak) => i try to replace a special value (when a FieldMerge has the value “<>”) by a break page but it doesn’t work: the value “<> is removed but there’s no page break.

My code:
lDoc.MailMerge.ExecuteWithRegions(aAsposeDataSet);
lDoc.Range.Replace(new Regex(Regex.Escape(”<>")), new AsposeWordSautPage(), false);

public class AsposeWordSautPage : IReplacingCallback
{
public ReplaceAction Replacing(ReplacingArgs a_args)
{
// Create Documentbuilder and move to matchnode.
DocumentBuilder builder = new DocumentBuilder((Document)a_args.MatchNode.Document);
builder.MoveTo(a_args.MatchNode);
builder.InsertBreak(BreakType.PageBreak);
a_args.MatchNode.Remove();
return ReplaceAction.Skip;
}
}

Is it possible to do this and how ?

Thanks.

Hello

Thanks for your request. Could you please attach your sample template here for testing. I will check how it goes on my side and provide you more information.

Best regards,

Hello,
As you can see in the template, i have also added a tag <> for a good numerotation.

Thanks

Hi

Thanks for your request. I think, in your case, you should not insert a paragraph or section break. In your case, you can simply set “Page Break Before” option of the paragraph where placeholder is located as you do in your template. As I can see this option works just perfectly, i.e. each master record starts from the new page.

The same you can do if you need a page break in the table, you should simply set this option for the paragraph that should be moved to the next page.

Best regards,

Hi,

I think your method doesn't work in my case. My template represents an invoice:

- The Table start MASTER represents the general informations of the invoice

- The Table start ITEM represents the lines of the invoice. The number of lines is undeterminated. In my application, the users can insert different types of lines: product, comment, sub-total and BREAK PAGE, etc...

That's why i tried to replace a special value in my table by a break page.

Hi

Thank you for additional information. Actually, the method I suggested should work for you. For example please see the attached test documents and the following code:

[Test]

public void Test001()

{

// Open document.

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

// Replace placeholders with page break.

doc.Range.Replace(new Regex(Regex.Escape("")), new PageBreakReplacingCallback(), false);

// Save output.

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

}

private class PageBreakReplacingCallback : IReplacingCallback

{

public ReplaceAction Replacing(ReplacingArgs args)

{

// Get paragraph where placeholder is located.

Paragraph paragraph = (Paragraph)args.MatchNode.GetAncestor(NodeType.Paragraph);

// Set "Page Break Before" option of this paragraph to force a page break.

paragraph.ParagraphFormat.PageBreakBefore = true;

// Replace paceholder with empty string.

args.Replacement = "";

return ReplaceAction.Replace;

}

}

Best regards,

Thanks for your response.

It works.

I have an other question: for my application, when i want to have a break page, i generate a line in ma datasource: only one datafield contains the value (the others datafields are blank) => when the value is replaced by a real page break, i have a line empy in the table of my document.

Is it possible to delete this line ?

Best regards

Hi

Thanks for your request. but unfortunately, it is not quite clear for me what you mean. Could you please attach your current output and expected output? I will take a look and provide you more information.

Best regards,

Hi,

I have changed my datasource to resolve this problem. Now, it's OK. :-)

Thanks

Hi

It is perfect that you managed to implement what you need. Please feel free to ask in case of any issues, we will be glad to help you.

Best regards,

Sorry but i have an other question for my final template. When i insert a break page, i would like to repeat header informations on each page.

I tried to put the image and the mergefields directly in the header of my template but it doesn't work (normal)...

How can i do this ?

Thanks

Hello

Thanks for your request. The simplest way to repeat some content on each page of your document is using Header/Footer concept. There should not be any problems with merge fields inside Header.

Please let me know in case of any issues. I will be glad to help you.

Best regards,

My template represents one or more invoices.

If i put mergefields tableStart and TableEnd in the header, it will repeat the informations for all my invoices => i want only the informations of the current invoice. If there is only one invoice edited, this method is ok (but not for more invoices edited at the same time...)

If i put the TableStart in the header and let the TableEnd in the content of the template, it doesn't work => errormessage: "mail merge region is badly formed. TableStart and TableEnd should be in then section, etc..." (normal..)

So i don't understand how i can do this.

Thanks

Hi

Thank you for additional information. In this case you should modify your template. I think you can create one table, then you can select rows that should be repeated on each page and set “Repeat as header row on top of each page” in MS Word.

Also, you can set this option programmatically. Please see the following link to learn more:

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net/aspose.words.tables.rowformat.headingformat.html

Hope this helps.

Best regards,