I have read through some of the forum post related to these topics and I haven’t found an approach that accounts for the product requirements I have been given.
First let me start by asking, what is the best/recommended practice for inserting contents on external documents into a mail merge document while respecting all the source section settings (ex. styles, font, orientation, etc). Unfortunately I cannot just append all the document content to the end of the mail merge document, that seems to work fine. Its seems that using DocumentBuilder.InsertBreak(BreakType.SectionBreakNewPage) has something to do with the "reference node is not a child of this node " exception that I am seeing. (Using Aspose.Words for .NET 11.10.0.0)
So the scenario is:
Section
MergeField
MergeField (This one is asking for attachments/external documents)
MergeField
It seems like I need to be able to do the following:
Section
MergeField
(Insert Page and Section Break)
(Add Sections from external document in place of second merge field)
Section
Section
(Then have a Section with the last Merge field remaining )
Section
MergeField
If that makes sense. If I dont execute the insertbreak() the code works exactly as expected only the sections are @ the end of the document. If I execute the insertbreak() code without the code that is attempting to insert external document content, I still get the above mentioned exception. So I’m not sure if it is a bug internal to that method or if there is something else in the mail merge process that I have yet to identify.
Mail merge is executing with regions (and nested regions for that matter).
Consider this Merge Template (or view attachment for specifics):
{MERGEFIELD Tracking_ID }
{MERGEFIELD
TableStart:Level_2 * MERGEFORMAT}
{MERGEFIELD Attachments * MERGEFORMAT}
{MERGEFIELD TableStart:Level_3 * MERGEFORMAT}
{MERGEFIELD More_Attachments * MERGEFORMAT}
{MERGEFIELD TableEnd:Level_3 * MERGEFORMAT}
{MERGEFIELD TableEnd:Level_2 * MERGEFORMAT}
{MERGEFIELD Description * MERGEFORMAT}
Essentially, I am able to detect the “attachment” merge fields and I attempt to page and section break using insert break but that’s not working. I need the final output to be the data for the first merge field then all the attachment data “inline” then the data for the last merge field.
I can get code snippets if needed, hoping this has been encountered before and there is a better approach.
Calling pattern in code is basically:
Merge()
{
…
Document.MailMerge.ExecuteWithRegions(myDataSource);
…
}
MyMailMerge_MergeFieldCallback(FieldMergingArgs e)
{
Process(e.Document, e.Field.GetFieldCode(), e.FieldName, e.FieldValue);
}
Process(Document document, string fieldCode, string fieldName, object fieldValue)
{
…
Builder.MoveToMergeField(fieldName);
…
(if my value is a memory stream)
Section sectionBeforeBreak = Builder.CurrentSection;
Builder.InsertBreak(BreakType.SectionBreakNewPage);
Document supportingDocumentation = new Document((MemoryStream)value);
InsertDocument(Builder, supportingDocumentation, sectionBeforeBreak );
}
InsertDocument(DocumentBuilder builder, Document sourceDocument, Section currentSection)
{
if (sourceDocument.Sections == null || sourceDocument.Sections.Count == 0) return;
foreach (Section srcSection in sourceDocument.Sections)
{
srcSection.ClearHeadersFooters();
Section sectionToInsert = (Section)builder.Document.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
builder.Document.InsertAfter(sectionToInsert, currentSection);
currentSection = sectionToInsert;
}
}
Let me know what you think or if you need any additional details.
Thanks
Vernon