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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

replace bookmark in paragraph

Thread ID:

Created:

Updated:

Platform:

Replies:

145262 Jun 14,2019 07:41 AM UTC Jun 18,2019 08:09 AM UTC WinForms 5
loading
Tags: DocIO
AtliDev
Asked On June 14, 2019 07:41 AM UTC

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

Vijayasurya Anandhan [Syncfusion]
Replied On 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 '\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(); 

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
 


AtliDev
Replied On 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.

AtliDev
Replied On 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


Mohanaselvam  Jothi [Syncfusion]
Replied On 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
 


AtliDev
Replied On June 18, 2019 08:09 AM UTC

thanks , it's working ☺☻

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon