Possible to keep the header/footer when sectionbreak is removed?

Hi we are having issue when updated to newer version of Aspose.Words.

It appears to exhibit different behaviour on new version: when section break is essentially remove by removing the bookmark containing it, it remove the previous sections header/footer in the new version, which we want to keep, and the header/footer is preserved on the older version.

Here is the simplified code:

Bookmark? bmToRemove = null;
foreach(var bm in doc.Range.Bookmarks)
{
    if (bm.Name == "FSR")
    {
        bmToRemove = bm;
    }
}

if (bmToRemove is not null)
{
    bmToRemove.Text = "";
    bmToRemove.Remove();
}

Iā€™ve also attached the input file and two output files from different versions (18.8 and 22.5).BookmarkSecbrkInput.docx (15.0 KB)
BookmarkSecbrkProcessedV18.docx (10.5 KB)
BookmarkSecbrkProcessedV22.docx (10.5 KB)

I am hoping that the header/footer can be kept as the older version, so that we donā€™t need search existing documents to update.

Update: Now after install different versions of Aspose.Words, I can narrow down the change has been introduced from21.10.

@DougT Aspose.Words behavior has indeed changed in this respect since version 18.8. It has become similar to MS Word behavior, and you can verify this by running

ActiveDocument.Bookmarks("FSR").Range.Delete

Just like MS Word, Aspose.Words moves the contents of the first section to the second one along with the header/footer, while leaving the attributes of the document root section. Actually, your Header is not deleted, you can see it via setting

doc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = true;

after deleting the bookmark. However, please, keep in mind that you may need to copy other section attributes (such as page sizes) in case they had different values ā€‹ā€‹before this deletion.

RemoveSecBreak.zip (249.4 KB)
Thanks Vadim.

Yes Iā€™ve tested the code you provided to put the header back, looks like the header are still there, but the PageSetup was merged back to the section following it? I think it could be the work around I am looking.

I also agree that what the new version of Aspose doing seems mapping Words behaviour, for example, when I delete the bookmark/Sectionbreak using Microsoft Words, the header/footer also disappeared, to me thatā€™s exactly the new Aspose is doing. but I am still trying to understand the exact changes caused the change of behaviour and trying to reverse the change for our case, as there are documents to be modified and some are tricky to get right and testing.

On the other hand I donā€™t agree with you that the change is from version 18.8, Iā€™ve testing a few different versions of Aspose.Words, and based on my test, the behaviour is maintained until version 21.10. Please see my attached project which using version 21.9 and the header are still maintained, but not on version 21.10.

I do not state that the changes were carried out in version 18.8, I pointed out that the behavior has changed since then.

The difference from the previous behavior is that the content of the previous section is moved to the next one, and not vice versa. To demonstrate the difference Iā€™ve attached your modified document to the post. I changed the root section PageSize there, you can run the code on the current and the old versions and see the difference. So to keep the behavior similar to the old version, you will need to copy the values from the previous section to the new one.
BookmarkSecbrkInputEdtd.docx (14.0 KB)

Ok, thanks Vadim, what you said make sense.

So in the newer version, when the sections get merged, all the content from the section before the section break atomically inherent style form the later section, is that right?

I think Iā€™ll have to copy the content from later section into the previous section, then delete the later section, basically reverse the new behaviour by code.

Thanks again for the help and explanation.

You donā€™t need to copy the content of the sections, Aspose.Words code does it for you, this is not a completely obvious and safe task. To achieve the previous behavior, you need to copy the PageSetup. Copy PrevSection.PageSetup values to NextSection.PageSetup. It is safe.

Hi Vadim,

I am having issue with the approach you suggested, I got error message ā€œCS0200 Property or indexer ā€˜Section.PageSetupā€™ cannot be assigned to ā€“ it is read onlyā€.

@DougT You need to copy the values from PageSetup.

PageSetup prevPage = doc.Sections[0].PageSetup;
PageSetup nextPage = doc.Sections[0].PageSetup;

nextPage.DifferentFirstPageHeaderFooter = prevPage.DifferentFirstPageHeaderFooter;
nextPage.PageHeight = prevPage.PageHeight;
nextPage.PageWidth = prevPage.PageWidth;
...

Oh, thanks Vadim, I thought there will be simpler approach. Yes, thatā€™s what Iā€™d do.

@DougT Please feel free to ask in case of any issues, we will be glad to help you.

1 Like