//Creates an instance of WordDocument class (Empty Word Document)
WordDocument document = new WordDocument();
//Adds a section and a paragraph to the document
document.EnsureMinimal();
//Adds a new bookmark start into paragraph
document.LastParagraph.AppendBookmarkStart("Bookmark1");
//Adds a new bookmark end into paragraph
document.LastParagraph.AppendBookmarkEnd("Bookmark1");
//Adds a text after the bookmark end
/* Reason for adding '\n' at the end of the text
When a new paragraph is inserted to the end of the existing paragraph,
the contens of the new paragraph will be append to the end of the existing paragraph.
Here your requirement is to insert a separate paragraph so we have added '\n'.
The contents following '\n' will be in a new line. */
document.LastParagraph.AppendText("ceci est un paragraph \n");
/* Alternate for this '\n' adding is, you can add second bookmark into a separate paragraph.
Remove '\n' from the text and include the below code.
document.LastSection.AddParagraph(); */
//Adds a new bookmark start into paragraph
document.LastParagraph.AppendBookmarkStart("Bookmark2");
//Adds a new bookmark end into paragraph
document.LastParagraph.AppendBookmarkEnd("Bookmark2");
//Create a new paragraph and appends text into paragraph
WParagraph firstPara = new WParagraph(document);
firstPara.AppendText("Etape1 :");
WParagraph secondPara = new WParagraph(document);
secondPara.AppendText("Voici ma premiere etape.");
//Creates the bookmark navigator instance to access the bookmark
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
//Moves the virtual cursor to the location before the end of the bookmark Bookmark1
bookmarksNavigator.MoveToBookmark("Bookmark1");
//Insert paragraphs to the bookmark Bookmark1
bookmarksNavigator.InsertParagraph(firstPara);
bookmarksNavigator.InsertParagraph(secondPara);
//Create a new paragraph and appends text into paragraph
WParagraph lastPara = new WParagraph(document);
WTextRange text = lastPara.AppendText("Fin") as WTextRange;
//Set Bold formatting for the text
text.CharacterFormat.Bold = true;
//Moves the virtual cursor to the location before the end of the bookmark Bookmark2
bookmarksNavigator.MoveToBookmark("Bookmark2");
//Insert paragraph to the bookmark Bookmark2
bookmarksNavigator.InsertParagraph(lastPara);
//Save the Word document and release the resources hold by Word document instance
document.Save("Bookmark.docx");
document.Close(); |