如何在 Java 中預覽 PDF 文件
本文將示範如何在 Java 應用程式中使用IronPDF預覽 PDF 檔案。
IronPDF
IronPDF是一個高效能的 Java 庫,提供快速且準確的操作,使其成為 PDF 相關任務(如讀取PDF 文件、提取文字和圖像、合併和分割)的絕佳選擇。
使用IronPDF庫,您可以從 HTML、URL 和字串建立 PDF,並實現精確的像素級渲染。
先決條件
要在 Java 中建立一個用於檢視 PDF 文件的文件檢視器,您需要做好以下準備。
- 已安裝 JDK(Java 開發工具包)和 Swing UI 框架。
- Java IDE(整合開發環境),例如 Eclipse、NetBeans 或 IntelliJ IDEA。
- IronPDF Java 庫(您可以從IronPDF網站下載並將其包含在您的專案中)。
設定
- 在您選擇的 IDE 中建立一個新的 Java 專案。我使用的是 IntelliJ IDEA,並使用Maven建立了這個專案。
-
使用Maven將IronPDF庫新增至您的專案中,方法是在專案的
pom.xml檔案中新增以下所示的依賴項:<dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf</artifactId> <version>latest_version</version> </dependency><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 文檔,下一步是使用PdfDocument類將 PDF 文件加載到此 Java PDF 檢視器應用程式中。
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 類別的 main 方法中:
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,這是用於建立視窗的頂級容器。- 宣告的實例變數:
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()是程式的入口點。 它使用PDFPreview建構子來確保 Swing 元件在事件分發線程(GUI 操作專用線程)上建立和修改。
現在,執行該程序,PDF 文件檢視器將顯示已載入的 PDF 文件。
輸出的PDF文件
結論
本文示範如何使用IronPDF 適用於 Java 應用程式在 Java 應用程式中預覽 PDF 文件,以及如何存取和顯示 PDF 文件。
使用IronPDF,您可以輕鬆地將 PDF 預覽功能整合到您的 Java 應用程式中。 有關使用IronPDF 適用於 Java 的詳細指導和範例,您可以參考此範例。 有關 Java PDF 閱讀器閱讀 PDF 文件的教程,請訪問這篇文章。
IronPDF可免費用於開發用途。 要了解更多許可詳情,您可以訪問提供的許可頁面。 也提供免費的商業用途試用版。
常見問題解答
如何在Java應用程式中預覽PDF檔案?
您可以使用IronPDF將 PDF 頁面轉換為圖像,然後使用 Java Swing 元件顯示這些圖像,從而在 Java 應用程式中預覽 PDF 文件。這包括使用PdfDocument類別載入 PDF,將頁面轉換為BufferedImage ,並使用JPanel和JScrollPane來顯示它們。
如何將PDF庫整合到我的Java專案中?
若要將IronPDF整合到您的 Java 專案中,請將該程式庫作為 Maven 依賴項新增至專案的pom.xml檔案中,並指定 group ID 為“com.ironsoftware”,artifact ID 為“IronPDF”。請確保您已安裝 JDK 和 Java IDE。
如何使用Java將PDF頁面轉換為圖片?
使用IronPDF,您可以透過載入PdfDocument類別並將每一頁轉換為BufferedImage ,將 PDF 頁面轉換為圖片。然後,這些圖像可以儲存為 PNG 檔案以供進一步使用。
將 PDF 頁面顯示為圖像需要哪些 Java 元件?
要在 Java 中將 PDF 頁面顯示為圖像,可以使用 Java Swing 元件,特別是JPanel來保存圖像,使用JScrollPane來允許滾動瀏覽圖像。
在 Java 中建立 PDF 檢視器時,事件分發執行緒為何如此重要?
事件分發執行緒 (EDT) 至關重要,因為它確保所有 Swing 元件都在專用於 GUI 操作的執行緒上建立和修改,從而防止 Java 應用程式中潛在的執行緒問題。
我可以在沒有授權的情況下使用IronPDF 適用於 Java 進行開發嗎?
是的, IronPDF在開發階段可以免費使用。此外,它還提供免費的商業試用版,方便您在購買許可證之前體驗其各項功能。
哪裡可以找到更多關於在Java中使用IronPDF的資源?
IronPDF網站上提供了更多關於在 Java 中使用IronPDF 的資源、範例和教學。這些資源包括如何從 HTML 建立 PDF 以及各種 PDF 操作技巧的指南。
如何將 PDF 頁面轉換為圖像並在 Java Swing 中顯示?
若要使用IronPDF將 PDF 頁面轉換為映像,請使用PdfDocument載入 PDF,將每一頁轉換為BufferedImage ,然後將其儲存為 PNG 檔案。在 Java Swing 應用程式中使用JPanel和JScrollPane顯示這些圖像。




