透かしなしで本番環境でテストしてください。
必要な場所で動作します。
30日間、完全に機能する製品をご利用いただけます。
数分で稼働させることができます。
製品トライアル期間中にサポートエンジニアリングチームへの完全アクセス
この記事では、Javaアプリケーション内でIronPDFを使用してPDFファイルをプレビューする方法を示します。
IronPDFは高性能なJavaライブラリであり、高速で正確な操作を提供し、PDFファイルの読み取り、テキストと画像の抽出、結合、分割など、PDF関連のタスクに最適な選択です。
IronPDFライブラリを使用すると、HTMLからPDFを作成し、URLや文字列を使用してピクセルパーフェクトなレンダリングを実現できます。
JavaでPDFドキュメントのビューアーを作成するには、以下の要件が必要です。
JDK(Java Development Kit)およびSwing UIフレームワークをインストール。
Eclipse、NetBeans、IntelliJ IDEA などの Java IDE(統合開発環境)。
選択したIDEで新しいJavaプロジェクトを作成します。私はIntelliJ IDEAを使用し、Mavenを使用してプロジェクトを作成しました。
pom.xml
ファイルに追加します: :ProductInstall
:ProductInstall
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;
PDFドキュメントを表示するには、PdfDocument
クラスを使用して、このJava PDFビューアーアプリケーションにPDFファイルをロードする次のステップを踏む必要があります。
public class PDFPreview extends JFrame {
private List<String> imagePaths = new ArrayList<>();
private List<String> ConvertToImages() throws IOException {
PdfDocument pdfDocument = PdfDocument.fromFile(Paths.get("example.pdf"));
// Create a list of paths for images
List<BufferedImage> extractedImages = pdfDocument.toBufferedImages();
int i = 1;
for (BufferedImage extractedImage : extractedImages) {
String fileName = "assets/images/" + i + ".png";
ImageIO.write(extractedImage, "PNG", new File(fileName));
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 {
PdfDocument pdfDocument = PdfDocument.fromFile(Paths.get("example.pdf"));
// Create a list of paths for images
List<BufferedImage> extractedImages = pdfDocument.toBufferedImages();
int i = 1;
for (BufferedImage extractedImage : extractedImages) {
String fileName = "assets/images/" + i + ".png";
ImageIO.write(extractedImage, "PNG", new File(fileName));
imagePaths.add("assets/images/" + i + ".png");
i++;
}
return imagePaths;
}
}
出力PDFファイル
画像に変換:
PDFファイルを画像に変換
次に、Java Swingコンポーネントを使用して、変換された画像をプレビューウィンドウに表示できます。
public class PDFPreview extends JFrame {
private JPanel imagePanel;
private JScrollPane scrollPane;
public PDFPreview() {
try {
imagePaths = this.ConvertToImages();
} catch (Exception e) {
}
// 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 {
imagePaths = this.ConvertToImages();
} catch (Exception e) {
}
// 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) {
SwingUtilities.invokeLater(
PDFPreview::new
);
}
public static void main(String[] args) {
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ベースのアプリケーションでIronPDF for Javaを使用して、Javaアプリケーション内でPDFファイルをプレビューする方法、およびPDFファイルにアクセスして表示する方法を示しました。
IronPDFを使用すると、JavaアプリケーションにPDFプレビュー機能を簡単に統合できます。 Java 向けの IronPDF の利用に関する詳細なガイダンスと例については、この例を参照してください。 Java PDFリーダーチュートリアルについては、このPDFファイルを読む記事をご覧ください。
IronPDFは開発目的で無料です。 ライセンスの詳細については、提供されたライセンスページをご覧ください。 商業利用向けの無料試用版も利用可能です。