Bookmarks removed when removing blank pages

hi,

we have this code to remove blank pages from a document

var stream = new MemoryStream();
var listOfBlankPageNumbers = new List<int>();
int pagecount = document.PageCount;
for (int i = 0; i < pagecount; i++)
{
    var page = document.ExtractPages(i, 1);
    string pagetext = page.FirstSection.Body.ToString(SaveFormat.Text);
    int shapeCount = page.FirstSection.Body.GetChildNodes(NodeType.Shape, true).Count;
    if (string.IsNullOrWhiteSpace(pagetext?.Replace("\r", "")?.Replace("\n", "")?.Replace("\a", "")?.Replace("\t", "")?.Replace("\v\r", "")) && shapeCount == 0)
    {
        listOfBlankPageNumbers.Add(i);
    }
}

if (listOfBlankPageNumbers?.Any() == true)
{
    listOfBlankPageNumbers.Insert(0, -1);
    listOfBlankPageNumbers.Add(pagecount);

    // Add all the non-empty pages to the final document
    Document nonEmptyDocument = (Document)document.Clone(false);
    nonEmptyDocument.RemoveAllChildren();

    int index;
    int count;
    for (int iCount = 1; iCount < listOfBlankPageNumbers.Count; iCount++)
    {
        index = (int)listOfBlankPageNumbers[iCount - 1] + 1;
        count = (int)listOfBlankPageNumbers[iCount] - index;

        if (count > 0)
        {
            Document subDoc = document.ExtractPages(index, count);
            subDoc.FirstSection.PageSetup.RestartPageNumbering = false;
            nonEmptyDocument.AppendDocument(subDoc, ImportFormatMode.UseDestinationStyles);
        }
    }
    nonEmptyDocument.Save(stream, SaveFormat.Docx);

}
else
{
    document.Save(stream, SaveFormat.Docx);
}

return stream;

but when we have a document with two bookmarks
the second bookmark is removed from the document. can you please advise.

we observed that the bookmark that was not removed from the page was editable and the one removed from the page was non-editable.

@randomuser123 I suspect this occurs because the bookmark is on the blank page and your conditions that detects empty pages do not check for bookmarks. You can try modifying your code like this:

var page = document.ExtractPages(i, 1);
string pagetext = page.FirstSection.Body.ToString(SaveFormat.Text);
int shapeCount = page.FirstSection.Body.GetChildNodes(NodeType.Shape, true).Count;
int bookmakrsCount = page.Range.Bookmarks.Count;
if (string.IsNullOrWhiteSpace(pagetext?.Replace("\r", "")?.Replace("\n", "")?.Replace("\a", "")?.Replace("\t", "")?.Replace("\v\r", "")) && shapeCount == 0 && bookmakrsCount == 0)
{
    listOfBlankPageNumbers.Add(i);
}

If this does not help, please attach your problematic document here for testing, we will check it and provide you more information.