本文我们将详细介绍如何使用Spire.Doc高效的复制Word文档的页眉或页脚(包括文本和图形),并插入到其他Word文档。
C#
//加载源文档
Document doc1 = new Document();
doc1.LoadFromFile("test1.docx");
//获取第一个section的页眉
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());
}
}
VB.NET
'加载源文档
Dim doc1 As Document = New Document
doc1.LoadFromFile("test1.docx")
'获取第一个section的页眉
Dim header As HeaderFooter = doc1.Sections(0).HeadersFooters.Header
'加载新文档
Dim doc2 As Document = New Document("test2.docx")
For Each section As Section In doc2.Sections
'将页眉中的对象进行复制,并插入到新文档中
For Each obj As DocumentObject In header.ChildObjects
section.HeadersFooters.Header.ChildObjects.Add(obj.Clone)
Next
Next
源文档:
结果文档: