Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Nov 04, 2015 7:24 pm

Is it possible to copy the header from an existing word document and add it to another word document?

nseyler
 
Posts: 12
Joined: Wed Nov 04, 2015 6:43 pm

Thu Nov 05, 2015 4:00 am

Hi,

Thanks for your inquiry.
Our Spire.Doc supports it. Share sample code:
Code: Select all
Document doc1=new Document();
doc1.LoadFromFile("..\\..\\test1.docx");
HeaderFooter header = doc1.Sections[0].HeadersFooters.Header;
Document doc2 = new Document("..\\..\\test2.docx");
foreach (Section section in doc2.Sections)
{
foreach (DocumentObject obj in header.ChildObjects)
{
section.HeadersFooters.Header.ChildObjects.Add(obj.Clone());
}
}

string output = "result.docx";
doc2.SaveToFile(output,FileFormat.Docx);


Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3046
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc