Add Line Breaks in text in numbered list

I have C# code that loops through a list of records from a database and inserts them in a
numbered list such as:

  1. My First Line
  2. My Second Line
  3. My Third Line
    etc…

Some of these records have non-printable characters which are ‘\n’, the newline character. If
such a record has text such as:
“I have a cat\n I have a dog \n I have a horse”
When my code creates the list this record should appear in the document as:

  1. I have a cat
    I have a dog
    I have a horse
  2. Next record
  3. Next record
    etc

Instead it appears as:

  1. I have a cat
  2. I have a dog
  3. I have a horse
  4. Next record
  5. Next record
    etc

Right now I have the following code to strip out the ‘\n’:

myString = myString.Replace('\n', ' ');

However that makes the list look like this:

  1. I have a cat I have a dog have a horse
  2. Next record
  3. Next record
    etc

When in Word, I would type a Shift + Enter to get that line break. How can I do that when creating my list ? What do I need to replace the ‘\n’ with and get the proper line break that does not increment the number in the list ?

1 Like

Hi Walter,

Thanks for your inquiry. Please replace the ‘\n’ with ControlChar.LineBreak as shown in following code snippet. Hope this helps you. Please let us know if you have any more queries.

string str = "I have a cat\n I have a dog \nI have a horse";
Document doc = new Document();
// Create a list based on one of the Microsoft Word list templates.
Aspose.Words.Lists.List list = doc.Lists.Add(ListTemplate.NumberDefault);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.ListFormat.List = list;
builder.Writeln(str.Replace("\n", ControlChar.LineBreak));
builder.Writeln("Next record");
builder.Writeln("Next record");
builder.ListFormat.RemoveNumbers();
builder.Document.Save(MyDir + "Out.docx");
1 Like