Hii , I want to remove Blank pages from Document so I use this Snippest
but in output one extra space add at last line of last paragraph
private void RemoveBlankPages()
{
try
{
Document sourceDoc = new Aspose.Words.Document(@"C:\\Temp\\sourceDoc.docx");
Document NewDoc = (Document)sourceDoc.Clone(false);
NewDoc.RemoveAllChildren();
NewDoc.CompatibilityOptions.OptimizeFor(Aspose.Words.Settings.MsWordVersion.Word2019);
for (int i = 0; i < sourceDoc.PageCount; i++)
{
Document Page = (Document)sourceDoc.Clone(false);
Page.RemoveAllChildren();
Page.CompatibilityOptions.OptimizeFor(Aspose.Words.Settings.MsWordVersion.Word2019);
Page = sourceDoc.ExtractPages(i, 1);
var pageText = Page.FirstSection.Body.ToString(SaveFormat.Text) + Page.LastSection.Body.ToString(SaveFormat.Text);
var shapeCount = Page.FirstSection.Body.GetChildNodes(NodeType.Shape, true).Count + Page.LastSection.Body.GetChildNodes(NodeType.Shape, true).Count;
if (string.IsNullOrEmpty(pageText.Trim()) && shapeCount == 0)
continue;
NewDoc.AppendDocument(Page, ImportFormatMode.KeepSourceFormatting);
}
if (NewDoc.Sections.Count > 0)
{
NewDoc.FirstSection.PageSetup.RestartPageNumbering = true;
}
sourceDoc.RemoveAllChildren();
sourceDoc.CompatibilityOptions.OptimizeFor(Aspose.Words.Settings.MsWordVersion.Word2019);
sourceDoc = (Document)NewDoc.Clone(true);
sourceDoc.Save(@"C:\\Temp\\Output.docx", SaveFormat.Docx);
}
catch (Exception e)
{
throw e;
}
}
SourceDoc.docx (2.0 MB)
OutputDoc.docx (2.0 MB)
Err.png (39.8 KB)
Err2.png (44.7 KB)