file.docx (32.6 KB)
To delete blank spaces in a Word document using Aspose.Words, you can utilize the RemoveBlankPages method. This method effectively removes any blank pages from your document, which helps eliminate unwanted spaces.
Cause
Blank pages can occur due to various reasons, such as extra paragraph marks, page breaks, or formatting issues, leading to an unprofessional appearance in your document.
Solution
- Load your document using the
Documentclass. - Call the
RemoveBlankPagesmethod on the document instance.
Code Example
Here’s a simple code snippet to demonstrate how to do this:
using Aspose.Words;
// Load the document
Document doc = new Document("file.docx");
// Remove blank pages
doc.RemoveBlankPages();
// Save the modified document
doc.Save("modified_file.docx");
Next Steps
- Ensure you have the Aspose.Words library referenced in your project.
- Replace
"file.docx"with the path to your actual document. - Run the code to remove any blank pages and save the modified document.
This approach will help you maintain a clean and professional document. If you have any further questions or need additional assistance, feel free to ask!
I just want to remove blank space
can anyone please help?
@Soumen2001 You can use the following code to remove blank paragraphs at the end of a document:
Document doc = new Document("file.docx");
// Get the last section of the document.
Section lastSection = doc.LastSection;
Body body = lastSection.Body;
// Iterate backwards through paragraphs in the last section.
for (int i = body.Paragraphs.Count - 1; i >= 0; i--)
{
Paragraph paragraph = body.Paragraphs[i];
// Check if paragraph is empty (no text and no other nodes).
if (paragraph.GetText().Trim() == string.Empty)
// Remove the empty paragraph.
paragraph.Remove();
else
// Stop when we hit a non-empty paragraph.
break;
}
doc.Save("output.docx");
@Soumen2001 These look like empty bookmarks. You can delete them using the following code:
foreach (Bookmark bookmark in doc.Range.Bookmarks)
{
if (bookmark.Name.Equals(string.Empty))
bookmark.Remove();
}
But removing blank paragraphs also removes blank lines from the provided document, which is as follows:
how it is possible. If there is bookmark then bookmark name also be there. So how can i delete the bookmark if the name is not empty?
@Soumen2001 Unfortunately, I can’t reproduce the problem. Here is a document created using the first code provided here How to delete this bank space in the document - #5 by vyacheslav.deryushev
output.docx (25.1 KB)
All blank paragraphs have been removed and there are no other spaces.
@Soumen2001 You can use following code:
Document doc = new Document("input.docx");
foreach (Section section in doc.Sections)
{
foreach (Table table in section.Body.Tables)
{
Paragraph nextParagraph = table.NextSibling as Paragraph;
if (nextParagraph.GetText().Trim() == string.Empty)
{
Node nextSibling = nextParagraph.NextSibling;
while (nextSibling != null && nextSibling.GetText().Trim() == string.Empty)
{
Paragraph toRemove = nextParagraph;
nextParagraph = nextParagraph.NextSibling as Paragraph;
toRemove.Remove();
}
}
}
}
doc.Save("output.docx");
@Soumen2001 Sorry for the old code. Here’s the code:
Document doc = new Document("input.docx");
foreach (Section section in doc.Sections)
{
foreach (Table table in section.Body.Tables)
{
Paragraph nextParagraph = table.NextSibling as Paragraph;
if (nextParagraph.GetText().Trim() == string.Empty)
{
while (nextParagraph.NextSibling != null && nextParagraph.NextSibling.GetText().Trim() == string.Empty)
{
Paragraph toRemove = nextParagraph;
nextParagraph = nextParagraph.NextSibling as Paragraph;
toRemove.Remove();
}
}
}
}
doc.Save("output.docx");
I have just tested with this code and the input and output are attached below
file.docx (38.7 KB)
treated_file.docx (30.3 KB)
@Soumen2001 Do you want to delete not only blank lines but also the section break on the second page?
yes I have to remove section break
@Soumen2001 You can use this code:
Document doc = new Document("input.docx");
// Remove section breaks from the document.
for (int i = doc.Sections.Count - 2; i >= 0; i--)
{
doc.LastSection.PrependContent(doc.Sections[i]);
doc.Sections[i].Remove();
}
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
// Iterate through paragraphs in reverse order to safely remove nodes.
for (int i = paragraphs.Count - 1; i >= 0; i--)
{
Paragraph paragraph = (Paragraph)paragraphs[i];
// Check if the paragraph is empty and if the next node is also empty.
if (string.IsNullOrWhiteSpace(paragraph.GetText().Trim()) &&
(paragraph.NextSibling == null || string.IsNullOrWhiteSpace(paragraph.NextSibling.GetText().Trim())))
{
paragraph.Remove();
}
}
doc.Save("output.docx");




