Hi,
I checked you code. Even though it is removing extra space of header and footer. But there is one more problem occurred. Like we have 3 documents to merge and last one has header in it. It is removing that header too. It should not remove the header of third document.
The modified version of your code i am using is below:
DirectoryInfo dirinfo = new DirectoryInfo(resultpath);
FileInfo[] files = dirinfo.GetFiles().OrderBy(p => p.CreationTime).ToArray();
if (files.Count() == 1)
{
Document pdfdoc = new Document(files[0].FullName);
pdfdoc.Save(resultpath + savefilename + “.doc”);
pdfdoc.Save(resultpath + savefilename + “.pdf”);
}
else
{
Document docall = new Document(files[0].FullName);
docall.Save(resultpath + savefilename + “.doc”);
for (int i = 1; i < files.Count(); i++)
{
Document docall_final = new Document(resultpath + savefilename + “.doc”);
Document doc_current = new Document(files[i].FullName);
doc_current.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
doc_current.FirstSection.PageSetup.RestartPageNumbering = true;
doc_current.FirstSection.HeadersFooters.LinkToPrevious(false);
//doc_current.UpdatePageLayout();
foreach (Section sec in doc_current.Sections)
{
foreach (HeaderFooter hf in sec.HeadersFooters)
{
if (hf.ChildNodes.Count == 1 &&
hf.FirstChild.NodeType == NodeType.Paragraph &&
hf.ToString(SaveFormat.Text).Trim().Equals(string.Empty))
{
hf.Remove();
}
}
}
docall_final.AppendDocument(doc_current, ImportFormatMode.KeepDifferentStyles);
<a class="attachment" href="/uploads/discourse_instance3/5190">b1.zip</a> (17.4 KB)
docall_final.Save(resultpath + savefilename + ".doc");
docall_final.Save(resultpath + savefilename + ".pdf");
}
}
I am also attaching 3rd document which contains header part. you can test with this.