Skip to main content
Interwork Corporation
IDR Solutions Product Support Portal
モードの切替 ダーク/ライト/自動 モードの切替 ダーク/ライト/自動 モードの切替 ダーク/ライト/自動

C#を使用してPDFからテキストを抽出する

目次

  1. はじめに
  2. 前提条件
  3. コード例
  4. コールバックURLに結果を返す
  5. 設定オプション
  6. URLによるアップロード
  7. 認証の使用
  8. 詳細情報

はじめに

以下のチュートリアルでは、ホストされたJPedalクラウドAPIを使用してPDFからテキストを抽出する方法を説明します。独自のセルフホスト型JPedalマイクロサービス をセットアップすることもできます。

上記のサービスは通常のHTTPリクエストでアクセスできますが、このチュートリアルではREST API のシンプルなC#ラッパーを提供するオープンソースのC# IDRCloudClient を使用します。

前提条件

nugetを使用して、以下のコマンドでidrsolutions-csharp-clientパッケージ をインストールします:

nuget install idrsolutions-csharp-client

コード例

以下は、PDFからテキストを抽出するための基本的なコード例です。設定オプションと高度な機能については、以下を参照してください。

using System;
using System.Collections.Generic;
using idrsolutions_csharp_client;

class ExampleUsage
{
    static void Main(string[] args)
    {
        
        var client = new IDRCloudClient("https://my-self-hosted-service.com/" + IDRCloudClient.JPEDAL);

        try
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>
            { 
                ["input"] = IDRCloudClient.UPLOAD,
                ["file"] = "path/to/input.pdf",
                ["settings"] = "{\"mode\":\"extractText\",\"type\":\"plainText\"}"
            };

            Dictionary<string, string> results = client.Convert(parameters);

            String outputUrl = results.GetValueOrDefault("downloadUrl", "No download URL provided");
            
            client.DownloadResult(results, "path/to/output/dir");

            Console.WriteLine("Converted: " + outputUrl);
        }
        catch (Exception e)
        {
            Console.WriteLine("Conversion failed: " + e.Message);
        }
    }
}

コールバックURLに結果を返す

JPedalマイクロサービスは、抽出完了時に抽出のステータス を送信するコールバックURLを受け付けます。コールバックURLを使用することで、抽出の完了を確認するためにサービスをポーリングする必要がなくなります。
コールバックURLは、以下のようにconvertメソッドに提供できます。

Dictionary<string, string> parameters = new Dictionary<string, string>
{ 
    ["callbackUrl"] = "http://listener.url",
    ["input"] = IDRCloudClient.UPLOAD,
    ["file"] = "path/to/input.pdf",
    ["settings"] = "{\"mode\":\"extractText\",\"type\":\"plainText\"}"
};

設定オプション

JPedal APIは、抽出をカスタマイズするためのキーと値のペアの設定オプションを含む、文字列化されたJSONオブジェクトを受け付けます。設定はparameters配列に追加する必要があります。PDFからテキストを抽出するための設定オプションの完全なリストはこちら で確認できます。

["settings"] = "{\"key\":\"value\",\"key\":\"value\"}"

URLによるアップロード

ローカルファイルをアップロードする以外に、JPedalマイクロサービスがダウンロードして抽出を実行するURLを提供することもできます。これを行うには、parameters変数のinputとfileの値を以下のように置き換える必要があります。

["input"] = IDRCloudClient.DOWNLOAD
["url"] = "http://exampleURL/exampleFile.pdf"

認証の使用

PDFからテキストを抽出するためにユーザー名とパスワードが必要な独自のJPedalマイクロサービスをデプロイした場合、各変換時にそれらを提供する必要があります。これらは、以下のようにusernameとpasswordという名前の2つの変数をconvertメソッドに渡すことで提供されます。

var client = new IDRCloudClient("http://exampleURL.com/" + IDRCloudClient.JPEDAL, "username", "password");

詳細情報

GitHubのIDRCloudClient
NugetのIDRCloudClient
JPedalマイクロサービスAPI
JPedalマイクロサービスの使用方法