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

構造化PDFファイルから構造化コンテンツを抽出する

構造化PDFファイルと非構造化PDFファイル

構造化PDFファイル(ページ構造に関する情報を含む)または非構造化PDFファイル(構造情報を含まず、コンテンツが任意の順序で配置される)を作成することができます。これはPDFが作成される際に決定されるため、非構造化PDFファイルを構造化PDFファイルに変換することはできません。

PDFファイルには、PDF内のテキストコンテンツの構造を保持するためのメタデータタグを含めることができます(これはPDFファイル作成時のオプションです)。タグが存在する場合、JPedalはPDFファイルからテキストコンテンツを抽出するいくつかのメソッドを提供します。この場合、PDF内に存在する構造化されたテキストを抽出できます。存在しない場合、出力ファイルにはコンテンツが利用できなかったことを説明する簡単なメッセージが含まれます。

構造化PDFファイルの利点は、コンテンツを抽出して他の形式に変換できることです。JPedalは現在、このコンテンツをHTMLJSONXML、またはYAMLとして出力することをサポートしています。

PDFファイルには、構造化コンテンツに画像が含まれる場合もあります。これらは図と呼ばれます。図にalt textまたはactualtextがある場合、これらも出力に含まれます。

コマンドラインまたは他の言語からPDFの構造化テキストを抽出する

コンテンツをHTMLとして抽出するには

java --module-path . --add-modules com.idrsolutions.jpedal org/jpedal/examples/text/ExtractStructuredText 
"inputFileOrFolder" "outputFolder" "html"

コンテンツをJSONとして抽出するには

java --module-path . --add-modules com.idrsolutions.jpedal org/jpedal/examples/text/ExtractStructuredText 
"inputFileOrFolder" "outputFolder" "json"

コンテンツをMarkdownとして抽出するには

java --module-path . --add-modules com.idrsolutions.jpedal org/jpedal/examples/text/ExtractStructuredText 
"inputFileOrFolder" "outputFolder" "markdown"

コンテンツをXMLとして抽出するには

java --module-path . --add-modules com.idrsolutions.jpedal org/jpedal/examples/text/ExtractStructuredText 
"inputFileOrFolder" "outputFolder" "xml"

コンテンツをYAMLとして抽出するには

java --module-path . --add-modules com.idrsolutions.jpedal org/jpedal/examples/text/ExtractStructuredText 
"inputFileOrFolder" "outputFolder" "yaml"

図を含むコンテンツを抽出するには

java --module-path . --add-modules com.idrsolutions.jpedal org/jpedal/examples/text/ExtractStructuredText 
"inputFileOrFolder" "outputFolder" "xml" "figuresFolder" "jpeg"

モジュールの使用を推奨しますが、必要に応じてクラスパスを使用する こともできます。

APIメソッドにアクセスする例

ExtractStructuredTextProperties properties = new ExtractStructuredTextProperties();
properties.setFileOutputMode(OutputModes.XML);
//properties.setFileOutputMode(OutputModes.HTML);
//properties.setFileOutputMode(OutputModes.JSON);
//properties.setFileOutputMode(OutputModes.MARKDOWN);
//properties.setFileOutputMode(OutputModes.YAML);
ExtractStructuredText extract = new ExtractStructuredText("inputFile.pdf", properties);
//extract.setPassword("password");
if (extract.openPDFFile()) {
    // すべての構造化コンテンツを含むドキュメント
    Document anyStructuredText = extract.getStructuredTextContent();
     
    // 各ページの構造化コンテンツを含むドキュメントの配列
    Document[] anyStructuredTextPerPage = extract.getStructuredTextContentPerPage();
     
    // これらのメソッドは図(画像)も書き出します
    Document anymoreStructuredText = extract.getStructuredTextContentAndFigures("figuresFolder", "imageFormat");
    Document[] anymoreStructuredTextPerPage = extract.getStructuredTextContentAndFiguresPerPage("figuresFolder", "imageFormat");
}

extract.closePDFfile();

デフォルトの出力モードは、setFileOutputMode()で上書きしない限りXMLです

Javaでタグ付きPDFから構造化テキストを抽出する

//XMLは構造化テキストを抽出するためのデフォルトです
ExtractStructuredText.writeAllStructuredTextOutlinesToDir("inputFileOrFolder", "outputFolder");

//オプションをより細かく制御して構造化テキストを抽出する
final String password = null; //パスワードが不要な場合はnullを使用
final ErrorTracker tracker = null; //ErrorTracker実装を使用して抽出を監視できます
ExtractStructuredTextProperties properties = new ExtractStructuredTextProperties();
properties.setFileOutputMode(OutputModes.XML);
//properties.setFileOutputMode(OutputModes.HTML);
//properties.setFileOutputMode(OutputModes.JSON);
//properties.setFileOutputMode(OutputModes.MARKDOWN);
//properties.setFileOutputMode(OutputModes.YAML);
        
ExtractStructuredText.
        writeAllStructuredTextOutlinesToDir("inputFileOrFolder", password, "outputFolder", tracker, properties);

ExtractStructuredText.
        writeAllStructuredTextOutlinesAndFiguresToDir("inputFileOrFolder", password, "outputFolder", tracker, properties, "figuresFolder", "imageFormat");

デフォルトの出力モードは、setFileOutputMode()で上書きしない限りXMLです

この例では、JPedalのExtractStructuredText クラスを使用しています。ExtractStructuredTextは、ファイルに含まれる構造化コンテンツを詳述したXMLファイルを出力します。

使用される座標

抽出メソッドはすべて、指定された矩形内のPDFテキストを抽出します。この矩形の座標に必要な形式は、x1、y1(左上隅)、x2、y2(右下隅)です。

JavaはPDFとは異なり左上座標系を使用するため、AffineTransformで出力を垂直方向に反転する必要がある場合があります。PDFファイルのページ座標について詳しくは、このブログ記事 をご確認ください。