Dear Aspose Support Team,
We are using Aspose.Words to generate Word documents programmatically and have encountered an issue related to page breaks.
When we insert a page break using DocumentBuilder.InsertBreak(BreakType.PageBreak)
, an empty paragraph is automatically added immediately after the break. This appears to align with the default behavior of Microsoft Word, which inserts a paragraph following a page break.
However, we are unable to programmatically access or remove this paragraph using Aspose APIs. It does not seem to be identifiable in the document structure, which prevents us from modifying or replacing it. This is leading to unintended empty paragraphs appearing in our generated documents.
We would appreciate your guidance on the following:
- Is there a recommended way to access or suppress the empty paragraph that follows a page break?
- Is this a known limitation or behavior in Aspose.Words?
- Are there any alternative approaches or best practices you suggest to prevent this empty paragraph from being rendered in the final output when inserting page break?
Please let us know if you need any code samples or document examples to reproduce the issue.
Looking forward to your support.
@e.scanlan
Cause
The behavior you’re experiencing, where an empty paragraph is automatically added after a page break, is consistent with how Microsoft Word handles page breaks. This is a known behavior in Aspose.Words, as it mimics the Word document structure.
Solution
To address this issue, you can programmatically remove the empty paragraph after inserting a page break. Here’s a recommended approach:
- After inserting the page break, check if the next paragraph is empty.
- If it is empty, remove it.
Code
Here’s a sample code snippet demonstrating how to achieve this:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a page break
builder.InsertBreak(BreakType.PageBreak);
// Check if the next paragraph is empty and remove it
if (builder.CurrentParagraph != null && builder.CurrentParagraph.NextSibling != null)
{
Paragraph nextParagraph = (Paragraph)builder.CurrentParagraph.NextSibling;
if (string.IsNullOrWhiteSpace(nextParagraph.GetText()))
{
nextParagraph.Remove();
}
}
doc.Save("output.docx");
Next Steps
Implement the above code in your project. If you continue to face issues or if the empty paragraph is not being removed as expected, please share a sample document or additional details, and I will assist you further.
Thank you for your inquiry, and I look forward to your feedback!
Thank you for your prompt response.
We tried the exact same code provided by you but we got NextSibling as Null on running the code and hence the empty paragraph after page break does not got removed.
Attaching the screenshot of the values we are getting and the code snippet for the reference. Also the word document generated.
epi231022_imatinib_teva_en_20250723104514.docx (12.0 KB)
@e.scanlan Paragraph break in MS Word document is a simple special character it is always inserted inside paragraph.:
The same you can see in internal document XML:
<w:p w14:paraId="672A6659" w14:textId="77777777" w:rsidR="00A77B3E" w:rsidRDefault="00BE63B2">
<w:r>
<w:br w:type="page"/>
</w:r>
</w:p>
I think in your case to avoid such situation, you can use ParagraphFormat.PageBreakBefore option instead of inserting explicit page break.