Theme Graphic
Theme Graphic

Excel, Word, PDF components for .NET

Elerium Software developes professional components for use in .NET projects (C#, VB.NET, ASP.NET) that aimed to read/write/convert PDF, DOC...

Subscribe

Author

Archive

Tags

Posted on Tuesday, April 02, 2013 at 10:34 PM

How to read data and formatting of Word documents in .NET


MS Word documents are one of the most popular formats for the reporting. It allows presenting information with different styles and formatting exactly such as it should look on a paper. Often MS Word is not installed on the server/computer, nevertheless a developer wants to process these reports inside C#/VB.NET/ASP.NET project. The best way is using a professional .NET library that includes various Word API functions. One of these libraries is introduced by Elerium Software.

Elerium Word .NET Reader presents an easy way to read data and formatting of Word documents. Here are the basic steps of getting the text of the document.

First off all, a developer must install Elerium Word .NET Reader to the project:

1. Download the latest version of the component from this link:
http://www.eleriumsoft.com/Word_NET/WordReader/Default.aspx
2. Extract the downloaded archive and put the Word.dll component into /bin folder of the project.
3. Add the component to the “using” section:
using Docs.Word;


After that developer can easily read data from the Word document.

C# example:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using Docs.Word;  
namespace OpenDocument  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            // Creates an instance of Document class  
            Document Doc = new Document();  
            // Reads a .doc file into internal document structure  
            Doc.ReadDoc(@"..\..\Data\DocFile.doc");  
            // Gets text of 1st paragraph of 1st section of the document  
            string Text = ((Paragraph)Doc.Sections[0].Nodes[0]).Text;  
            // Writes gotten text to console  
            Console.WriteLine(Text);  
            Console.ReadKey();  
        }  }  }
VB.NET Example:

Imports Docs.Word  
Module Module1  
    Sub Main()  
        ' Creates an instance of Document class  
        Dim Doc As New Document()  
        ' Reads a .doc file into internal document structure  
        Doc.ReadDoc("..\..\Data\DocFile.doc")  
        ' Gets text of 1st paragraph of 1st section of the document  
        Dim Text As String = DirectCast(Doc.Sections(0).Nodes(0), Paragraph).Text  
        ' Writes gotten text to console  
        Console.WriteLine(Text)  
        Console.ReadKey()  
    End Sub  
End Module


This sample demonstrates the reading of different text formatting such as Font Name, Size, Color, Background color, Footnotes etc.

C# Example:

using System;  
using System.Collections.Generic;  
using System.Text;  
using System.Windows.Forms;  
using Docs.Word;  
namespace TextRun_Styles  
{  
        private void Form1_Load(object sender, EventArgs e)  
        {  
            // Creates a new instance of Document class and reads a .doc file into this structure  
            Document Doc = new Document();  
            Doc.ReadDoc(@"..\..\Data\WordTextFormatting.doc");  
            // Gets two first text runs, in this example - two sentences  
            for (int i = 0; i < 2; i++)  
            {  
                // Gets text run  
                TextRun tTextRun = ((Paragraph)Doc.Sections[0].Nodes[0]).TextRuns[i];  
                // Writes its properties  
                textBox1.Text += "=== Text run " + (i+1) + " ===" + "\r\n";  
                textBox1.Text += "Text          : " + tTextRun.Text + "\r\n";  
                textBox1.Text += "Font name     : " + tTextRun.Style.FontName + "\r\n";  
                textBox1.Text += "Font size (in half-point) : " + tTextRun.Style.FontSize + "\r\n";  
                textBox1.Text += "Text color            : " + tTextRun.Style.TextColor + "\r\n";  
                textBox1.Text += "Bold          : " + tTextRun.Style.FontStyle.Bold + "\r\n";  
                textBox1.Text += "Italic            : " + tTextRun.Style.FontStyle.Italic + "\r\n";  
                textBox1.Text += "Underlined        : " + tTextRun.Style.FontStyle.Underlined + "\r\n";  
                textBox1.Text += "Strike-out            : " + tTextRun.Style.FontStyle.StrikeOut + "\r\n\r\n";  
            }      }     }     }
VB.NET Example:

Imports Docs.Word  
Public Class Form1  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load  
        ' Creates a new instance of Document class and reads a .doc file into this structure  
        Dim Doc As New Document()  
        Doc.ReadDoc("..\..\Data\WordTextFormatting.doc")  
        ' Gets two first text runs, in this example - two sentences  
        For i As Integer = 0 To 1  
            ' Gets text run  
            Dim tTextRun As TextRun = DirectCast(Doc.Sections(0).Nodes(0), Paragraph).TextRuns(i)  
            ' Writes its properties  
            textBox1.Text += "=== Text run " & (i + 1).ToString & " ===" & vbCr & vbLf  
            textBox1.Text += "Text" & vbTab & vbTab & vbTab & ": " + tTextRun.Text & vbCr & vbLf  
            textBox1.Text += "Font name" & vbTab & vbTab & ": " + tTextRun.Style.FontName & vbCr & vbLf  
            textBox1.Text += "Font size" & vbTab & "(in half-point)" & vbTab & ": " + tTextRun.Style.FontSize.ToString & vbCr & vbLf  
            textBox1.Text += "Text color" & vbTab & vbTab & vbTab & ": " + tTextRun.Style.TextColor.ToString & vbCr & vbLf  
            textBox1.Text += "Bold" & vbTab & vbTab & vbTab & ": " + tTextRun.Style.FontStyle.Bold.ToString & vbCr & vbLf  
            textBox1.Text += "Italic" & vbTab & vbTab & vbTab & ": " + tTextRun.Style.FontStyle.Italic.ToString & vbCr & vbLf  
            textBox1.Text += "Underlined" & vbTab & vbTab & ": " + tTextRun.Style.FontStyle.Underlined.ToString & vbCr & vbLf  
            textBox1.Text += "Strike-out" & vbTab & vbTab & vbTab & ": " + tTextRun.Style.FontStyle.StrikeOut.ToString & vbCr & vbLf & vbCr & vbLf  
        Next  
    End Sub  
End Class


About Elerium Software

Elerium Software develops professional solutions for use in .NET projects (C#, VB.NET, ASP.NET) that aimed to read/write/convert different office/web documents and formats. Elerium Software components are based on the unique design and fast algorithms that allow being independent from the third-party applications and libraries.

For more information about the component please visit the product page:
http://www.eleriumsoft.com/Word_NET/WordReader/Default.aspx

If you have any questions or concerns about component, let us know:
http://eleriumsoft.com/Company/Contact.aspx
Bookmark: Submit To Digg Submit To reddit Submit To del.icio.us Bookmark With StumbleUpon Bookmark With FaceBook Bookmark With Google Bookmarks   Share: Share By Email By Email

0 comments on "How to read data and formatting of Word documents in .NET"
No comments posted yet.

Leave A Comment
Subject:


Comment:
   Bold Italic Underline          Code Link Image Horizontal Rule


Because you do not have or are not logged in to your Programmer's Heaven account, please enter your name.

Name:


To help prevent comment SPAM, please enter the magic code '584' in the box:




Posting Rules
Please follow these rules when posting comments on blog posts.
  • Do not post anything that is racist, hate speech or of a sexual or adult nature.
  • Do not post or link to anything that infringes copyrighted laws.
  • Posting about security or legal topics is fine so long as you are not glorifying or encouraging people to perform illegal activities.
  • Both the author of this blog and the Programmer's Heaven administrators may delete any inappropriate comments without notice at their own discretion.
 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.