Remove the extra gaps in the document

Hi,
I am inserting the data into document using no.of html string contents. but finally my docuemnt look likes with extra spaces at some areas. I need to remove the extra gaps/line feeds… any method helps me to do this? PFA for my sample document.

Hi

Thanks for your inquiry. You can do it using DocumentVisitor. Please try using the following code:

// Remove empty paragraph from document
public void Test037()
{
    // Load document
    Document doc = new Document(@"Test037\in.doc");
    RemoveEmptyParagraph parRem = new RemoveEmptyParagraph();
    doc.Accept(parRem);
    doc.Save(@"Test037\Out.doc");
}
public class RemoveEmptyParagraph: DocumentVisitor
{
    private bool _delete = false;
    public override VisitorAction VisitParagraphStart(Paragraph paragraph)
    {
        if (string.IsNullOrEmpty(paragraph.ToTxt().Trim()))
            paragraph.Remove();
        return VisitorAction.Continue;
    }
}

Hope this helps.
Best regards,

Hi Andrey,
Thanks for your response, the code above provided by you is working fine for me, but unfortunately after using this code all the paragraphs looks bit closer. I need at leaset one blank paragraph/empty line b/w normal paragraph and Heading style paragraph, like:

1.Heading1
Some content of Heading1
1.1 Heading1.1 //needed at least one blank paragraph/empty line on above
content of Heading1.1
2. Heading2 //needed at least one blank paragraph/empty line on above
Some content of Heading1

Shall you please suggest on this or else please suggest on deleting empty paragraphs which are occuring morethan one time continuously?
Thanks,

Hi

Thanks for your request. I modified my code from the previous post. Please try using the following code:

// Remove empty paragraph from document
public void Test037()
{
    // Load document
    Document doc = new Document(@"Test037\in.doc");
    RemoveEmptyParagraph parRem = new RemoveEmptyParagraph();
    doc.Accept(parRem);
    doc.Save(@"Test037\Out.doc");
}
public class RemoveEmptyParagraph : DocumentVisitor
{
    public override VisitorAction VisitParagraphStart(Paragraph paragraph)
    {
        if (string.IsNullOrEmpty(paragraph.ToTxt().Trim()))
        {
            if (paragraph.NextSibling != null && paragraph.NodeType == NodeType.Paragraph && string.IsNullOrEmpty(paragraph.NextSibling.ToTxt().Trim()))
                paragraph.Remove();
        }
        return VisitorAction.Continue;
    }
}

Hope this helps.
Best regards,

Hi Andrey,

Thanks for your response, i am using this above code, it works fine for me, but i am facing the new problem as, if “in.doc” having the images, after using doc.Accept(parRem); the images are also removed in “out.doc”,

Could you please suggest me to overcome this?
Thanks,

Hi

Thanks for your inquiry. This occurs because Shape is child node of the paragraph you removed. Please try using the following code:

public class RemoveEmptyParagraph: DocumentVisitor
{
    public override VisitorAction VisitParagraphStart(Paragraph paragraph)
    {
        if (!paragraph.HasChildNodes)
            paragraph.Remove();
        return VisitorAction.Continue;
    }
}

Hope this helps.
Best regards,

Hi Andrey,

Thanks for your response, i tried the above code now images are coming fine, but the empty paragraphs are not removing, the condition is fails my previous thread requirements.

Could you please suggest one condition, which removes the empty paragraphs/gaps in the document as well as images should not be delete.

Thanks,

Hi

Thanks for your request. Could you please attach your current document (with images and extra gaps) here for testing. I will investigate the issue and provide you more information.
Best regards,

Hi Andrey,

Thanks for your response, please find my test document having extra gaps and images. Please provide the way to “Delete empty paragraphs which are occured morethan one time continuously without deleting images in the document”.

Thanks,

Hi

Thank you for additional information. I think the code which I provide you earlier, works absolutely as you need. I cannot find any empty paragraphs in my output document. The “empty” paragraphs which you can see actually contains some MACROBUTTON AutoText, please press Alt-F9 to see them.
Best regards,