DEV Community

YaHey
YaHey

Posted on

Read Word Document in C# .NET: Extract Text, Tables, Images

Word documents often serve as rich repositories of information, ranging from simple text notes to complex tables and embedded visuals. However, programmatically accessing and parsing this diverse content from .docx files can be a significant hurdle for developers. Imagine the frustration of needing to extract specific data points from hundreds of reports, each stored as a Word document, without an automated solution. This challenge underscores the critical need for efficient Word Data Extraction techniques.

Fortunately, C# .NET, combined with powerful third-party libraries, offers a robust solution. This article aims to guide you through the process of how to read Word Document content in C #, specifically focusing on extracting text, tables, and images, using the highly capable Spire.Doc for .NET library.


Setting Up Your Project with Spire.Doc for .NET

Spire.Doc for .NET stands out as a comprehensive and user-friendly library for Word document manipulation. Its extensive feature set, including robust support for various Word document formats and complex structures, makes it an ideal choice for programmatic data extraction.

To begin, you need to integrate Spire.Doc for .NET into your C# project. This is most efficiently done via NuGet Package Manager.

  1. Open your C# project in Visual Studio.
  2. Right-click on your project in the Solution Explorer.
  3. Select "Manage NuGet Packages...".
  4. In the "Browse" tab, search for "Spire.Doc".
  5. Select the "Spire.Doc" package by e-iceblue and click "Install".

Once installed, you'll need to include the necessary namespaces in your code files:

using Spire.Doc;
using Spire.Doc.Documents; // For document elements like Paragraph, Table, etc.
using Spire.Doc.Fields;    // For document elements like Pictures, TextBoxes, etc.
using System.IO;            // For file operations
using System.Drawing;       // For image handling
Enter fullscreen mode Exit fullscreen mode

With the library set up, you're ready to dive into extracting data.

Reading a Word Document and Extracting Text

The fundamental step in any C# Read Word Document operation is loading the document itself. Once loaded, extracting all plain text is straightforward. Spire.Doc provides methods to access the document's content, allowing you to iterate through paragraphs or simply retrieve all text at once.

To Extract Text from a Word document, follow these steps:

// Load the Word document
Document document = new Document();
document.LoadFromFile("SampleDocument.docx"); // Replace with your document path

// Extract all text from the document
string allText = document.GetText();
Console.WriteLine("--- Extracted Text ---");
Console.WriteLine(allText);

// Alternatively, iterate through sections and paragraphs for more granular control
Console.WriteLine("\n--- Text by Paragraph ---");
foreach (Section section in document.Sections)
{
    foreach (DocumentObject obj in section.Body.ChildObjects)
    {
        if (obj.DocumentObjectType == DocumentObjectType.Paragraph)
        {
            Paragraph paragraph = obj as Paragraph;
            if (!string.IsNullOrEmpty(paragraph.Text)) // Avoid empty paragraphs
            {
                Console.WriteLine(paragraph.Text);
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This code snippet demonstrates two ways to extract text: document.GetText() for a complete text dump, and iterating through sections and paragraphs for more structured retrieval, which can be useful if you need to process text paragraph by paragraph.

Extracting Data from Tables

Tables in Word documents often hold crucial structured data. Programmatically accessing this data is a common requirement for Word Data Extraction. Spire.Doc simplifies the process of identifying tables and iterating through their rows and cells to retrieve content.

Consider a Word document with a table like this:

Header 1 Header 2 Header 3
Row 1, C1 Row 1, C2 Row 1, C3
Row 2, C1 Row 2, C2 Row 2, C3

Here's how to Extract Tables data:

// Assuming 'document' is already loaded from the previous step
Console.WriteLine("\n--- Extracted Table Data ---");

foreach (Section section in document.Sections)
{
    foreach (DocumentObject obj in section.Body.ChildObjects)
    {
        if (obj.DocumentObjectType == DocumentObjectType.Table)
        {
            Table table = obj as Table;
            Console.WriteLine($"Found a table with {table.Rows.Count} rows and {table.Rows[0].Cells.Count} columns.");

            foreach (TableRow row in table.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    // Extract text from each paragraph within the cell
                    string cellText = "";
                    foreach (Paragraph paragraph in cell.Paragraphs)
                    {
                        cellText += paragraph.Text + " ";
                    }
                    Console.Write($"{cellText.Trim()}\t"); // Use tab for column separation
                }
                Console.WriteLine(); // New line for each row
            }
            Console.WriteLine("-------------------------");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This code iterates through all objects in each section's body. If an object is identified as a Table, it then proceeds to iterate through its rows and cells, extracting the text content from each cell. This provides a clean way to parse structured data.

Programmatically Extracting Images

Embedded images are another common component of Word documents. Whether they are diagrams, logos, or photos, the ability to Extract Images programmatically is often essential for archiving, analysis, or content migration. Spire.Doc allows you to locate these images and save them to a specified directory.

// Assuming 'document' is already loaded
string outputDirectory = "ExtractedImages";
if (!Directory.Exists(outputDirectory))
{
    Directory.CreateDirectory(outputDirectory);
}

Console.WriteLine($"\n--- Extracting Images to: {Path.GetFullPath(outputDirectory)} ---");
int imageCount = 0;

foreach (Section section in document.Sections)
{
    foreach (DocumentObject obj in section.Body.ChildObjects)
    {
        if (obj.DocumentObjectType == DocumentObjectType.Paragraph)
        {
            Paragraph paragraph = obj as Paragraph;
            foreach (DocumentObject paragraphChild in paragraph.ChildObjects)
            {
                if (paragraphChild.DocumentObjectType == DocumentObjectType.Picture)
                {
                    DocPicture picture = paragraphChild as DocPicture;
                    string imageFileName = $"Image_{imageCount++}.{picture.ImageFormat.ToString().ToLower()}";
                    string imagePath = Path.Combine(outputDirectory, imageFileName);

                    // Save the image
                    picture.Image.Save(imagePath, picture.ImageFormat);
                    Console.WriteLine($"Saved: {imagePath}");
                }
            }
        }
    }
}

if (imageCount == 0)
{
    Console.WriteLine("No images found in the document.");
}
Enter fullscreen mode Exit fullscreen mode

This snippet demonstrates how to traverse paragraphs to find embedded DocPicture objects. Once found, it saves each image to a local file, using its original format. This is a robust way to handle various image types within your documents.


Conclusion

This article has demonstrated the power and simplicity of using C# .NET with Spire.Doc for .NET to perform essential Word Data Extraction tasks. We've covered how to efficiently Read Word Document content in C #, specifically targeting the extraction of plain text, structured data from tables, and embedded images.

By leveraging a library like Spire.Doc, developers can overcome the inherent complexities of parsing .docx files, transforming what was once a manual or cumbersome process into an automated, reliable solution. This capability is invaluable in scenarios ranging from report analysis and content migration to automated data processing workflows. We encourage you to experiment with these code examples and explore the broader capabilities of Spire.Doc for even more advanced document manipulation, further enhancing your .NET development toolkit.

Top comments (0)