Is there a way to replace or remove a paragraph break?

Hi,

In working with Aspose Words, one of the problems/limitations I’ve run into is that when performing mail-merges that have loops, a paragraph break is automatically added after each loop iteration.

Is there any way to prevent that, or remove the paragraph break after-the-fact?

I thought I found a solution based on this old forum post: Replacing line breaks with paragraph breaks using replace evaluator

However, when I try to replace a paragraph break, I receive the following error:

“The match includes one or more special or break characters and cannot be replaced”

So is there no way to remove a paragraph break after it was added/mail-merged-into to the document?

Thanks,
Dan

@dhartley

Thanks for your inquiry. We will appreciate it if you please ZIP and share your input document with extra paragraph breaks along with your sample code to remove these paragraph breaks. We will look into the issue and will guide you accordingly.

My overall code is much more complex than the question I’m asking, but for full disclosure, what I’m doing doing is executing mail-merges which can contain loops (TableStarts/TableEnds). The problem is that an unwanted paragraph break is added to the document after each iteration through the loop. So I followed this old forum post: Section break (with page break) using mail merge - #2 by tahir.manzoor and am using an IFieldMergingCallback to insert bookmarks into the document during the mail-merge process after the last merge-field in the loop has been processed. After the mail-merge process is complete, I’m moving through the document’s bookmarks, and trying to remove the unwanted paragraph breaks.

So the code that’s throwing the error is the following:

// Find specially-named bookmarks and remove paragraph breaks
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (Bookmark bm in doc.Range.Bookmarks)
{
    var bookmarkName = bm.Name;

    if (bookmarkName.StartsWith("RemoveParagraphBreak"))
    {
        if (builder.CurrentParagraph != null && builder.CurrentParagraph.Range != null)
        {
            builder.CurrentParagraph.Range.Replace(new Regex(ControlChar.ParagraphBreak), new ParagraphReplaceEvaluator(), false);
        }
    }
}
doc.Range.Bookmarks.Clear();

And my “ParagraphReplaceEvaluator” is simple and looks like this:

internal class ParagraphReplaceEvaluator : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        e.Replacement = "";
        return ReplaceAction.Replace;
    }
}

But the error I get is:

System.NotSupportedException: The match includes one or more special or break characters and cannot be replaced

@dhartley

Thanks for your feedback. As requested above please also share your sample word document(Mail Merge document with extra paragraph break). It will help us to replicate your issue exactly and address it accordingly.

We are sorry for the inconvenience.

I’m not sure how to attach my sample word document, but it’s a simple test document that I’m playing with and it looks like this:

«TableStart:Sports»«Sports_Name»«TableEnd:Sports»

That’s literally the only content of the Word doc and my source data is just a list of sports like “Hockey”, “Basketball”, and “Football”.

But instead of the output being on 1 line, the output is like this:

Hockey
Basketball
Football

See how the extra paragraph break is added after each item? Also, if I change the document to be this:

«TableStart:Sports»
«Sports_Name»«TableEnd:Sports»

Then the output is changed to:

Hockey

Basketball

Football

Once again an extra paragraph break is added after each item.

Hope that makes sense.

@dhartley

As already suggested above please ZIP your Word document (Mail Merge output document with extra paragraph break) and attach to the post using upload option. image.png (2.1 KB)

Here you go.Test.zip (12.9 KB)

@dhartley

Thanks for sharing your source document. You can get expected results with use of MailMerge.UseWholeParagraphAsRegion Property. Please set this property false to get data on same line. Hopefully it will help you to accomplish the task. However if there is some difference in your requirement and my understanding then please share your expected output document here as well. Test.zip (25.2 KB)

Document doc = new Document(@"test.docx");
doc.MailMerge.UseWholeParagraphAsRegion = false;
doc.MailMerge.ExecuteWithRegions(dt);            
doc.Save("test_AW177.pdf");

Edit: I have further investigated the scenario and updated my reply.

Thanks Tilal, that UseWholeParagraphAsRegion property looks promising. Unfortunately, our existing templates may need to be modified as it does change the looping behavior/format. But it definitely gives me more control over the spacing and paragraph breaks and might do the trick. Thanks for the help!