Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
Word .NET
>
Tutorial
> Insert bookmark text
The example shows you how to update bookmark's content.
C# example
using System; using System.Collections.Generic; using Independentsoft.Office; using Independentsoft.Office.Word; namespace Sample { class Program { static void Main(string[] args) { WordDocument doc = new WordDocument("c:\\test\\input.docx"); for (int i = 0; i < doc.Body.Content.Count; i++) { if (doc.Body.Content[i] is Paragraph) { Paragraph paragraph = (Paragraph)doc.Body.Content[i]; for (int j=0; j < paragraph.Content.Count; j++) { if (paragraph.Content[j] is BookmarkStart) { BookmarkStart bookmarkStart = (BookmarkStart)paragraph.Content[j]; if (bookmarkStart.Name == "mybookmark") { Run run1 = new Run(); run1.AddText("some text"); run1.Bold = ExtendedBoolean.True; paragraph.Content.Insert(j,run1); break; } } } } } doc.Save("c:\\test\\output.docx", true); } } }
VB example
Imports System Imports Independentsoft.Office Imports Independentsoft.Office.Word Imports Independentsoft.Office.Word.Tables Module Module1 Sub Main(ByVal args() As String) Dim doc As New WordDocument("c:\test\input.docx") For i As Integer = 0 To doc.Body.Content.Count - 1 If TypeOf doc.Body.Content(i) Is Paragraph Then Dim paragraph As Paragraph = DirectCast(doc.Body.Content(i), Paragraph) For j As Integer = 0 To paragraph.Content.Count - 1 If TypeOf paragraph.Content(j) Is BookmarkStart Then Dim bookmarkStart As BookmarkStart = DirectCast(paragraph.Content(j), BookmarkStart) If bookmarkStart.Name = "mybookmark" Then Dim run1 As New Run() run1.AddText("some text") run1.Bold = ExtendedBoolean.[True] paragraph.Content.Insert(j, run1) Exit For End If End If Next End If Next doc.Save("c:\test\output.docx", True) End Sub End Module
Need help? Ask our developers:
Name*
Email*
Message*