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

Free Support Forum - aspose.com

Word document formatting not getting properly when using ImportNode() fo saving individual section

Hi,

When using ImportNode to import all sections from a document to another document then document formatting(Header/Footer) is getting properly. But when I import each individual section and saving them individually then document formatting(Header/Footer) is not getting properly. The following code:

Document partialdoc = (Document)doc.Clone(false);
foreach (Section sec in doc.Sections)
{
    Node tmpNode = partialdoc.ImportNode(sec, true,
    ImportFormatMode.KeepSourceFormatting);
    partialdoc.AppendChild(tmpNode);
    partialdoc.Save(@"C:\\newSection .docx");
}
partialdoc.Save(@"C:\\newDoc .docx");

newDoc.docx (315.8 KB)
newSection.docx (85.7 KB)

@RajChauhan

We ran several tests, but we can’t replicate your issue, also the formatting looks the same for the equivalent sections in the 2 documents that you upload, can you please share the original document from where you are extracting the sections?
And please note that your code is not saving the sections individually since you are saving in each execution of the loop the same Document in where you are appending all the sections, so after the last execution of the foreach loop the newSection.docx files will contain as many sections as the original Document that you are loading; so at the end of the code the newSection.docx file will contain the same data that the newDoc.docx file.

Please check newSection.docx, Footer(Page number) not getting while section save

@RajChauhan as I said we cant replicate your issue, I’m attaching what’s the result that we are getting from run the code that you provide using the newDoc.docx file (that you also provided) as base document, as you can see the page number remain in the both resultant documents. Can you please share with us the original base file that you are using and check that the code snippet that you post is the actual code that you are using in your app.

Results from run the following code using the package of Aspose.Words with version 23.1.0:

    // Load the document.
    Document doc = new Document("C:\\Temp\\newDoc.docx");
    Document partialdoc = (Document)doc.Clone(false);
    foreach (Section sec in doc.Sections)
    {
        Node tmpNode = partialdoc.ImportNode(sec, true,
        ImportFormatMode.KeepSourceFormatting);
        partialdoc.AppendChild(tmpNode);
        partialdoc.Save(@"C:\\Temp\\newSection.docx");
    }
    partialdoc.Save(@"C:\\Temp\\newDoc_clone.docx");

newDoc.docx (315.8 KB)
newDoc_clone.docx (315.7 KB)
newSection.docx (315.7 KB)

@RajChauhan
We had to modify a bit your code, to be able to reproduce the issue, actually the “issue” that you mention is the expected behavior, when you save the Sections individually you cannot see the footer in the saved documents after the 3rd section because those Sections don’t contain any footer. If a Section does not have a HeaderFooter of a specific type or the HeaderFooter has no child nodes, this header/footer is considered linked to the header/footer of the same type of the previous section in Microsoft Word, and for that reason you see all the pages with a page number in MS Word app. You can learn more about HeaderFooter class here.
Now, for you particular case what you can do to keep the footer in the Sections where don’t exists any is add it manually to that Section. I’m attaching a code example that will solve your issue:

// Load the document.
Document doc = new Document("C:\\Temp\\newDoc.docx");
Document clonDoc = (Document)doc.Clone(false);

short sectionCounter  = 1;
HeaderFooter? footer = null;
foreach (Section sec in doc.Sections)
{
    // Add the Section to the clone Document
    Node node = clonDoc.ImportNode(sec, true, ImportFormatMode.KeepSourceFormatting);
    clonDoc.AppendChild(node);

    // Create a Document to be able to save each individual section
    Document temp = (Document)clonDoc.Clone(false);

    // Get the current footer of the section
    var currentFooter = sec.HeadersFooters.FirstOrDefault(hf => ((HeaderFooter)hf).HeaderFooterType == HeaderFooterType.FooterPrimary) as HeaderFooter;

    // Should add footer when the current section don't have any footer and the prevoius section had a footer to replicate
    if (currentFooter == null && footer != null)
    {
        // Add the footer to the section
        Node tempFooterNode = sec.Document.ImportNode(footer, true, ImportFormatMode.KeepSourceFormatting);
        sec.AppendChild(tempFooterNode);
    }
    else if(currentFooter != null)
    {
        footer = currentFooter;
    }

    // Add the current Section to the Document
    Node tempNode = temp.ImportNode(sec, true, ImportFormatMode.KeepSourceFormatting);
    temp.AppendChild(tempNode);

    // Save the Document containing only the section and increase the counter
    temp.Save(@$"C:\\Temp\\Section_{sectionCounter++}.docx");
}

