如何在 Java 中預覽 PDF 文件
本文將演示如何在Java應用程式中使用IronPDF預覽PDF文件。
IronPDF
IronPDF 是一個高效能的Java程式庫,提供快速且準確的操作,使其成為與PDF相關任務的理想選擇,例如讀取 PDF文件、提取文字和圖像、合併及拆分。
借助IronPDF程式庫,您可以通過從HTML、URL和字串精確渲染建立PDF。
先決條件
要在Java中為PDF文件建立文件查看器,您需要準備以下事項。
- 安裝JDK (Java開發工具包) 以及Swing使用者介面框架。
- 一個Java整合開發環境(IDE),如Eclipse、NetBeans或IntelliJ IDEA。
- IronPDF for Java程式庫(您可以從IronPDF網站下載並將其包含在您的專案中)。
設置
- 在您選擇的IDE中建立一個新的Java專案。我使用的是IntelliJ IDEA,並使用Maven建立了專案。
-
通過在您的專案
pom.xml文件中新增如下所示的依賴關係,將IronPDF程式庫新增到您的專案中。<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文件,下一步是在此Java PDF查看應用程式中使用PdfDocument類載入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
);
}
程式碼說明
JFrame,這是一個用於建立窗口的頂級容器。- 宣告實例變數:
imagePaths。 example.pdf,並將其轉換為一系列圖像。imagePaths。PDFPreview()初始化應用程式。 它呼叫imagePaths。imagePanel被初始化,並設置其佈局為一個垂直的盒子佈局。- 它遍歷
imagePanel。 - 該源碼建立
imagePanel為其視窗。 - 接下來,程式碼將
scrollPane新增到框架的內容窗格,設置框架的標題,設定預設關閉操作,包裝組件,在螢幕上居中框架並使其可見。 main()是程式的入口點。 它使用PDFPreview構造函式,確保Swing組件在專用的GUI操作執行緒(事件派發執行緒)上建立和修改。
現在,執行程式,PDF文件查看器將顯示已載入的PDF文件。
輸出PDF文件
結論
本文演示了如何在基於Java的應用程式中使用IronPDF預覽PDF文件,以及如何存取和顯示PDF文件。
使用IronPDF,您可以輕鬆地將PDF預覽功能整合到您的Java應用程式中。 有關使用IronPDF for Java的詳細指南和範例,您可以參考這個範例。 有關Java的PDF閱讀器教程,請存取此文章來閱讀PDF文件。
常見問題
如何在Java應用程式中預覽PDF檔案?
您可以通過使用IronPDF將PDF頁面轉換成影像,然後使用Java Swing元件顯示這些影像來在Java應用程式中預覽PDF檔案。這涉及使用PdfDocument類載入PDF,將頁面轉換為BufferedImage,並使用JPanel和JScrollPane來顯示它們。
將PDF程式庫整合到我的Java專案有哪些步驟?
要將IronPDF整合到您的Java專案中,請在專案的pom.xml檔案中包含作為Maven相依項的程式庫,其群組ID為'com.ironsoftware',工件ID為'ironpdf'。確保您已安裝JDK和Java IDE。
如何使用Java將PDF頁面轉換為影像?
使用IronPDF,您可以通過使用PdfDocument類載入PDF檔案並將每個頁面轉換為BufferedImage來將PDF頁面轉換為影像。然後可以將這些影像儲存為PNG檔案以供進一步使用。
顯示為影像的PDF頁面需要哪些Java元件?
要在Java中顯示為影像的PDF頁面,您可以使用Java Swing元件,具體來說是使用JPanel來承載影像,以及使用JScrollPane來允許滾動影像。
為何在Java中建立PDF檢視器時事件分派執行緒很重要?
事件分派執行緒(EDT)至關重要,因為它確保所有Swing元件是在專用的執行緒上建立並修改的,為了GUI操作,從而防止Java應用程式可能的執行緒問題。
我可以在不授權的情況下使用IronPDF for Java開發嗎?
是的,IronPDF可以在開發期間免費使用。還可以免費試用版,用於商業用途,使您在購買授權之前探索其功能。
我在哪裡可以找到其他使用IronPDF in Java的資源?
其他使用IronPDF in Java的資源、範例和教程可在IronPDF網站上找到。這些資源包含有關從HTML建立PDF以及各種PDF操作技術的指南。
將PDF頁面轉換為影像並在Java Swing 中顯示的過程是什麼?
要使用IronPDF將PDF頁面轉換為影像,請使用PdfDocument載入PDF,將每頁轉換為BufferedImage,並將其儲存為PNG檔案。然後在Java Swing應用程式中使用JPanel和JScrollPane顯示這些影像。





