We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

replace bookmark in paragraph

hi,
i have a document with one paragraph contains two bookmarks, the first is normal and the second is bold caractere
i want to add contains in each bookmark and keep the paragraph at the same order.

exemple:
[] paragraph []
result:
first line
paragraph
third line

i give in attach the main document and the rresult that i want.

how can i do ?

Attachment: sample_135c5d40.zip

5 Replies

VA Vijayasurya Anandhan Syncfusion Team June 14, 2019 12:54 PM UTC

Hi AtliDev,

Thank you for contacting Syncfusion support.

To meet your requirement, we suggest you use the following code snippets to insert the paragraphs to the bookmarks in the Word document using DocIO .
 
//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 '' 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 ''.  
The contents following '' will be in a new line.  */ 
document.LastParagraph.AppendText("ceci est un paragraph ");

 
/*  Alternate for this '' adding is, you can add second bookmark into a separate paragraph. 
Remove '' 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(); 

Please refer the following UG documentation to work with Bookmarks.
https://help.syncfusion.com/file-formats/docio/working-with-bookmarks

Please let us know if you have any questions.

Regards,
Vijayasurya A
 



AT AtliDev June 14, 2019 01:10 PM UTC

hi,
your code is working thanks,
but the problem that u create a other paragraph,
if my bookmark have a specific charactere format i will lose it.

exemple:
[] paragraph []
what i want :
debut
paragraph
fin
what i have with your code:
debut
paragraph
fin


the first bookmark can have a lot format charactere !!

thanks.


AT AtliDev June 14, 2019 03:45 PM UTC

hi ,
can we have the specific character format of a bookmark content,
     
       For Each element As IWTextRange In bknav.currentbookmarkitem.ownerparagraph.ChildEntities
            dim cf = element.CharacterFormat
       Next

give me the character of all paragraph and not the content of bookmark.

thanks



MJ Mohanaselvam Jothi Syncfusion Team June 17, 2019 11:35 AM UTC

Hi AtliDev,

Thank you for your update.

A bookmark identifies a location or a selection of text within a document that you can name and identify for future reference. In the Word document, bookmark is a reference and doesn’t contain the character formats.

We suspect that your end requirement is to replace the bookmark content and need to preserve same character formats (Bold) after replacing. To achieve this at your end, we suggest to set bookmark for text range and apply necessary formats for that text. Then, get the character format of text and replace the bookmark content by applying same formatting. We have prepared a sample application for the same and it can be downloaded from the below link:
http://www.syncfusion.com/downloads/support/forum/145262/ze/GenerateWord-84753757

Note: Please find the input document in “Data” folder of above sample application.

In this sample, we have done the following things:
1. Loads an existing Word document with bookmark.
2. Get character format of bookmark content.
3. Create new body part and apply character format.
4. Replace the bookmark content using
BookmarksNavigator API.
5. Saves the Word document.


Please let us know if you have any other questions.

Regards,
Mohanaselvam J
 



AT AtliDev June 18, 2019 08:09 AM UTC

thanks , it's working ☺☻

Loader.
Live Chat Icon For mobile
Up arrow icon