Hello Team ,
Footer got disappear after merging multiple documents by using aspose lib ,
Please check attach code and documents for more details .
You help is appreciated
Footer Dissapaer Issue.zip (232.5 KB)
Thanks,Avinash
Hello Team ,
Footer got disappear after merging multiple documents by using aspose lib ,
Please check attach code and documents for more details .
You help is appreciated
Footer Dissapaer Issue.zip (232.5 KB)
Thanks,Avinash
You are merging several Word and PDF files with Aspose libraries and the footer disappears after the merge operation.
| Cause | Why it removes the footer | How to fix |
|---|---|---|
Using ImportFormatMode.UseDestinationStyles when appending documents |
The destination document’s style/section settings overwrite the source footers. | Use ImportFormatMode.KeepSourceFormatting. |
| Merging PDF → Word by simply loading the PDF | Aspose.Words treats the PDF as a single section without preserving the original footers. | Convert the PDF to a Word document first (e.g., Aspose.Pdf.Facades.PdfConverter or Aspose.Words.Document(pdfStream)) and then append. |
| Footers defined only on the first page / odd‑even | The merged document may create a new section where those special footer settings are lost. | Ensure all sections have the required footer type (Primary, First, Even). |
Using DocumentBuilder.InsertDocument without the proper ImportFormatMode |
Similar to the first case – source footer styles are ignored. | Pass ImportFormatMode.KeepSourceFormatting to InsertDocument. |
Below is a robust pattern that works for Word + PDF merges while preserving every footer.
using Aspose.Words;
using Aspose.Pdf.Facades;
// ---------------------------------------------------
// 1. Load the first Word document (acts as the destination)
// ---------------------------------------------------
Document finalDoc = new Document("Doc1.docx");
// ---------------------------------------------------
// 2. Append additional Word documents
// ---------------------------------------------------
string[] wordFiles = { "Doc2.docx", "Doc3.docx" };
foreach (var file in wordFiles)
{
Document src = new Document(file);
// Keep the source formatting (including footers)
finalDoc.AppendDocument(src, ImportFormatMode.KeepSourceFormatting);
}
// ---------------------------------------------------
// 3. Convert each PDF to a temporary Word document
// ---------------------------------------------------
string[] pdfFiles = { "FileA.pdf", "FileB.pdf" };
foreach (var pdf in pdfFiles)
{
// Convert PDF → DOCX in memory
using (MemoryStream pdfStream = new MemoryStream(File.ReadAllBytes(pdf)))
using (MemoryStream docxStream = new MemoryStream())
{
// PDF → DOCX conversion (Aspose.PDF)
PdfConverter converter = new PdfConverter();
converter.BindPdf(pdfStream);
converter.DoConvert();
converter.SaveAsDocX(docxStream);
converter.Close();
// Load the converted DOCX
docxStream.Position = 0;
Document pdfAsDoc = new Document(docxStream);
// Append to the final document preserving footers
finalDoc.AppendDocument(pdfAsDoc, ImportFormatMode.KeepSourceFormatting);
}
}
// ---------------------------------------------------
// 4. Save the merged result
// ---------------------------------------------------
finalDoc.Save("MergedResult.docx");
ImportFormatMode.KeepSourceFormatting – Guarantees the source footers (and other section settings) are retained.Aspose.Pdf.Facades.PdfConverter to turn each PDF into a Word document first, which ensures the PDF’s original footers (if any) are turned into Word footers that can be merged.foreach (Section sec in finalDoc.Sections)
{
sec.Footers.LinkToPrevious(false); // keep each section's own footer
}
If you still experience missing footers after trying the steps above, please share the following (you can paste the relevant snippets instead of the whole zip):
AppendDocument, InsertDocument, DocumentBuilder, etc.).Aspose.Words for .NET 23.9 and Aspose.PDF for .NET 23.9).With those details we can reproduce the issue and give a pinpoint solution.
Helpful Links
If you have any further questions or need clarification, feel free to reply with the requested details. We’re happy to help!
@Chilke This occurs because in your code you unlink headers/footer in all sections of your input document. So inherited headers/footers are not shown in the output. You can try using LowCode method to merge your documents:
Aspose.Words.LowCode.Merger.Merge(@"C:\Temp\out.docx", new string[] { @"C:\Temp\Doc1.docx", @"C:\Temp\Doc2.pdf" });
out.docx (34.1 KB)
Or unlink headers/footers only in the first section:
Document doc1 = new Document(@"C:\Temp\Doc1.docx");
Document doc2 = new Document(@"C:\Temp\Doc2.pdf");
// Unlink headers/footer only in the first section.
doc1.FirstSection.HeadersFooters.LinkToPrevious(false);
doc2.FirstSection.HeadersFooters.LinkToPrevious(false);
// Merge documents.
Document result = Aspose.Words.LowCode.Merger.Merge(new Document[] { doc1, doc2 }, MergeFormatMode.KeepSourceFormatting);
result.Save(@"C:\Temp\out.docx");
out.docx (40.1 KB)
Thanks @alexey.noskov and @Professionalize.Discourse
Merge Code is working fine
OutPutAfterPaggingPageno.Docx (40.4 KB)
, But Existing footer got removed when adding Page number to merged document , After third page footer (Written By________) got removed , Please provide code to add page number for merged document , without disturbing exiting footer text , Below code is using for adding page number .
public Document AddPageNumberAfterMergingDocs(Document doc)
{
if (doc == null || doc.Sections.Count == 0)
return doc;
int tocSectionIndex = -1;
// Step 1: Find TOC section index
foreach (Field field in doc.Range.Fields)
{
if (field.Type == FieldType.FieldTOC)
{
Section tocSection = (Section)field.Start.GetAncestor(NodeType.Section);
tocSectionIndex = doc.Sections.IndexOf(tocSection);
break;
}
}
for (int i = 0; i < doc.Sections.Count; i++)
{
Section section = doc.Sections[i];
// Step 2: Get or create footer
HeaderFooter footer = section.HeadersFooters[HeaderFooterType.FooterPrimary];
if (footer == null)
{
footer = new HeaderFooter(doc, HeaderFooterType.FooterPrimary);
section.HeadersFooters.Add(footer);
}
// Step 3: Check if footer has a page number field
bool hasPageNumber = footer.Range.Fields.Any(f => f.Type == FieldType.FieldPage);
if (!hasPageNumber)
{
// Add centered "Page X" paragraph
Paragraph pageNumberPara = new Paragraph(doc);
pageNumberPara.ParagraphFormat.Alignment = ParagraphAlignment.Center;
pageNumberPara.ParagraphFormat.SpaceBefore = 6;
// "Page " prefix + page number field
pageNumberPara.AppendChild(new Run(doc, "Page "));
pageNumberPara.AppendField(FieldType.FieldPage, true);
footer.AppendChild(pageNumberPara);
}
else
{
// Optional: If already exists, update the field (forces refresh)
foreach (Field f in footer.Range.Fields.Where(f => f.Type == FieldType.FieldPage))
{
f.Update();
}
}
// Step 4: Restart numbering at TOC section
if (i == tocSectionIndex)
{
section.PageSetup.RestartPageNumbering = true;
section.PageSetup.PageStartingNumber = 1;
}
else
{
section.PageSetup.RestartPageNumbering = false;
}
}
// Step 5: Update all fields and layout
doc.UpdateFields();
doc.UpdatePageLayout();
return doc;
}
@Chilke The problem occurs because when a header or footer is linked to the previous section, it does not actually exist in the current section — it is null. When you create an empty header or footer in the section, it no longer uses the header or footer from the previous section. Keeping this in mind, please try modifying your code as follows:
// Step 2: Get or create footer
HeaderFooter footer = section.HeadersFooters[HeaderFooterType.FooterPrimary];
HeaderFooter prevFooter = (i > 0) ? doc.Sections[i - 1].HeadersFooters[HeaderFooterType.FooterPrimary] : null;
if (footer == null)
{
footer = prevFooter != null ? (HeaderFooter)prevFooter.Clone(true) : new HeaderFooter(doc, HeaderFooterType.FooterPrimary);
section.HeadersFooters.Add(footer);
}
Thanks @alexey.noskov Code is keeping footer in document , but page number is not updated for other merged document , if it contain already page no , Please check attach data
DataFiles.zip (135.8 KB)
Thanks,
Avinash
@Chilke Page numbers in MS Word documents are represented with PAGE fields. In the appended PDF there is no PAGE field. There is simple text: