Find placeholder keyword in Word Document, replace with bulleted list

Good morning! 

Having some trouble trying to figure out how to do this. My scenario is - I am taking in a word document. Within that worddocument is a placeholder keyword. I want to replace this keyword with a bulleted list. I've gotten close, but not quite. It puts everything in on its own line - but each line doesn't have a bullet. For example:
 
- LINE 1
  LINE 2
  LINE 3
  LINE 4


versus the desired output

- LINE 1
- LINE 2
- LINE 3
- LINE 4

I've been trying to follow examples. But most seem to be creating things and appending them, so I'm wondering what I might be doing wrong. Any suggestions would be greatly appreaciated! :)


TextSelection[] selections = sourceDocument.FondAll(placeholderKey, false, false);

foreach (TextSelection selection in selections)
{
IWParagraph paragraph = selection.GetAsOneRange().OwnerParagraph;

...

paragraph.ListFormat.ApplyDefBulletStyle();

foreach (string item in valueList)
{
paragrapgh.ListFOrmat.ApplyDefBulletStyle();

paragraph.AppendText(item);

  • paragraph.AppendBreak(BreakType.LineBreak);

paragraph.ListFormat.ContinueListNumbering();
}
sourceDocument.Replace(placeholderKey, ....)

}


2 Replies 1 reply marked as answer

AN Anto Nihil Sahaya Raj Syncfusion Team April 9, 2024 08:45 AM UTC

Hi Sydney,

From the given code snippet, we found that a line break is appended to add multiple values to multiple lines, which will not be considered as multiple paragraphs. However, according to the Microsoft Word application, list formats are applied to paragraphs and not multiple lines. Thus, the bullet list is applied to the first line, and the remaining lines do not have the bullet list. To apply the list format to multiple values, you need to append each value to different paragraphs and apply the list format to all the paragraphs. This is not an issue but a behavior in the Microsoft Word application. DocIO also behaves the same way.

To fulfill this requirement, we have prepared a sample that follows the steps below:

1. Open Word document.
2. Use the Find API to find the text in the Word document.
3. Clone the owner paragraph of the finding text.
4. Then, the text in the cloned paragraph was replaced with values from the list.
5. Apply the bulleted list to the paragraph and add each paragraph to the TextBodyPart.
6. Replace text with the TextBodyPart in the Word document.
7. Save the Word document.


You can find the sample in the below.

Find-text-and-replace-with-multiple-list-paragraphs

Regards,
Anto Nihil S


Marked as answer

SB Sydney Brea April 10, 2024 03:03 PM UTC

Exactly what I'm looking for and appreciate you showing how that works. :) Had a system where it was about 5 times more code to even get close. Appreciated! <3


Loader.
Up arrow icon