JavaでPDFファイルを読む方法
この記事では、プログラムによってソフトウェアアプリケーション内でPDFファイルを開くためのPDFリーダーを作成する方法を探ります。 このタスクを効果的に実行するために、IronPDF for JavaはJavaプログラム内でファイル名を使用してPDFファイルを開き、読み取るのに役立つシステムライブラリの1つです。
JavaでPDFファイルを読む方法
- IronPDFのJavaライブラリをダウンロードしてください。
- 既存のPDF文書を読み込むには
fromFileメソッドを使用します。 - PDFに埋め込まれたテキストを抽出するために
extractAllTextメソッドを呼び出します。 extractTextFromPageメソッドで特定のページからテキストを抽出します。- URLからレンダリングされたPDFからテキストを取得する
IronPDF
IronPDF - Java Library は、すでに成功を収めている.NET Frameworkの上に構築されています。 これにより、IronPDFはApache PDFBoxのような他のクラスライブラリと比較して、PDFドキュメントを操作するための多用途なツールとなります。 それはコンテンツの抽出と解析、テキストの読み込み、画像の読み込みを提供します。 また、ページレイアウト、マージン、ヘッダーとフッター、ページの向きなど、PDFページをカスタマイズするためのオプションも提供しています。
これに加えて、IronPDFは他のファイル形式からの変換、パスワードによるPDF保護、デジタル署名、PDF文書の結合、分割もサポートしています。
JavaでPDFファイルを読む方法
前提条件
IronPDFを使用してJava PDFリーダーを作成するには、コンピュータに次のコンポーネントがインストールされている必要があります:
- JDK - Java Development KitはJavaプログラムの構築と実行に必要です。 インストールされていない場合は、Oracleウェブサイトからダウンロードしてください。
- IDE - Integrated Development Environmentはプログラムの作成、編集、デバッグをサポートするソフトウェアです。 Eclipse、NetBeans、IntelliJなど、Java用の任意のIDEをダウンロードしてください。
- Maven - Mavenはライブラリを中央リポジトリからダウンロードするのを助ける自動化ツールです。 Apache Maven Websiteからダウンロードしてください。
- 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>必要なインポートの追加
まず、Javaソースファイルの最初に以下のコードを追加して、IronPDFからのすべての必要なメソッドを参照してください:
import com.ironsoftware.ironpdf.*;
// Necessary imports from IronPDF libraryimport com.ironsoftware.ironpdf.*;
// Necessary imports from IronPDF library次に、有効なライセンスキーでIronPDFを設定して、そのメソッドを使用します。 メインメソッドでsetLicenseKeyメソッドを呼び出します。
License.setLicenseKey("Your license key");
// Set your IronPDF license key - required for full versionLicense.setLicenseKey("Your license key");
// Set your IronPDF license key - required for full version注意: 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);上記のコードでは、[fromFile](/java/object-reference/api/com/Iron Software/ironpdf/PdfDocument.html#fromFile(java.nio.file.Path))はPDFドキュメントを開きます。 Paths.getメソッドはファイルのディレクトリを取得し、ファイルからコンテンツを抽出する準備が整います。その後、[extractAllText](/java/object-reference/api/com/Iron Software/ironpdf/PdfDocument.html#extractAllText())はドキュメント内のすべてのテキストを読み取ります。
出力は以下の通りです:
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);
PDFテキストの出力を読み取る
さまざまなページからテキストを抽出するために使用できるPageSelectionクラスで利用可能な他のメソッドには、[firstPage](/java/object-reference/api/com/Iron Software/ironpdf/edit/PageSelection.html#lastPage()), [lastPage](/java/object-reference/api/com/Iron Software/ironpdf/edit/PageSelection.html#firstPage(), [pageRange](/java/object-reference/api/com/Iron Software/ironpdf/edit/PageSelection.html#pageRange(int,int), および [allPages](/java/object-reference/api/com/Iron Software/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);
新しいファイルから読む
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);
}
}まとめ
この記事では、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の特定のページからテキストを抽出できます。










