hi,
I am inserting a document into parent document (including headers). During the insert process the columns of the child document are lost. The code is posted below and I have attached the Parent and Child documents.
Can you please help.
private void RunInsert()
{
String dirname = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
Aspose.Words.Document dstDoc = new Aspose.Words.Document(dirname + @"\Parent Document.docx");
Aspose.Words.Document srcDoc = new Aspose.Words.Document(dirname + @"\Columns.docx");
DocumentInsertHeaders(dstDoc, srcDoc, "InsertLocation");
String fsavename = dirname + @"\OutPut.docx";
dstDoc.Save(fsavename);
System.Diagnostics.Process.Start(fsavename);
}
public Document DocumentInsertHeaders(Document dstDoc, Document srcDoc, String InsLocation)
{
// Create DocumentBuilder so we can modify the document.
DocumentBuilder builder = new DocumentBuilder(dstDoc);
string[] fldNames = dstDoc.MailMerge.GetFieldNames();
foreach(string fldName in fldNames)
{
if (fldName == InsLocation)
{
builder.MoveToMergeField(InsLocation);
Section sectA = null;
// Insert empty section
builder.InsertBreak(BreakType.SectionBreakNewPage);
sectA = builder.CurrentSection;
foreach(Section sectB in srcDoc.Sections)
{
int sectBIndex = srcDoc.Sections.IndexOf(sectB);
// Content
sectA.PrependContent(sectB);
// Loop throught all Headers/Footers in the B document
foreach(HeaderFooter hfB in sectB.HeadersFooters)
{
// Check whether current section from docA conteins
// Header/Footer with the same type as h/f from docB
if (sectA.HeadersFooters[hfB.HeaderFooterType] != null)
{
// Append content from h/f B to h/f A
foreach(Node childB in hfB.ChildNodes)
{
// Import node
Node childA = dstDoc.ImportNode(childB, true, ImportFormatMode.KeepSourceFormatting);
// Appent node to h/f
sectA.HeadersFooters[hfB.HeaderFooterType].AppendChild(childA);
}
}
else
{
// Copy whole h/f
Node hfA = dstDoc.ImportNode(hfB, true, ImportFormatMode.KeepSourceFormatting);
// Insert h/f
sectA.HeadersFooters.Add(hfA);
}
}
}
}
}
return dstDoc;
}