JavaでPDFファイルをプレビューする方法
この記事では、Javaアプリケーション内でPDFファイルをプレビューするためにIronPDFを使用する方法を示します。
IronPDF
IronPDFは、高性能なJavaライブラリであり、高速で正確な操作を提供し、PDFファイルの読み取り、テキストと画像の抽出、マージ、分割などのPDF関連タスクに最適な選択となります。
IronPDFライブラリを使用すると、正確なピクセルパーフェクトレンダリングでHTML、URL、および文字列からPDFを作成できます。
前提条件
JavaでPDFドキュメント用のドキュメントビューアーを作成するには、次の要素を整えておく必要があります。
- JDK(Java Development Kit)とSwing UIフレームワークがインストールされていること。
- Eclipse、NetBeans、IntelliJ IDEA などのJava IDE (統合開発環境) を用意してください。
- IronPDFライブラリ(IronPDFのウェブサイトからダウンロードしてプロジェクトに含めることができます)。
セットアップ
- 選んだIDEで新しいJavaプロジェクトを作成します。私はIntelliJ IDEAを使用し、Mavenでプロジェクトを作成しました。
プロジェクトの
pom.xmlファイルに以下の依存関係を追加して、プロジェクトにIronPDFライブラリをMavenで追加します。<!-- Add IronPDF dependency in your pom.xml --> <dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf</artifactId> <version>latest_version</version> </dependency><!-- Add IronPDF dependency in your pom.xml --> <dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf</artifactId> <version>latest_version</version> </dependency>XML必要なインポートを追加します。
import com.ironsoftware.ironpdf.PdfDocument; import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List;import com.ironsoftware.ironpdf.PdfDocument; import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List;JAVA
PDFファイルフォーマットの読み込み
PDFドキュメントを表示するためには、次のステップとして、このJava PDFビューアーアプリケーション内でPDFファイルを読み込む必要があります。[PdfDocument](/java/object-reference/api/com/Iron Software/ironpdf/PdfDocument.html)クラスを使用します。
public class PDFPreview extends JFrame {
private List<String> imagePaths = new ArrayList<>();
private List<String> ConvertToImages() throws IOException {
// Load the PDF document from a file
PdfDocument pdfDocument = PdfDocument.fromFile(Paths.get("example.pdf"));
// Convert the PDF pages to a list of BufferedImages
List<BufferedImage> extractedImages = pdfDocument.toBufferedImages();
int i = 1;
// Iterate over the extracted images and save each to an image file
for (BufferedImage extractedImage : extractedImages) {
String fileName = "assets/images/" + i + ".png";
ImageIO.write(extractedImage, "PNG", new File(fileName));
// Store the file paths in the image paths list
imagePaths.add("assets/images/" + i + ".png");
i++;
}
return imagePaths;
}
}public class PDFPreview extends JFrame {
private List<String> imagePaths = new ArrayList<>();
private List<String> ConvertToImages() throws IOException {
// Load the PDF document from a file
PdfDocument pdfDocument = PdfDocument.fromFile(Paths.get("example.pdf"));
// Convert the PDF pages to a list of BufferedImages
List<BufferedImage> extractedImages = pdfDocument.toBufferedImages();
int i = 1;
// Iterate over the extracted images and save each to an image file
for (BufferedImage extractedImage : extractedImages) {
String fileName = "assets/images/" + i + ".png";
ImageIO.write(extractedImage, "PNG", new File(fileName));
// Store the file paths in the image paths list
imagePaths.add("assets/images/" + i + ".png");
i++;
}
return imagePaths;
}
}
出力PDFファイル
画像に変換:
PDFファイルを画像に変換
PDFビューアーウィンドウの作成
Java Swingコンポーネントを使用して、変換された画像をプレビューウィンドウに表示できます。
public class PDFPreview extends JFrame {
private JPanel imagePanel;
private JScrollPane scrollPane;
public PDFPreview() {
try {
// Convert the PDF to images and store the image paths
imagePaths = this.ConvertToImages();
} catch (Exception e) {
e.printStackTrace();
}
// Create imagePanel
imagePanel = new JPanel();
imagePanel.setLayout(new BoxLayout(imagePanel, BoxLayout.Y_AXIS));
// Add images to the panel
for (String imagePath : imagePaths) {
ImageIcon imageIcon = new ImageIcon(imagePath);
JLabel imageLabel = new JLabel(imageIcon);
imageLabel.setBorder(new EmptyBorder(10, 10, 10, 10));
imagePanel.add(imageLabel);
}
// Create the scroll pane and add imagePanel to it
scrollPane = new JScrollPane(imagePanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// Set up the frame
getContentPane().add(scrollPane);
setTitle("PDF Viewer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}public class PDFPreview extends JFrame {
private JPanel imagePanel;
private JScrollPane scrollPane;
public PDFPreview() {
try {
// Convert the PDF to images and store the image paths
imagePaths = this.ConvertToImages();
} catch (Exception e) {
e.printStackTrace();
}
// Create imagePanel
imagePanel = new JPanel();
imagePanel.setLayout(new BoxLayout(imagePanel, BoxLayout.Y_AXIS));
// Add images to the panel
for (String imagePath : imagePaths) {
ImageIcon imageIcon = new ImageIcon(imagePath);
JLabel imageLabel = new JLabel(imageIcon);
imageLabel.setBorder(new EmptyBorder(10, 10, 10, 10));
imagePanel.add(imageLabel);
}
// Create the scroll pane and add imagePanel to it
scrollPane = new JScrollPane(imagePanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// Set up the frame
getContentPane().add(scrollPane);
setTitle("PDF Viewer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}メインクラスコンストラクタの呼び出し
最後に、PDFPreviewクラスのメインメソッドに次のコードを配置します。
public static void main(String[] args) {
// Run the PDF viewer in the Event Dispatch Thread
SwingUtilities.invokeLater(
PDFPreview::new
);
}public static void main(String[] args) {
// Run the PDF viewer in the Event Dispatch Thread
SwingUtilities.invokeLater(
PDFPreview::new
);
}コードの説明
PDFPreviewはウィンドウ作成用のトップレベルコンテナであるJFrameを拡張します。- インスタンス変数が宣言されます:
imagePanel、scrollPane、imagePaths。 ConvertToImages()はPDFファイルexample.pdfを受け取り、画像のシリーズに変換します。PdfDocumentはPDFファイルを読み込み、各ページをBufferedImageに変換し、assets/images/ディレクトリにPNGとして保存し、そのパスをimagePathsに追加します。PDFPreview()はアプリケーションを初期化します。 これは、ConvertToImages()を呼び出してimagePathsを埋めます。imagePanelが初期化され、そのレイアウトが垂直ボックスレイアウトとして設定されます。imagePathsを繰り返し処理し、各画像にImageIconを作成してJLabelに追加し、ラベルをimagePanelに追加します。- ソースコードは
JScrollPaneを作成し、imagePanelをそのビューポートとして設定します。 - 次に、コードは
scrollPaneをフレームのコンテンツペインに追加し、フレームのタイトルを設定し、デフォルトのクローズ操作を設定し、コンポーネントをパックし、画面中央にフレームを配置して表示可能にします。 main()はプログラムのエントリーポイントです。SwingUtilities.invokeLater()を使用してPDFPreviewコンストラクタを呼び出し、SwingコンポーネントがGUI操作専用のイベントディスパッチスレッドで作成および変更されることを保証します。
プログラムを実行すると、読み込まれたPDFドキュメントを表示するPDFドキュメントファイルビューアーが表示されます。
出力PDFファイル
結論
この記事では、JavaベースのアプリケーションでPDFファイルをプレビューするためにIronPDFを使用する方法、およびPDFファイルをアクセスして表示する方法を示しました。
IronPDFを使用すると、JavaアプリケーションにPDFプレビュー機能を簡単に統合できます。 IronPDFをJavaで利用するための詳細なガイダンスと例については、この例を参照できます。 JavaのPDFリーダーチュートリアルについては、この記事を訪れてPDFファイルを読み取ることができます。
IronPDFは開発目的で無料です。 ライセンスの詳細については、提供されたライセンスページを訪れることができます。 商用利用のための無料トライアルも利用可能です。
よくある質問
JavaアプリケーションでPDFファイルをプレビューするにはどうすればよいですか?
JavaアプリケーションでPDFファイルをプレビューするには、IronPDFを使用してPDFページをイメージに変換し、Java Swingコンポーネントを使用してこれらのイメージを表示します。これはPdfDocumentクラスを使用してPDFをロードし、ページをBufferedImageに変換し、JPanelとJScrollPaneを使用して表示することを含みます。
JavaプロジェクトにPDFライブラリを統合する手順は何ですか?
IronPDFをJavaプロジェクトに統合するには、Maven依存関係としてプロジェクトのpom.xmlファイルにライブラリを含め、グループIDを「com.Iron Software」、アーティファクトIDを「ironpdf」とします。JDKとJava IDEがインストールされていることを確認してください。
Javaを使用してPDFページをイメージに変換するにはどうすればよいですか?
IronPDFを使用すると、PdfDocumentクラスを使用してPDFドキュメントをロードし、各ページをBufferedImageに変換することで、PDFページをイメージに変換できます。これらのイメージはPNGファイルとして保存できます。
JavaでPDFページをイメージとして表示するには、どのJavaコンポーネントが必要ですか?
Javaでは、Java Swingコンポーネントを使用してPDFページをイメージとして表示できます。具体的には、JPanelを使用してイメージを保持し、JScrollPaneを使用してイメージをスクロールします。
JavaでPDFビューアを作成する際にEvent Dispatch Threadが重要な理由は何ですか?
Event Dispatch Thread(EDT)は、すべてのSwingコンポーネントがGUI操作用の専用スレッドで作成および変更されることを保証し、Javaアプリケーションでのスレッド問題を防ぐため重要です。
開発のために、IronPDF for Javaをライセンスなしで使用できますか?
はい、IronPDFは開発中無料で利用できます。ライセンスを購入する前にその機能を探るための商業用の無料トライアルも利用可能です。
JavaでIronPDFを使用するための追加リソースはどこにありますか?
JavaでIronPDFを使用するための追加リソース、例、およびチュートリアルは、IronPDFのウェブサイトで利用可能です。これらのリソースには、HTMLからPDFを作成するガイドやさまざまなPDF操作技術が含まれます。
PDFページをイメージに変換し、Java Swingで表示するプロセスは何ですか?
IronPDFを使用してPDFページをイメージに変換するには、PdfDocumentを使用してPDFをロードし、各ページをBufferedImageに変換してPNGファイルとして保存します。これらの画像をJava SwingアプリケーションでJPanelとJScrollPaneを使って表示します。










