Blank page at the end of the finished document

Hi,
I use aspose.word to generate a report using a container document and a single page word template that contains tables and smart tags. I am able to generate a report with multiple pages, however, there’s always a blank page at the end of the report.
Question: How do I get rid of this blank page which is always present at the end of the document?
Regards,
AD

Hi
Thanks for your inquiry. Could you please provide me your code for testing and attach sample document? I will investigate the problem and try to help you.
Best regards.

Hi Alexey,
I learned from another posting that you can delete the 1st page of a multiple append document using:
Document.FirstSection.Remove()
Is there a method to remove the last page from a document. I’ve already tried:

  1. Document.LastSection.Remove() - which got rid of the last appended document altogether but not the last (blank) page.
  2. Document.LastChild.Remove() - which got rid of the last appended document altogether but not the last (blank) page.

Thank you,
AD

Alexey,
Please find attached sample code.
Regards,

AD

ContainerDocument

Template Document

Hi
Thank you for additional information. There is no blank section at the end of your document. There are few empty paragraphs. You can just remove these paragraphs before saving document. Please try using the following code:

'Remove empty paragraphs at the end of document
While (Not doc.LastSection.Body.LastParagraph.HasChildNodes)
If (Not doc.LastSection.Body.LastParagraph.PreviousSibling.NodeType.Equals(NodeType.Paragraph)) Then
Exit While
End If
doc.LastSection.Body.LastParagraph.Remove()
End While

Hope this helps.
Best regards.

I currently have the same issue, the interesting part is that if I save the document as a word file, I do not get the blank page at the end, it only happens when i save as PDF

I tried to do what alexey says, but it did not help

Hi Amrom,

Thanks for your request. Could you share more details about the issue you are having? Also please attach your input and output documents.

Thanks,

The issue is very simple, I am trying to print labels trough words, I created a template document, and do a mail merge on it, when I print the document, or save it as a Word document, it works perfect, but when i save as PDF I get a blank page at the end.

Hi

Thank you for additional information. As I can see there are two pages in your input document and one of them is blank. In the output PDF I see the same two pages. I use the latest version of Aspose.Words (9.0.0) for testing.
Best regards,

I am sorry, I don’t see more then 1 page in the input document, which version of Word are you using to look at this? I am using Word 2007

Hi

Thank you for additional information. I use Word 2007 for testing. Please see the attached screenshot.
Best regards,

Thanks, I didn’t realize that I need to look with paragraph marks showing, now it works

Hi Andrey,
Is there a way i can check for these blank paragraphs using java ?

-Afzal

Hi Afzal,
Thanks for your inquiry.
Sure, you can find the code to remove any empty paragraphs at the end of the document below.

while (!doc.getLastSection().getBody().getLastParagraph().hasChildNodes())
{
    if (doc.getLastSection().getBody().getLastParagraph().getPreviousSibling().getNodeType() != NodeType.PARAGRAPH)
        break;
    doc.getLastSection().getBody().getLastParagraph().remove();
}

Please note that a document should always end in a paragraph, so in some instances you may find that during save an empty paragraph is added if for example the previous node is a table.
Thanks,

Hi …
Thank you for the quick reply. I have been trying to implement this but keep running into a NullPointerException. Im still in the process of narrowing it down but im guessing it breaks down somewhere in the getPreviousSibling() call. Im also attaching the concatenated document which is generated without the above piece of code. If you take a look at it, you’ll see that the last 2 pages of the document are blank except for the header and footer. In MS Word 2007 view, it shows a number of page breaks on those 2 pages.
Im not sure if the above code is actually looking at the final footer instead of the content on the page. Is there anyway we can figure this out ?

Hi
Thanks for your request. Please try using the following modified code:

Document doc = new Document("C:\\Temp\\report.docx");
while (!doc.getLastSection().getBody().getLastParagraph().hasChildNodes())
{
    if (doc.getLastSection().getBody().getLastParagraph().getPreviousSibling() != null &&
    doc.getLastSection().getBody().getLastParagraph().getPreviousSibling().getNodeType() != NodeType.PARAGRAPH)
        break;
    doc.getLastSection().getBody().getLastParagraph().remove();
    // If the current section becomes empty, we should remove it.
    if (!doc.getLastSection().getBody().hasChildNodes())
        doc.getLastSection().remove();
    // We should exit the loop if the document becomes empty.
    if (!doc.hasChildNodes())
        break;
}
doc.save("C:\\Temp\\out.docx");

Hope this helps.
The code does not take in account headers and footers. The modified code I provided just removes empty paragraphs and sections from the end of the document. Empty section means section without content in the main body.
Best regards,

Yep !!! That did the trick !
Thank you Alexy !