Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
ODF .NET
>
Tutorial
> Set bookmark text
The following example shows you how to update bookmark content.
C# example
using System; using System.Collections.Generic; using Independentsoft.Office.Odf; namespace Sample { class Program { static void Main(string[] args) { TextDocument doc = new TextDocument("c:\\test\\input.odt"); IList
paragraphs = doc.GetParagraphs(); for (int i = 0; i < paragraphs.Count; i++) { foreach (IParagraphContent paragraphElement in paragraphs[i].Content) { if (paragraphElement is Bookmark) { Bookmark bookmark = (Bookmark)paragraphElement; if (bookmark.Name.IndexOf("BookmarkName") > -1) { foreach (IParagraphContent bookmarkElement in bookmark.Content) { if (bookmarkElement is Text) { Text text = (Text)bookmarkElement; text.Value = "New Bookmark text"; } } } } else if (paragraphElement is BookmarkStart) { BookmarkStart bookmarkStart = (BookmarkStart)paragraphElement; foreach (IParagraphContent bookmarkElement in bookmarkStart.Content) { if (bookmarkElement is Text) { Text text = (Text)bookmarkElement; text.Value = "New Bookmark text"; } } } } } doc.Save("c:\\test\\output.odt", true); } } }
VB example
Imports System Imports Independentsoft.Office.Odf Module Module1 Sub Main(ByVal args() As String) Dim doc As New TextDocument("c:\test\input.odt") Dim paragraphs As IList(Of Paragraph) = doc.GetParagraphs() For i As Integer = 0 To paragraphs.Count - 1 For Each paragraphElement As IParagraphContent In paragraphs(i).Content If TypeOf paragraphElement Is Bookmark Then Dim bookmark As Bookmark = DirectCast(paragraphElement, Bookmark) If bookmark.Name.IndexOf("BookmarkName") > -1 Then For Each bookmarkElement As IParagraphContent In bookmark.Content If TypeOf bookmarkElement Is Text Then Dim text As Text = DirectCast(bookmarkElement, Text) text.Value = "New Bookmark text" End If Next End If ElseIf TypeOf paragraphElement Is BookmarkStart Then Dim bookmarkStart As BookmarkStart = DirectCast(paragraphElement, BookmarkStart) For Each bookmarkElement As IParagraphContent In bookmarkStart.Content If TypeOf bookmarkElement Is Text Then Dim text As Text = DirectCast(bookmarkElement, Text) text.Value = "New Bookmark text" End If Next End If Next Next doc.Save("c:\test\output.odt", True) End Sub End Module
Need help? Ask our developers:
Name*
Email*
Message*