C# Code to Remove Empty Pages from Word Document using Aspose.Words for .NET API

Hi Tahir,
I wanted to remove the empty pages too, but after the “section.Remove();” is the doc.LastSection null. If I debug in Visual Studio and I check the document object, then is the LastSection not null any more. What refreshes this property property or the collection behind it?

@david.csillik.messerli

In your case, we suggest you following solution.

  1. Please convert the Word document to PDF using Aspose.Words.
  2. Remove the empty pages from the PDF using Aspose.PDF.
  3. Convert PDF to DOCX using Aspose.PDF.

Below code example shows how to remove the empty pages from PDF.

Aspose.Pdf.Document inputDoc = new Aspose.Pdf.Document(MyDir + "input.pdf");
Aspose.Pdf.Document outputDoc = new Aspose.Pdf.Document();
foreach (var page in inputDoc.Pages)
{
    if (page.IsBlank(0.01d))
        continue;
    else
        outputDoc.Pages.Add(page);
}

Aspose.Pdf.DocSaveOptions saveOptions = new Aspose.Pdf.DocSaveOptions();
// Specify the output format as DOCX
saveOptions.Format = Aspose.Pdf.DocSaveOptions.DocFormat.DocX;
// Save document in docx format
outputDoc.Save("output.docx", saveOptions);

Hi Tahir,
Thank you for your answer and for the new ticket!
Can I ask, what is the difference? In the other ticket Mr. Jaswanth used also Aspose.Words. And is my issue a bug or a feature? If it is a bug, and there is a workaround - there must be, because in Visual Studio on mouse hover appears the right value -, no problem, I make it until you fix it.
I cannot use Aspose.PDF, because we have not decided yet, to use it in the production: I had performance problems with it in the first tests, so I opened a consulting issue:

(If I create a document and I start to insert a whole bunch of images, it starts to use a lot of memory and getting slower. And because our clients still have old PCs, it is very important. I hope there is a more performant solution, your product it not a new one, I am sure you have already tested such a scenario, or one of your client)

@david.csillik.messerli I recently answered exactly the same question here java - How can I get rid of blank pages in aspose.words? - Stack Overflow
In short there is no easy way to determine empty pages in Word documents. The first option is to remove explicit page breaks from your document, as I suggest in the post above.
Also you can try using new Aspose.Words feature - document splitting, to achieve this.
Document.ExtractPages | Aspose.Words for .NET
You can try splitting your document to small one page documents and check if the documents has content. Than concatenate not empty documents again. Please let me know if you need more assistant with this.

1 Like

A post was split to a new topic: Remove Blank Pages from Word Document with Headers Footers using C# .NET or Java