フッターコンテンツにスキップ
JAVA用IRONPDFの使用

JavaでPDFファイルを読む方法

この記事では、プログラムによってソフトウェアアプリケーション内でPDFファイルを開くためのPDFリーダーを作成する方法を探ります。 このタスクを効果的に実行するために、IronPDF for JavaはJavaプログラム内でファイル名を使用してPDFファイルを開き、読み取るのに役立つシステムライブラリの1つです。

class="hsg-featured-snippet">

JavaでPDFファイルを読む方法

  1. IronPDF Javaライブラリをダウンロード
  2. 既存のPDFドキュメントを読み込むためにfromFileメソッドを使用
  3. PDFに埋め込まれたテキストを抽出するためにextractAllTextメソッドを呼び出す
  4. 特定のページからテキストを抽出するにはextractTextFromPageメソッドを使用
  5. URLからレンダリングされたPDFからテキストを取得

IronPDF

IronPDF - Java Library は、すでに成功を収めている.NET Frameworkの上に構築されています。 これにより、IronPDFはApache PDFBoxのような他のクラスライブラリと比較して、PDFドキュメントを操作するための多用途なツールとなります。 それはコンテンツの抽出と解析、テキストの読み込み、画像の読み込みを提供します。 It also provides options to customize the PDF pages such as page layout, margins, header and footer, page orientation, and much more.

これに加えて、IronPDFは他のファイル形式からの変換、パスワードによるPDF保護、デジタル署名、PDF文書の結合、分割もサポートしています。

JavaでPDFファイルを読む方法

前提条件

IronPDFを使用してJava PDFリーダーを作成するには、コンピュータに次のコンポーネントがインストールされている必要があります:

  1. JDK - Java Development KitはJavaプログラムの構築と実行に必要です。 インストールされていない場合は、Oracleウェブサイトからダウンロードしてください。
  2. IDE - Integrated Development Environmentはプログラムの作成、編集、デバッグをサポートするソフトウェアです。 Eclipse、NetBeans、IntelliJなど、Java用の任意のIDEをダウンロードしてください。
  3. Maven - Mavenはライブラリを中央リポジトリからダウンロードするのを助ける自動化ツールです。 Apache Maven Websiteからダウンロードしてください。
  4. IronPDF - 最後に、JavaでPDFファイルを読み取るためにIronPDFが必要です。 これをJava Mavenプロジェクトに依存関係として追加する必要があります。 pom.xmlファイルに示されているように、IronPDFアーティファクトとslf4j依存を含めてください:
<!-- Add Maven dependencies for IronPDF -->
<dependencies>
    <!-- IronPDF Dependency -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>your-version-here</version>
    </dependency>

    <!-- SLF4J Dependency necessary for logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.32</version>
    </dependency>
</dependencies>
<!-- Add Maven dependencies for IronPDF -->
<dependencies>
    <!-- IronPDF Dependency -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>your-version-here</version>
    </dependency>

    <!-- SLF4J Dependency necessary for logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.32</version>
    </dependency>
</dependencies>
XML

必要なインポートの追加

まず、Javaソースファイルの最初に以下のコードを追加して、IronPDFからのすべての必要なメソッドを参照してください:

import com.ironsoftware.ironpdf.*;
// Necessary imports from IronPDF library
import com.ironsoftware.ironpdf.*;
// Necessary imports from IronPDF library
JAVA

次に、有効なライセンスキーでIronPDFを設定して、そのメソッドを使用します。 メインメソッドでsetLicenseKeyメソッドを呼び出します。

License.setLicenseKey("Your license key");
// Set your IronPDF license key - required for full version
License.setLicenseKey("Your license key");
// Set your IronPDF license key - required for full version
JAVA

注意: PDFを作成、読み取り、印刷するための無料試用ライセンスキーを取得できます。

既存のPDFファイルをJavaで読み取る

PDFファイルを読むには、PDFファイルが存在するか、作成する必要があります。 この記事では、すでに作成されたPDFファイルを使用します。コードはシンプルで、ドキュメントからテキストを抽出するための2ステッププロセスです:

// Load the PDF document from file
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
// Extract all text from the PDF
String text = pdf.extractAllText();
// Print the extracted text
System.out.println(text);
// Load the PDF document from file
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
// Extract all text from the PDF
String text = pdf.extractAllText();
// Print the extracted text
System.out.println(text);
JAVA

