We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Remove blank pages with duplicate page breaks using C#

We are trying to delete consecutive page breaks with in document so that blank pages can be removed. But they are not deleted. Please suggest on whether we are going in right way. Attached is sample document and code

static void Main(string[] args)
{

        string FileIn = @"C:\Users\raja_c\Downloads\blankpage.docx";
        string FileOut = @"C:\Users\raja_c\Downloads\blankpage_cp.docx";
        License license = new License();
        license.SetLicense("Aspose.Total.lic");
        Document doc = new Document(FileIn);
		foreach (Section sct in doc.Sections)
		{
			NodeCollection nc = sct.Body.GetChildNodes(NodeType.Paragraph, true);
			foreach (Paragraph p in nc)
			{
				if (p.GetText().EndsWith("\f\f"))
				{
				   p.Remove();
				}

			}
		}
        doc.Save(FileOut);
    }

blankpage.zip (9.5 KB)

@crshekharam

Please use the following code example to achieve your requirement. Hope this helps you.

Document doc = new Document(MyDir + "blankpage.docx");
for (int i = 0; i < doc.Sections.Count - 1; i++)
{
    Section section = doc.Sections[i];
    Section nextsection = doc.Sections[i + 1];
    if (section.Body.LastParagraph.GetText().Contains(ControlChar.PageBreak)
        && nextsection.Body.FirstParagraph.GetText().Contains(ControlChar.PageBreak))
    {
        section.Body.LastParagraph.Range.Replace(ControlChar.PageBreak, "");
        nextsection.Body.FirstParagraph.Range.Replace(ControlChar.PageBreak, "");
    }
}  
                 
doc.Save(MyDir + "20.4.docx");

Thanks. It worked