Problem with Aspose Words with extra spacing after bulleted list

Ok, I am close. If the html has more than one paragraph, the current paragraph is the first paragraph, I need to do that on the last paragraph of the html that was inserted. Is there an easy way to do that? Thank you so much for your help!

@pdeangelo sorry can you please post the HTML code that you are using, I created a simple example with 2 paragraphs and I’m getting the last paragraph as value of builder.CurrentParagraph:

var html = @"<style type=""text/css""> 
    ol,ul {margin: 0px 0px -100px 0px;} 
</style>
<p>Test Drug 8/31/2022 a quantity of 90 for 30 days has been approved for 6 months from 4/18/2023 to 10/15/2023.</p>
<p>Test Manual line break 2 paragraphs</p>";

Document doc = new Document("C:\\Temp\\input.docx");

FindReplaceOptions opt = new FindReplaceOptions()
{
    ReplacingCallback = new ReplaceWithHtmlHandler()
};
doc.Range.Replace("$Rationale", html, opt);

doc.Save("C:\\Temp\\output.docx");
public class ReplaceWithHtmlHandler : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
    {
        DocumentBuilder builder = new DocumentBuilder((Document)args.MatchNode.Document);
        Paragraph parent = ((Run)args.MatchNode).ParentParagraph;

        parent.RemoveAllChildren();

        Run tempRunNode = new Run(parent.Document);
        parent.AppendChild(tempRunNode);
        builder.MoveTo(tempRunNode);
        builder.InsertHtml(args.Replacement, HtmlInsertOptions.RemoveLastEmptyParagraph | HtmlInsertOptions.UseBuilderFormatting);

        if(builder.CurrentParagraph != null)
        {
            Console.WriteLine(builder.CurrentParagraph.Range.Text);
            builder.CurrentParagraph.ParagraphFormat.SpaceAfter = 50;
        }

        (tempRunNode).Remove();

        return ReplaceAction.Skip;
    }
}

input.docx (24.7 KB)
output.docx (21.9 KB)

I had it after the tempnode.remove, I placed it before and now it works. Thanks so much!

1 Like

Hi Eduardo,

I am facing a similar issue with a mail merge version. There is an extra line break between the letter checkbox mail merge and rationale mail merge. This usually works but never tried it with the html included that has check boxes. Do you know why? LetterWithCheckBox.zip (47.0 KB)

@pdeangelo You can modify your code like the following to remove an extra paragraph after inserting table from HTML:

