How to remove blank pages in word files before appending them?

Hi Aspose,

I have 2 word templates used to fill dynamic data to bookmarks on them. After the necessary data filled to those templates, I will append the contents of them into a destination word file. However, I got an issue about blank page while appending those files.

In the template #1, I filled a table data to its bookmark and then saving this temp as another output file. The output file generated 2 pages. Page #1 contains the table data, page #2 is a blank page.
In the template #2, i filled the other dynamic data to its bookmark and saved it as another output file also.

After I have 2 output files, I will append the contents of them into a word file. However, the destination file still contains a blank page, which appears at the end of output file of template #1.

So, my question is: How to remove/clear all blank pages at the end of all output files before appending the contents of them into a destination file? I spent many times to research and find the solution on the internet and at Aspose site. Unfortunately, I cannot find any solutions to solve this issue. Pls help me to do this work.

Thanks so much for your help.

Hi ChienVH,

Thanks for your inquiry. Please note that MS Word documents are flow documents and there is no page concept in MS Word documents. Pages are created by MS Word on the fly and unfortunately there is no direct way to remove a Blank page.

You can try removing empty paragraphs from the end of your output documents. Please use the following code snippet to achieve your requirements. Hope this helps you.

// Open template.
Document doc = new Document(@"Test001\in.doc");
// Remove the empty paragraphs if necessary.
while (!doc.LastSection.Body.LastParagraph.HasChildNodes)
{
    if (doc.LastSection.Body.LastParagraph.PreviousSibling != null &&
    doc.LastSection.Body.LastParagraph.PreviousSibling.NodeType != NodeType.Paragraph)
        break;
    doc.LastSection.Body.LastParagraph.Remove();
    // If the current section becomes empty, we should remove it.
    if (!doc.LastSection.Body.HasChildNodes)
        doc.LastSection.Remove();
    // We should exit the loop if the document becomes empty.
    if (!doc.HasChildNodes)
        break;
}
// Save output.
doc.Save(@"Test001\out.doc");