---
title: "How do you extract text content from a PDF or DOCX file in C#?"  
description: "How do you extract text content from a PDF or DOCX file in C#?"  
author: "ICSM Computer"  
published: 2025-05-18  
updated: 2025-05-18  
canonical: https://www.mindstick.com/interview/34137/how-do-you-extract-text-content-from-a-pdf-or-docx-file-in-c-sharp  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How do you extract text content from a PDF or DOCX file in C#?

To **extract text content from a PDF or DOCX file in C#**, you can use dedicated libraries designed for parsing each format.

## Extracting Text from a PDF

### Recommended Library: PdfPig (free and open-source)

```cs
Install-Package UglyToad.PdfPig
```

### Example:

```cs
using UglyToad.PdfPig;
using UglyToad.PdfPig.Content;
using System;

public static class PdfTextExtractor
{
    public static void ExtractText(string filePath)
    {
        using var document = PdfDocument.Open(filePath);
        foreach (Page page in document.GetPages())
        {
            string text = page.Text;
            Console.WriteLine(text);
        }
    }
}
```

## Alternative Libraries:

1. `iTextSharp` (not free for commercial use)
2. `PdfSharp` (limited text extraction)

## Extracting Text from a DOCX File

### Recommended Library: Open XML SDK (from Microsoft)

```cs
Install-Package DocumentFormat.OpenXml
```

### Example:

```cs
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Linq;

public static class DocxTextExtractor
{
    public static string ExtractText(string filePath)
    {
        using var wordDoc = WordprocessingDocument.Open(filePath, false);
        var body = wordDoc.MainDocumentPart.Document.Body;
        return body.InnerText;
    }
}
```

## Notes

1. For PDFs with images/scanned content, you'll need **OCR tools** like **Tesseract.NET**.
2. For DOC files (old `.doc` format), use **Microsoft.Office.Interop.Word** (requires Word installed) or **Aspose.Words** (commercial).
3. Would you like to extract metadata (author, title) or just plain text?

## Answers

### Answer by ICSM Computer

To **extract text content from a PDF or DOCX file in C#**, you can use dedicated libraries designed for parsing each format.

## Extracting Text from a PDF

### Recommended Library: PdfPig (free and open-source)

```cs
Install-Package UglyToad.PdfPig
```

### Example:

```cs
using UglyToad.PdfPig;
using UglyToad.PdfPig.Content;
using System;

public static class PdfTextExtractor
{
    public static void ExtractText(string filePath)
    {
        using var document = PdfDocument.Open(filePath);
        foreach (Page page in document.GetPages())
        {
            string text = page.Text;
            Console.WriteLine(text);
        }
    }
}
```

## Alternative Libraries:

1. `iTextSharp` (not free for commercial use)
2. `PdfSharp` (limited text extraction)

## Extracting Text from a DOCX File

### Recommended Library: Open XML SDK (from Microsoft)

```cs
Install-Package DocumentFormat.OpenXml
```

### Example:

```cs
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Linq;

public static class DocxTextExtractor
{
    public static string ExtractText(string filePath)
    {
        using var wordDoc = WordprocessingDocument.Open(filePath, false);
        var body = wordDoc.MainDocumentPart.Document.Body;
        return body.InnerText;
    }
}
```

## Notes

1. For PDFs with images/scanned content, you'll need **OCR tools** like **Tesseract.NET**.
2. For DOC files (old `.doc` format), use **Microsoft.Office.Interop.Word** (requires Word installed) or **Aspose.Words** (commercial).
3. Would you like to extract metadata (author, title) or just plain text?


---

Original Source: https://www.mindstick.com/interview/34137/how-do-you-extract-text-content-from-a-pdf-or-docx-file-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