var mailMergeIssue = @"<table><tr><td style='vertical - align: top; width: 20px; '> [  ]</td><td> <font face='Arial, serif'><font style='font - size: 11pt; '>We determined that your requested services are <strong>not medically necessary</strong> because the services do not meet the reason(s) checked below: <em>(See Rule 59G-1.010)</em></font></font> </td></tr><tr><td style='vertical - align: top; width: 20px; '> [  ]</td><td> <font face='Arial, serif'><font style='font - size: 11pt; '><p style='margin - left: 40px; '>Must be needed to protect life, prevent significant illness or disability, or alleviate severe pain.</p></font></font> </td></tr><tr><td style='vertical - align: top; width: 20px; '> [  ]</td><td> <font face='Arial, serif'><font style='font - size: 11pt; '><p style='margin - left: 40px; '>Must be individualized, specific, consistent with symptoms or diagnosis of illness or injury and not be in excess of the patient’s needs.</p></font></font> </td></tr><tr><td style='vertical - align: top; width: 20px; '> [  ]</td><td> <font face='Arial, serif'><font style='font - size: 11pt; '><p style='margin - left: 40px; '>Must meet accepted medical standards and not be experimental or investigational.</p></font></font> </td></tr><tr><td style='vertical - align: top; width: 20px; '> [  ]</td><td> <font face='Arial, serif'><font style='font - size: 11pt; '><p style='margin - left: 40px; '>Must be able to be the level of service that can be safely furnished, and for which no equally effective and more conservative or less costly treatment is available statewide.</p></font></font> </td></tr><tr><td style='vertical - align: top; width: 20px; '> [  ]</td><td> <font face='Arial, serif'><font style='font - size: 11pt; '><p style='margin - left: 40px; '>Must be furnished in a manner not primarily intended for convenience of the recipient, caretaker, or provider. </p><p style='margin - left: 40px; '><em>(The convenience factor is not applied to the determination of the medically necessary level of private duty nursing (PDN) for children under the age of 21.)</em></p></font></font> </td></tr><tr><td style='vertical - align: top; width: 20px; '> [  ]</td><td> <font face='Arial, serif'><font style='font - size: 11pt; '>The requested <strong>service is not a covered benefit. </strong></font></font> </td></tr><tr><td style='vertical - align: top; width: 20px; '> [  ]</td><td> <font face='Arial, serif'><font style='font - size: 11pt; '><strong>Other authority</strong></font></font> </td></tr><tr><td style='vertical - align: top; width: 20px; '> [x]</td><td> Test checkbox #1 - edited on 3/27/2019 </td></tr><tr><td style='vertical - align: top; width: 20px; '> [x]</td><td> Test add 11/30/21 Active/Checked </td></tr><tr><td style='vertical - align: top; width: 20px; '> [  ]</td><td> Test added 8/26/19. Edit. </td></tr><tr><td style='vertical - align: top; width: 20px; '> [x]</td><td> Test Option Added 9/23/19. Edit 10/28/19 </td></tr><tr><td style='vertical - align: top; width: 20px; '> [x]</td><td> Test Add 12/17/19. Test Edit 2/13/20 </td></tr><tr><td style='vertical - align: top; width: 20px; '> [x]</td><td> Test Addition Sequence 14 12/8/20 </td></tr><tr><td style='vertical - align: top; width: 20px; '> [x]</td><td> Test Add 2/13/20 active. </td></tr><tr><td style='vertical - align: top; width: 20px; '> [x]</td><td> Test Edit Active, Sequence 16&nbsp;; 12/8/20 </td></tr><tr><td style='vertical - align: top; width: 20px; '> [  ]</td><td> Test edit uncheck&nbsp;12/8/20 Sequence 17 </td></tr><tr><td style='vertical - align: top; width: 20px; '> [x]</td><td> Test Addition 10/5/21 Active/Checked- Seq 18 </td></tr><tr><td style='vertical - align: top; width: 20px; '> [x]</td><td> Test Edit, Sequence 23. Active and Unchecked </td></tr><tr><td style='vertical - align: top; width: 20px; '> [x]</td><td> Denial Checkbox added 10/12/2022. Test Edit 3/24/23 Checked </td></tr><tr><td style='vertical - align: top; width: 20px; '> [  ]</td><td> Test Add 3/24/23&nbsp; Active Unchecked </td></tr><tr><td style='vertical - align: top; width: 20px; '> [x]</td><td> Test Addition Active/Checked 1/25/23 </td></tr><tr><td style='vertical - align: top; width: 20px; '> [  ]</td><td> Test Add Active/Unchecked 6/30/23 </td></tr></table>";
Document mainDoc = new Document("C:\\Temp\\in.docx");
string key = "LetterCheckbox";
DocumentBuilder builder = new DocumentBuilder(mainDoc);
builder.MoveToMergeField(key);

Paragraph currentParagraph = builder.CurrentParagraph;
builder.InsertHtml(mailMergeIssue, HtmlInsertOptions.RemoveLastEmptyParagraph);

// Remove empty paragraph where HTML content was inserted.
if (!currentParagraph.HasChildNodes)
    currentParagraph.Remove();
// Remove DocumentBuilder curent paragrpah is it is empty.
if (!builder.CurrentParagraph.HasChildNodes)
    builder.CurrentParagraph.Remove();

mainDoc.Save("C:\\Temp\\out.docx");

This occurs because the current position of document builder cursor is in the paragraph, table cannot be inserted into a paragraph so it is split into two paragraphs and the table is inserted between them.

1 Like