Following example can be found and executed in GemBox.Document Sample Explorer under category 'Basic Features' and name 'Reading'.
Example
GemBox.Document supports reading document files from a file path or a stream. Document file format is specified through LoadOptions derived classes.
This sample demonstrates how to read a document from a file path, iterate over all paragraphs and runs and output text for all runs with font weight information to console output.
using System; using System.Linq; using System.Text; using GemBox.Document; using GemBox.Document.Tables; class Sample { [STAThread] static void Main(string[] args) { // If using Professional version, put your serial key below. ComponentInfo.SetLicense("FREE-LIMITED-KEY"); // If sample exceeds Free version limitations then continue as trial version. ComponentInfo.FreeLimitReached += (sender, e) => e.ContinueAsTrial = true; string fileName = @"C:\Users\User\Documents\SVN\Document\v1.0\DocumentCode\SampleExplorer\GBDSampleExplorer\Data\Reading.docx"; DocumentModel document = DocumentModel.Load(fileName, DocxLoadOptions.DocxDefault); StringBuilder sb = new StringBuilder(); foreach (Paragraph paragraph in document.GetChildElements(true, ElementType.Paragraph)) { foreach (Run run in paragraph.GetChildElements(true, ElementType.Run)) { bool isBold = run.CharacterFormat.Bold; string text = run.Text; sb.AppendFormat("{0}{1}{2}", isBold ? "<b>" : "", text, isBold ? "</b>" : ""); } sb.AppendLine(); } Console.WriteLine(sb.ToString()); } }
Imports System Imports System.Linq Imports System.Text Imports GemBox.Document Imports GemBox.Document.Tables Module Samples Sub Main() ' If using Professional version, put your serial key below. ComponentInfo.SetLicense("FREE-LIMITED-KEY") ' If sample exceeds Free version limitations then continue as trial version. AddHandler ComponentInfo.FreeLimitReached, Sub(sender, e) e.ContinueAsTrial = True Dim fileName As String = "C:\Users\User\Documents\SVN\Document\v1.0\DocumentCode\SampleExplorer\GBDSampleExplorer\Data\Reading.docx" Dim document As DocumentModel = DocumentModel.Load(fileName, DocxLoadOptions.DocxDefault) Dim sb As New StringBuilder() For Each paragraph As Paragraph In document.GetChildElements(True, ElementType.Paragraph) For Each run As Run In paragraph.GetChildElements(True, ElementType.Run) Dim isBold As Boolean = run.CharacterFormat.Bold Dim text As String = run.Text sb.AppendFormat("{0}{1}{2}", If(isBold, "<b>", ""), text, If(isBold, "</b>", "")) Next sb.AppendLine() Next Console.WriteLine(sb.ToString()) End Sub End Module