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

Free Support Forum - aspose.com

Combining Documents- importing Headers/Footers

Hi,


atm i am combining documents like this;
Document combined = new Document();
String outputPath;

foreach (String path in Paths)
{
using (FileStream src = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
Document srcDoc = new Document(src);

    <span style="color:#4ec9b0;">Section</span> srcSec <span style="color:#b4b4b4;">=</span> <span style="color:#569cd6;">new</span> <span style="color:#4ec9b0;">Section</span>(srcDoc);

    <span style="color:#569cd6;">foreach</span> (<span style="color:#4ec9b0;">Section</span> sec <span style="color:#569cd6;">in</span> srcDoc<span style="color:#b4b4b4;">.</span>Sections)
    {
        srcSec<span style="color:#b4b4b4;">.</span>AppendContent(sec);
    }

    <span style="color:#4ec9b0;">Section</span> secToAdd <span style="color:#b4b4b4;">=</span> (<span style="color:#4ec9b0;">Section</span>)combined<span style="color:#b4b4b4;">.</span>ImportNode(srcSec, <span style="color:#569cd6;">true</span>);

    combined<span style="color:#b4b4b4;">.</span>Sections<span style="color:#b4b4b4;">.</span>Add(secToAdd);
}

}

DocumentBuilder builder = new DocumentBuilder(combined);

builder.MoveToDocumentStart();
builder.InsertTableOfContents("\o “1 - 3” \h \z \u");
combined.UpdateFields();

outputPath = Directory.GetParent(Paths[0]).FullName;

combined.Save(Path.Combine(outputPath, “FileOut.docx”), SaveFormat.Docx);

This works pretty fine, but it cant import and combine header/footers. Is there any way to extract the headers/footers from the documents i combine? Even if headers/footers aren’t the same?

Greetings,

Max

Hi Max,


Thanks for your inquiry. Please use the following code example to merge the header/footer of documents. The MergeHeaderFooter method gets the contents of header/footer from source document and append into the output document’s header/footer.

Hope this helps you. Please let us know if you have any more queries.

Document doc1 = new Document(MyDir + “doc1.docx”);

Document doc2 = new Document(MyDir + “doc2.docx”);

MergeHeaderFooter(doc1, doc2, HeaderFooterType.HeaderPrimary);

MergeHeaderFooter(doc1, doc2, HeaderFooterType.FooterPrimary);

doc2.Save(MyDir + “Out.docx”);

public void MergeHeaderFooter(Document srcDoc, Document dstDoc, HeaderFooterType headerType)

{

foreach (Section section in dstDoc.Sections)

{

HeaderFooter header = section.HeadersFooters[headerType];

if (header == null)

{

// There is no header of the specified type in the current section, create it.

header = new HeaderFooter(section.Document, headerType);

section.HeadersFooters.Add(header);

}

foreach (Node srcNode in srcDoc.FirstSection.HeadersFooters[headerType].ChildNodes)

{

Node dstNode = dstDoc.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);

header.AppendChild(dstNode);

}

}

}


Hi Tahir,


many thanks for your answer :slight_smile: its working perfectly

Regards,

Max