In the realm of modern software development, handling and processing documents programmatically has become a crucial task. Extracting text from DOC files using C# is a common requirement for developers working on document processing applications. Whether you need to analyze content, convert documents, or automate tasks, extracting text is a fundamental step in many workflows. In this article, we’ll walk through the process of text extraction from DOC in C#. DOC is a legacy file format used by Microsoft Word before the introduction of DOCX in 2007. 古い言語であるにもかかわらず、多くの組織で今でも広く使用されており、開発者がこれらのドキュメントからテキストを抽出できることが重要になっています。C# を使用して DOC からテキストを抽出する 主な手順は次のとおりです。
C# を使用して DOC からテキストを抽出する手順
- GroupDocs.Parser for .NET をインストールして開発環境をセットアップします。これにより、DOC ファイルからテキストを取得できるようになります。
- 新しい Parser オブジェクトを作成し、DOC ファイルの場所を指定します。
- ParserオブジェクトのGetTextメソッドを使用してTextReaderを取得します。
- 最後に、TextReaderのReadToEndメソッドを使用してすべてのテキストを読み取ります。
上記の手順は、追加のソフトウェアを必要とせずに、Windows、macOS、または Linux で機能します。システムに .NET がインストールされている必要があります。Parser ライブラリは、テキスト抽出を処理するための強力で効率的な方法を提供するため、従来の DOC ファイルを扱う開発者にとって最適な選択肢となります。このアプローチにより、ドキュメント コンテンツを操作する能力が強化され、生産性とデータ処理機能の両方が向上します。環境が構成されたら、以下のコードを実装して、C# での DOC テキスト抽出を行うことができます。
C# を使用して DOC からテキストを抽出するコード
using System; | |
using System.IO; | |
using GroupDocs.Parser; | |
using GroupDocs.Parser.Options; | |
namespace ExtractTextfromDOCusingCSharp | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Apply the license to remove the limitations of the Parser library | |
License lic = new License(); | |
lic.SetLicense(@"GroupDocs.Parser.lic"); | |
// Instantiate the Parser class | |
using (Parser parser = new Parser("input.doc")) | |
{ | |
// Retrieve formatted text into the reader | |
using (TextReader reader = parser.GetFormattedText( | |
new FormattedTextOptions(FormattedTextMode.Html))) | |
{ | |
// Output the formatted text from the document | |
// If formatted text extraction is not supported, | |
// the reader will be null | |
Console.WriteLine(reader == null ? | |
"Formatted text extraction isn't supported" | |
: reader.ReadToEnd()); | |
Console.ReadLine(); | |
} | |
} | |
} | |
} | |
} |
ドキュメント変換、データ分析、コンテンツ管理のいずれに取り組んでいる場合でも、このアプローチはプロセスを自動化し、アプリケーションの効率を向上させるのに役立ちます。このアプローチにより、ドキュメント コンテンツを操作する能力が強化され、生産性とデータ処理能力の両方が向上します。この記事に従うことで、C# による DOC からのテキスト読み取り をアプリケーションにシームレスに統合し、効率的で信頼性の高いドキュメント処理を実現できます。推奨ライブラリをセットアップしてファイル パスを調整したら、提供されているコードをプロジェクトに追加するのは簡単です。
以前、C# を使用して XLSX からテキストを抽出する詳細なガイドを提供しました。より詳しく理解するには、C# を使用して XLSX からテキストを抽出する の方法に関する完全なチュートリアルをご覧ください。