// Save the whole cloned Document
clonDoc.Save(@"C:\\Temp\\newDoc_cloned.docx");

Here, I used above code for footer page number and see First page of section 4 not showing page number and from 2nd page, page number staring from 1. In section 5 also page number starting from 1 instead of continues. How I got continues page numbers? You can see in following documents:

Section_4.docx (86.5 KB)
Section_5.docx (106.6 KB)
newDoc_cloned.docx (151.5 KB)

@RajChauhan, in the code example that I posted I only replicate the footer of type HeaderFooterType.FooterPrimary, to preserve consistency across all Sections’ footers you must also replicate the HeaderFooterType.FooterFirst and HeaderFooterType.FooterEven, that will solve the issue with the First page in Section_4.
For the issue with the start page number you can count the pages and set the PageStartingNumber to match the value of the page counter, check how I did it in the following example:

// Load the document.
Document doc = new Document("C:\\Temp\\newDoc.docx");
Document clonDoc = (Document)doc.Clone(false);

short sectionCounter  = 1;
int pageCounter = 1;
HeaderFooter? footer = null;
HeaderFooter? firstPageFooter = null;

foreach (Section sec in doc.Sections)
{
    // Add the Section to the clone Document
    Node node = clonDoc.ImportNode(sec, true, ImportFormatMode.KeepSourceFormatting);
    clonDoc.AppendChild(node);

    // Create a Document to be able to save each individual section
    Document temp = (Document)clonDoc.Clone(false);

    // Get the current footer of the section
    var currentFooter = sec.HeadersFooters.FirstOrDefault(hf => ((HeaderFooter)hf).HeaderFooterType == HeaderFooterType.FooterPrimary) as HeaderFooter;
    var currentFirstPageFooter = sec.HeadersFooters.FirstOrDefault(hf => ((HeaderFooter)hf).HeaderFooterType == HeaderFooterType.FooterFirst) as HeaderFooter;

    sec.PageSetup.RestartPageNumbering = true;
    sec.PageSetup.PageStartingNumber = pageCounter;

    // Should add footer when the current section don't have any footer and the prevoius section had a footer to replicate
    if (currentFirstPageFooter == null && firstPageFooter != null)
    {
        // Add the footer to the section
        Node tempFooterNode = sec.Document.ImportNode(firstPageFooter, true, ImportFormatMode.KeepSourceFormatting);
        sec.AppendChild(tempFooterNode);
    }
    else if (currentFirstPageFooter != null)
    {
        firstPageFooter = currentFirstPageFooter;
    }

    // Should add footer when the current section don't have any footer and the prevoius section had a footer to replicate
    if (currentFooter == null && footer != null)
    {
        // Add the footer to the section
        Node tempFooterNode = sec.Document.ImportNode(footer, true, ImportFormatMode.KeepSourceFormatting);
        sec.AppendChild(tempFooterNode);
    }
    else if(currentFooter != null)
    {
        footer = currentFooter;
    }

    // Add the current Section to the Document
    Node tempSection = temp.ImportNode(sec, true, ImportFormatMode.KeepSourceFormatting);
    temp.AppendChild(tempSection);

    // Skip the 2 firsts sections to start counting pages
    if(sectionCounter > 2)
    {
        pageCounter += temp.PageCount;
    }

    // Save the Document containing only the section and increase the counter
    temp.Save(@$"C:\\Temp\\Section_{sectionCounter++}.docx");
}

// Save the whole cloned Document
clonDoc.Save(@"C:\\Temp\\newDoc_cloned.docx");

Please notice that this solution is tightly coupled with the input document.

Hi,
You gave solution it’s for particular document, but how can I see Header/Footer(Page number) in all individual sections in any document?

Here in Main Document Footer has Link to Previous in Microsoft Word but I don’t recognize it while Saving individual section in Aspose Word. In Aspose Word there is one property IsLinkedToPrevious, but it’s always show false. In Microsoft Word Link to Previous Header/Footer is active but in Aspose Word IsLinkedToPrevious is not getting true.
You can see section 4,5 in following document:
MainDoc.docx (151.6 KB)

@RajChauhan, this is a feature of MS Word you actually don’t have page numbers in sections 4 and 5 it’s a render feature. “If a Section does not have a HeaderFooter of a specific type or the HeaderFooter has no child nodes, this header/footer is considered linked to the header/footer of the same type of the previous section in Microsoft Word”, and for that reason you see all the pages with a page numbers for Sections 4 and 5 in MS Word app.