使用Java在桌面应用程序中打开PDF,可以使用Java的内置库javax.swing
和第三方库Apache PDFBox
。
javax.swing
库打开PDFimport java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.icepdf.ri.common.ComponentKeyBinding;
import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;
public class PDFViewer {
public static void main(String[] args) {
JFrame frame = new JFrame("PDF Viewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
JPanel panel = new JPanel(new BorderLayout());
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(scrollPane, BorderLayout.CENTER);
JButton openButton = new JButton("Open PDF");
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));
int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
SwingController controller = new SwingController();
SwingViewBuilder factory = new SwingViewBuilder(controller);
JPanel viewer = factory.buildViewerPanel();
controller.getDocumentViewController().setAnnotationCallback(new org.icepdf.ri.common.MyAnnotationCallback(controller.getDocumentViewController()));
controller.getDocumentViewController().setCursorToolManager(new org.icepdf.ri.common.MyCursorToolManager(controller));
controller.getDocumentViewController().setPageViewDecorator(new org.icepdf.ri.common.MyPageViewDecorator(controller.getDocumentViewController()));
controller.getDocumentViewController().setDocumentLoader(new org.icepdf.ri.common.MyDocumentLoader(controller.getDocumentViewController()));
controller.setComponentKeyBinding(new ComponentKeyBinding(controller));
controller.openDocument(file.getAbsolutePath());
panel.removeAll();
panel.add(viewer, BorderLayout.CENTER);
panel.revalidate();
} catch (Exception ex) {
JOptionPane.showMessageDialog(frame, "Error opening PDF: " + ex.getMessage());
}
}
}
});
panel.add(openButton, BorderLayout.NORTH);
frame.setContentPane(panel);
frame.setVisible(true);
}
}
Apache PDFBox
库打开PDFimport java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.icepdf.ri.common.ComponentKeyBinding;
import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;
public class PDFViewer {
public static void main(String[] args) {
JFrame frame = new JFrame("PDF Viewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
JPanel panel = new JPanel();
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(scrollPane);
JButton openButton = new JButton("Open PDF");
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));
int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
PDDocument document = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(document);
SwingController controller = new SwingController();
SwingViewBuilder factory = new SwingViewBuilder(controller);
JPanel viewer = factory.buildViewerPanel();
controller.getDocumentViewController().setAnnotationCallback(new org.icepdf.ri.common.MyAnnotationCallback(controller.getDocumentViewController()));
controller.getDocumentViewController().setCursorToolManager(new org.icepdf.ri.common.MyCursorToolManager(controller));
controller.getDocumentViewController().setPageViewDecorator(new org.icepdf.ri.common.MyPageViewDecorator(controller.getDocumentViewController()));
controller.getDocumentViewController().setDocumentLoader(new org.icepdf.ri.common.MyDocumentLoader(controller.getDocumentViewController()));
controller.setComponentKeyBinding(new ComponentKeyBinding(controller));
controller.openDocument(file.getAbsolutePath());
panel.removeAll();
panel.add(viewer);
panel.revalidate();
} catch (IOException ex) {
JOptionPane.showMessageDialog(frame, "Error opening PDF: " + ex.getMessage());
}
}
}
});
panel.add(openButton);
frame.setContentPane(panel);
frame.setVisible(true);
}
}
这两种方法都可以在Java桌面应用程序中打开PDF文件。使用javax.swing
库可以直接在Java应用程序中打开PDF文件,而使用Apache PDFBox
库则需要先下载并安装该库。
领取专属 10元无门槛券
手把手带您无忧上云