上記のコードでは、fromFileはPDFドキュメントを開きます。 Paths.getメソッドはファイルのディレクトリを取得し、ファイルからコンテンツを抽出する準備が整います。その後、[extractAllText](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#extractAllText())はドキュメント内のすべてのテキストを読み取ります。

出力は以下の通りです:

JavaでPDFファイルを読む方法、図1:PDFテキストの読み取り出力 PDFテキストの読み取り出力

特定のページからテキストを読む

IronPDFはPDF内の特定のページからのコンテンツを読み取ることもできます。 extractTextFromPageメソッドは、テキストが読み取られるページの範囲を受け入れるPageSelectionオブジェクトを使用します。

次の例では、PDFドキュメントの2ページ目からテキストが抽出されています。 PageSelection.singlePageは、抽出するページのインデックスを取ります(インデックスは0から始まります)。

// Load the PDF document from file
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
// Extract text from the second page (page index based, starts at 0, so 1 means second page)
String text = pdf.extractTextFromPage(PageSelection.singlePage(1));
// Print the extracted text from the specified page
System.out.println(text);
// Load the PDF document from file
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
// Extract text from the second page (page index based, starts at 0, so 1 means second page)
String text = pdf.extractTextFromPage(PageSelection.singlePage(1));
// Print the extracted text from the specified page
System.out.println(text);
JAVA

JavaでPDFファイルを読む方法、図2:PDFテキストの読み取り出力 PDFテキストの読み取り出力

Other methods available in the PageSelection class which can be used to extract text from various pages include: [firstPage](/java/object-reference/api/com/ironsoftware/ironpdf/edit/PageSelection.html#lastPage()), [lastPage](/java/object-reference/api/com/ironsoftware/ironpdf/edit/PageSelection.html#firstPage()), pageRange, and [allPages](/java/object-reference/api/com/ironsoftware/ironpdf/edit/PageSelection.html#allPages()).

新しく生成されたPDFファイルからテキストを読む

検索テキストはHTMLファイルまたはURLから新しく生成されたPDFファイルからも実行できます。 次のサンプルコードでは、URLからPDFを生成し、ウェブサイトからすべてのテキストを抽出します。

// Generate PDF from a URL
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://unsplash.com/");
// Extract all text from the generated PDF
String text = pdf.extractAllText();
// Print the extracted text from the URL
System.out.println("Text extracted from the website: " + text);
// Generate PDF from a URL
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://unsplash.com/");
// Extract all text from the generated PDF
String text = pdf.extractAllText();
// Print the extracted text from the URL
System.out.println("Text extracted from the website: " + text);
JAVA

JavaでPDFファイルを読む方法、図3:新しいファイルから読む 新しいファイルから読む

IronPDFはPDFファイルから画像を抽出することもできます。

完全なコードは次のとおりです。

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.edit.PageSelection;

import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the IronPDF license key for commercial use
        License.setLicenseKey("YOUR LICENSE KEY HERE");

        // Read text from a specific page in an existing PDF
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
        String text = pdf.extractTextFromPage(PageSelection.singlePage(1));
        System.out.println(text);

        // Read all text from a PDF generated from a URL
        pdf = PdfDocument.renderUrlAsPdf("https://unsplash.com/");
        text = pdf.extractAllText();
        System.out.println("Text extracted from the website: " + text);
    }
}
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.edit.PageSelection;

import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the IronPDF license key for commercial use
        License.setLicenseKey("YOUR LICENSE KEY HERE");

        // Read text from a specific page in an existing PDF
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
        String text = pdf.extractTextFromPage(PageSelection.singlePage(1));
        System.out.println(text);

        // Read all text from a PDF generated from a URL
        pdf = PdfDocument.renderUrlAsPdf("https://unsplash.com/");
        text = pdf.extractAllText();
        System.out.println("Text extracted from the website: " + text);
    }
}
JAVA

まとめ

この記事では、IronPDFを使用してJavaでPDFを開き、読む方法を説明しました。

IronPDFはHTMLやURLから簡単にPDFを作成したり、異なるファイル形式から変換したりするのに役立ちます。 それはまた、PDFタスクを迅速かつ容易に完了するのに役立ちます。

IronPDFを30日間無料で試用して、生産環境でどのように機能するかを確かめてみてください。 IronPDFの商用ライセンスオプションを探る。価格は$799から始めます。

よくある質問

JavaでPDFリーダーを作成するにはどうすればよいですか?

IronPDFを使用してJavaでPDFリーダーを作成するには、`fromFile`メソッドを使用してPDFドキュメントを読み込み、`extractAllText`などのメソッドを使用してコンテンツを解析および操作します。

JavaでIronPDFを使用するための前提条件をインストールする手順は何ですか?

JavaでIronPDFを使用するには、Java Development Kit (JDK) をインストールし、EclipseやIntelliJのような統合開発環境 (IDE)をセットアップし、依存関係管理のためにMavenを構成し、プロジェクトにIronPDFライブラリを含める必要があります。

JavaでPDFファイルからテキストを抽出するにはどうすればよいですか?

JavaでIronPDFを使用してPDFファイルからテキストを抽出するには、`extractAllText`メソッドを使用してドキュメント全体のテキストを取得するか、`extractTextFromPage`を使用して特定のページからテキストを抽出できます。

JavaでURLからPDFを生成できますか?

はい、IronPDFを使用すると、`renderUrlAsPdf`メソッドを使用して、WebコンテンツをPDF形式に変換することでURLからPDFを生成できます。

IronPDFはJavaでPDFにパスワード保護を追加することをサポートしていますか?

はい、IronPDFはパスワード保護の追加をサポートしており、デジタル署名やドキュメントの結合や分割などの他の機能も提供しています。

IronPDFはJavaでどのファイル形式をPDFに変換できますか?

IronPDFはHTMLおよびその他のドキュメント形式を含むさまざまなファイル形式をPDFに変換することができ、PDF生成と操作に柔軟なオプションを提供します。

JavaでIronPDFの試用版は利用できますか?

はい、IronPDFは30日間の無料試用版を提供しており、Javaアプリケーションでの機能をテストし、ライセンスを購入する前にそのパフォーマンスを評価することができます。

Javaライブラリを使用してPDFドキュメントの特定のページからテキストを抽出するにはどうすればよいですか?

IronPDFを使用すると、`extractTextFromPage`メソッドを使用して、ページ番号や範囲を指定することでPDFの特定のページからテキストを抽出できます。

Darrius Serrant
フルスタックソフトウェアエンジニア(WebOps)

Darrius Serrantは、マイアミ大学でコンピュータサイエンスの学士号を取得し、Iron SoftwareでフルスタックWebOpsマーケティングエンジニアとして働いています。若い頃からコーディングに惹かれ、コンピューティングを神秘的かつアクセス可能なものとし、創造性と問題解決のための完璧な媒体と考えていました。

Iron Softwareでは、新しいものを創造することと、複雑なコンセプトをより理解しやすくすることを楽しんでいます。Resident Developerの一人として、次世代に専門知識を共有するために、学生を教えることにも志願しました。

Darriusにとって、その仕事は価値があり、実際の影響があるため、満足感があります。