Extra Line at the end of Bulleted List C#


#1

Team,
I defined Tags and replacing tags with bulleted/numbered list programatically. I achieved the bulleted lists using ReplaceEvaluator. But i can see extra line added at the end of last bulleted list. Could you please suggest how to write bulleted/numbered lists without adding extra line. Your answer would be much appreciated.

Thanks,
Felix


#2

@fxprince

After inserting the list, please call ListFormat.RemoveNumbers method to remove numbers or bullets from the current paragraph and sets list level to zero. Hope this helps you.


#3

Hi,
Thanks for your answer. As informed earlier, bulleted list getting printed as expected. But i could still see empty line at the end, I tried below code snippet in ReplaceAction:

DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);
builder.MoveTo((Run)runs[runs.Count - 1]);
if (isBulletedList) builder.ListFormat.List = e.MatchNode.Document.Lists.Add(ListTemplate.BulletDefault);
if (isNumberedList) builder.ListFormat.List = e.MatchNode.Document.Lists.Add(ListTemplate.NumberDefault);
foreach (string item in listItems)
{
builder.Writeln(item);
}

            builder.ListFormat.ListLevel.TabPosition = 17;
            builder.ListFormat.ListLevel.TextPosition = 17;
            builder.ListFormat.ListLevel.NumberPosition = 0;
            builder.ListFormat.RemoveNumbers();

            foreach (Run run in runs)
                run.Remove();

            return ReplaceAction.Skip;

Note: I have set different values for TabPosition and TextPosition, which i required.


#4

@fxprince

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.


#5

Hi Team,
I have attached the required files AsposeSupport.zip (31.1 KB)
for your attention. Thank you for your assistance.

Thanks,
Felix.


#6

@fxprince

Please use the following modified code of ReplaceEvaluatorFindAndInsertList class to get the desired output.

public class ReplaceEvaluatorFindAndInsertList : IReplacingCallback
{
    private List<string> listItems;
    private bool isBulletedList, isNumberedList = false;

    public ReplaceEvaluatorFindAndInsertList(List<string> list, bool IsNumberedList, bool IsBulletedList)
    {
        listItems = list;
        isBulletedList = IsBulletedList;
        isNumberedList = IsNumberedList;
    }

    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        Node currentNode = e.MatchNode;
        if (e.MatchOffset > 0)
            currentNode = SplitRun((Run)currentNode, e.MatchOffset);

        ArrayList runs = new ArrayList();

        int remainingLength = e.Match.Value.Length;
        while (
            (remainingLength > 0) &&
            (currentNode != null) &&
            (currentNode.GetText().Length <= remainingLength))
        {
            runs.Add(currentNode);
            remainingLength = remainingLength - currentNode.GetText().Length;

            do
            {
                currentNode = currentNode.NextSibling;
            }
            while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));
        }

        if ((currentNode != null) && (remainingLength > 0))
        {
            SplitRun((Run)currentNode, remainingLength);
            runs.Add(currentNode);
        }

        DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);
        builder.MoveTo((Run)runs[0]);
        if (isBulletedList) builder.ListFormat.List = e.MatchNode.Document.Lists.Add(ListTemplate.BulletDefault);
        if (isNumberedList) builder.ListFormat.List = e.MatchNode.Document.Lists.Add(ListTemplate.NumberDefault);

        foreach (string item in listItems)
        {
            builder.Writeln(item);
        }

        builder.ListFormat.ListLevel.TabPosition = 17;
        builder.ListFormat.ListLevel.TextPosition = 17;
        builder.ListFormat.ListLevel.NumberPosition = 0;
        builder.ListFormat.RemoveNumbers();

        Paragraph paragraph = null;
        Boolean blnFirst = true;
        foreach (Run run in runs)
        {
            if (blnFirst)
            {
                blnFirst = false;
                paragraph = run.ParentParagraph;
            }
            run.Remove();
        }

        if (paragraph != null)
            paragraph.Remove();

        return ReplaceAction.Skip;
    }

    private static Run SplitRun(Run run, int position)
    {
        Run afterRun = (Run)run.Clone(true);
        afterRun.Text = run.Text.Substring(position);
        run.Text = run.Text.Substring(0, position);
        run.ParentNode.InsertAfter(afterRun, run);
        return afterRun;
    }

}

#7

Much Thanks Team for your working solution, which reduced my trail and error attempts. Your help is much appreciated. Thank you!


#8

@fxprince

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.