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

insert element in bookmark and keep the same format of the current paragraph

hi
i want to add paragraph in the bookmark and keeep the same format and caractere style of the current paragraph
like
     this is a paragraph [other paragraph of element] suite of the paragraph
this is wrong what i want is:
     this is a paragraph [other paragraph of element] suite of the paragraph

my code :

        Dim a As WordDocument = New WordDocument(chemin + "\testword\mehdi.doc", FormatType.Doc)
        Dim bknav As BookmarksNavigator = New BookmarksNavigator(a)
        bknav.MoveToBookmark("casa")



        Dim d As WordDocument = New WordDocument
        Dim tbp As TextBodyPart = New TextBodyPart(d)
        Dim para As WParagraph = d.CreateParagraph

        para.Text = "paragraph of element"

        tbp.BodyItems.Add(para)
        bknav.ReplaceBookmarkContent(tbp)
        d.Close()
        tbp.Close()




thanks

3 Replies

AV Abirami Varadharajan Syncfusion Team April 12, 2019 11:59 AM UTC

Hi AtliDev, 
 
To meet your requirement, we suggest you to set the formats and then replace the bookmark content in the Word document. Please refer the below highlighted code example to apply character and paragraph format at your end.
  
Dim a As WordDocument = New WordDocument("Sample.docx", FormatType.Doc)  
Dim bknav As BookmarksNavigator = New BookmarksNavigator(a)  
bknav.MoveToBookmark("casa")  
Dim d As WordDocument = New WordDocument  
Dim tbp As TextBodyPart = New TextBodyPart(d)  
Dim para As WParagraph = d.CreateParagraph  
  
'Append text into the paragraph.  
Dim textRange As IWTextRange = para.AppendText("paragraph of element")  
'Set character format for text.  
textRange.CharacterFormat.Bold = True  
'Apply paragraph format for the paragraph.  
para.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center  
  
tbp.BodyItems.Add(para)  
  
bknav.ReplaceBookmarkContent(tbp)  
d.Close()  
tbp.Close()  
  
a.Save("Result.docx")  

Please refer the below UG documentation link to know more about applying paragraph and character format in the Word document:
https://help.syncfusion.com/file-formats/docio/working-with-paragraph
 
 
Regards, 
Abirami 



AT AtliDev April 12, 2019 12:17 PM UTC

hi,
thanks for reply,
same time the bookmark have a specific charterformat different of the format of paragraph.
and when i want to insert element in bookmark , i want just to save the format of the bookmark

thanks



AV Abirami Varadharajan Syncfusion Team April 15, 2019 10:09 AM UTC

Hi AtliDev, 

We suspect that your requirement is to preserve the character formats of bookmark contents to the replacement contents. In the Word document, bookmarks are references and doesn’t contain character formats. To meet your requirement, we suggest you to get the character format from bookmark content and set the same character format to replacement contents. Please refer the sample for the same from the below link:

 
Sample Link:http://www.syncfusion.com/downloads/support/forum/143954/ze/Sample_application-1965261608 

In this sample, we have done the following things:
 1. Loads an existing Word document.
 2. Get the character format from text of bookmark content.
 3. Create new contents and set formats.
 4. Replace the bookmark content in the Word document.
 5. Saves the Word document.

If the above sample doesn’t meet your end requirement means, kindly share us the input Word document and the expected output document. Thereby, we will check on your requirements and will update you the solution at the earliest. 

Regards, 
Abirami 


Loader.
Live Chat Icon For mobile
Up arrow icon