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.