Hi ,
we have a 30 page document with pagenumber in the footer. after deleting few pages, the pagenumber in the footer is the same as before instead of continuing.
can you please advise.
this is our code to remove blank pages:
private MemoryStream RemoveBlankPages(Document 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);
if (string.IsNullOrWhiteSpace(pagetext))
{
listOfBlankPageNumbers.Add(i);
}
}
if (listOfBlankPageNumbers?.Any() == true)
{
listOfBlankPageNumbers.Insert(0, -1);
// 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)
nonEmptyDocument.AppendDocument(document.ExtractPages(index, count), ImportFormatMode.UseDestinationStyles);
}
if (document.PageCount > (int)listOfBlankPageNumbers.Last() + 1)
{
index = listOfBlankPageNumbers.Last() + 1;
count = document.PageCount - index;
nonEmptyDocument.AppendDocument(document.ExtractPages(index, count), ImportFormatMode.UseDestinationStyles);
}
nonEmptyDocument.Save(stream, SaveFormat.Docx);
}
else
{
document.Save(stream, SaveFormat.Docx);
}
return stream;
}