Spire.Doc for .NET (332)
Children categories
Spire.Doc supports to retrieve, replace and delete bookmark content of a specified bookmark. This article will show you how we can get the plain text within a bookmark by using Spire.Doc with C# and VB.NET.
Step 1: Create a Document instance, and load a sample Word document.
Document doc = new Document(); doc.LoadFromFile("Bookmark.docx");
Step 2: Creates a BookmarkNavigator instance to access the bookmark.
BookmarksNavigator navigator = new BookmarksNavigator(doc);
Step 3: Locate a specific bookmark by bookmark name. Call the method GetBookmarkContent to get content within the bookmark.
navigator.MoveToBookmark("bookmark_1"); TextBodyPart textBodyPart = navigator.GetBookmarkContent();
Step 4: Iterate through the items in the bookmark content to get the plain, unformatted text of the bookmark.
string text = null; foreach (var item in textBodyPart.BodyItems) { if (item is Paragraph) { foreach (var childObject in (item as Paragraph).ChildObjects) { if (childObject is TextRange) { text += (childObject as TextRange).Text; } } } }
Result:
Full Code:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System; namespace GetText { class Program { static void Main(string[] args) { Document doc = new Document(); doc.LoadFromFile("Bookmark.docx"); BookmarksNavigator navigator = new BookmarksNavigator(doc); navigator.MoveToBookmark("bookmark_1"); TextBodyPart textBodyPart = navigator.GetBookmarkContent(); string text = null; foreach (var item in textBodyPart.BodyItems) { if (item is Paragraph) { foreach (var childObject in (item as Paragraph).ChildObjects) { if (childObject is TextRange) { text += (childObject as TextRange).Text; } } } } Console.WriteLine(text); } } }
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace GetText Class Program Private Shared Sub Main(args As String()) Dim doc As New Document() doc.LoadFromFile("Bookmark.docx") Dim navigator As New BookmarksNavigator(doc) navigator.MoveToBookmark("bookmark_1") Dim textBodyPart As TextBodyPart = navigator.GetBookmarkContent() Dim text As String = Nothing For Each item As var In textBodyPart.BodyItems If TypeOf item Is Paragraph Then For Each childObject As var In TryCast(item, Paragraph).ChildObjects If TypeOf childObject Is TextRange Then text += TryCast(childObject, TextRange).Text End If Next End If Next Console.WriteLine(text) End Sub End Class End Namespace
Retrieve Style Names of all TextRanges in a Word Document in C#, VB.NET
2017-12-26 02:59:14 Written by support iceblueProgrammers may need to determine the style name of a section of text, or find the text in a document that appear in a specified style name, such as “Heading 1”. This article will show you how to retrieve style names that are applied in a Word document by using Spire.Doc with C# and VB.NET.
Step 1: Create a Document instance.
Document doc = new Document();
Step 2: Load a sample Word file.
doc.LoadFromFile("Sample.docx");
Step 3: Traverse all TextRanges in the document and get their style names through StyleName property.
foreach (Section section in doc.Sections) { foreach (Paragraph paragraph in section.Paragraphs) { foreach (DocumentObject docObject in paragraph.ChildObjects) { if (docObject.DocumentObjectType == DocumentObjectType.TextRange) { TextRange text = docObject as TextRange; Console.WriteLine(text.StyleName); } } } }
Result:
Full Code:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System; using System.Text.RegularExpressions; namespace RetrieveStyleNames { class Program { static void Main(string[] args) { Document doc = new Document(); doc.LoadFromFile("Sample.docx"); foreach (Section section in doc.Sections) { foreach (Paragraph paragraph in section.Paragraphs) { foreach (DocumentObject docObject in paragraph.ChildObjects) { if (docObject.DocumentObjectType == DocumentObjectType.TextRange) { TextRange text = docObject as TextRange; Console.WriteLine(text.StyleName); } } Console.WriteLine(); } } } } }
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports System.Text.RegularExpressions Namespace RetrieveStyleNames Class Program Private Shared Sub Main(args As String()) Dim doc As New Document() doc.LoadFromFile("Sample.docx") For Each section As Section In doc.Sections For Each paragraph As Paragraph In section.Paragraphs For Each docObject As DocumentObject In paragraph.ChildObjects If docObject.DocumentObjectType = DocumentObjectType.TextRange Then Dim text As TextRange = TryCast(docObject, TextRange) Console.WriteLine(text.StyleName) End If Next Console.WriteLine() Next Next End Sub End Class End Namespace
Create a Cross-Reference to Bookmark in Word in C#, VB.NET
2017-12-21 07:54:20 Written by support iceblueA cross-reference refers to related information elsewhere in the same document. You can create cross-references to any existing items such as headings, footnotes, bookmarks, captions, and numbered paragraphs. This article will show you how to create a cross-reference to bookmark using Spire.Doc with C# and VB.NET.
Step 1: Create a Document instance.
Document doc = new Document(); Section section = doc.AddSection();
Step 2: Insert a bookmark.
Paragraph paragraph = section.AddParagraph(); paragraph.AppendBookmarkStart("MyBookmark"); paragraph.AppendText("Text inside a bookmark"); paragraph.AppendBookmarkEnd("MyBookmark");
Step 3: Create a cross-reference field, and link it to the bookmark through bookmark name.
Field field = new Field(doc); field.Type = FieldType.FieldRef; field.Code = @"REF MyBookmark \p \h";
Step 4: Add a paragraph, and insert the field to the paragraph.
paragraph = section.AddParagraph(); paragraph.AppendText("For more information, see "); paragraph.ChildObjects.Add(field);
Step 5: Insert a FieldSeparator object to the paragraph, which works as separator in a field.
FieldMark fieldSeparator= new FieldMark(doc, FieldMarkType.FieldSeparator); paragraph.ChildObjects.Add(fieldSeparator);
Step 6: Set the display text of the cross-reference field.
TextRange tr = new TextRange(doc); tr.Text = "above"; paragraph.ChildObjects.Add(tr);
Step 7: Insert a FieldEnd object to the paragraph, which is used to mark the end of a field.
FieldMark fieldEnd = new FieldMark(doc, FieldMarkType.FieldEnd); paragraph.ChildObjects.Add(fieldEnd);
Step 8: Save to file.
doc.SaveToFile("output.docx", FileFormat.Docx2013);
Output:
The cross-reference appears as a link that takes the reader to the referenced item.
Full Code:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace CreatCR { class Program { static void Main(string[] args) { Document doc = new Document(); Section section = doc.AddSection(); //create a bookmark Paragraph paragraph = section.AddParagraph(); paragraph.AppendBookmarkStart("MyBookmark"); paragraph.AppendText("Text inside a bookmark"); paragraph.AppendBookmarkEnd("MyBookmark"); //insert line breaks for (int i = 0; i < 4; i++) { paragraph.AppendBreak(BreakType.LineBreak); } //create a cross-reference field, and link it to bookmark Field field = new Field(doc); field.Type = FieldType.FieldRef; field.Code = @"REF MyBookmark \p \h"; //insert field to paragraph paragraph = section.AddParagraph(); paragraph.AppendText("For more information, see "); paragraph.ChildObjects.Add(field); //insert FieldSeparator object FieldMark fieldSeparator = new FieldMark(doc, FieldMarkType.FieldSeparator); paragraph.ChildObjects.Add(fieldSeparator); //set display text of the field TextRange tr = new TextRange(doc); tr.Text = "above"; paragraph.ChildObjects.Add(tr); //insert FieldEnd object to mark the end of the field FieldMark fieldEnd = new FieldMark(doc, FieldMarkType.FieldEnd); paragraph.ChildObjects.Add(fieldEnd); //save file doc.SaveToFile("output.docx", FileFormat.Docx2013); } } }
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace CreatCR Class Program Private Shared Sub Main(args As String()) Dim doc As New Document() Dim section As Section = doc.AddSection() 'create a bookmark Dim paragraph As Paragraph = section.AddParagraph() paragraph.AppendBookmarkStart("MyBookmark") paragraph.AppendText("Text inside a bookmark") paragraph.AppendBookmarkEnd("MyBookmark") 'insert line breaks For i As Integer = 0 To 3 paragraph.AppendBreak(BreakType.LineBreak) Next 'create a cross-reference field, and link it to bookmark Dim field As New Field(doc) field.Type = FieldType.FieldRef field.Code = "REF MyBookmark \p \h" 'insert field to paragraph paragraph = section.AddParagraph() paragraph.AppendText("For more information, see ") paragraph.ChildObjects.Add(field) 'insert FieldSeparator object Dim fieldSeparator As New FieldMark(doc, FieldMarkType.FieldSeparator) paragraph.ChildObjects.Add(fieldSeparator) 'set display text of the field Dim tr As New TextRange(doc) tr.Text = "above" paragraph.ChildObjects.Add(tr) 'insert FieldEnd object to mark the end of the field Dim fieldEnd As New FieldMark(doc, FieldMarkType.FieldEnd) paragraph.ChildObjects.Add(fieldEnd) 'save file doc.SaveToFile("output.docx", FileFormat.Docx2013) End Sub End Class End Namespace