How to set the bookmark font?

Hello:
I want to set the bookmark text Font style.For example,I have a word documnet, there is a bookmark in this.the font is fangsong before of the bookmark.When I replace the bookmark text ,the replaced text font may be not fangsong or may be not the right size .
1.How can I set the replace text same font as before it?
2.How can I set a regular font of the replaced text?
Like below:

Please,give me a code example.Thank you

@JohnKj Font applied to the text when Bookmakr.Text property is used is the same as font applied to the old text inside bookmark. If you need to set custom font, you can use DocumentBuilder to achieve this. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToBookmark("bk1", true, true);
builder.Font.Bold = true;
builder.Write("bold bookmark text");

builder.MoveToBookmark("bk2", true, true);
builder.Font.Italic = true;
builder.Write("Italic bookmark text");

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

There is some word in a bookmark,use this way,There is appending the bookmark text,not replace the bookmark text.

@JohnKj You can modify code like this then:

doc.Range.Bookmarks["bk1"].Text = "";
builder.MoveToBookmark("bk1", true, true);
builder.Font.Bold = true;
builder.Write("bold bookmark text");

Thank for your help

1 Like