Java实现PDF打印的方式有很多,话不多说,我这里将给出J三种打开PDF的方法。

第一种:Java Swing自带的组件预览、打印PDF

首先需要一个工具类:

package com.beiqisoft.cic;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;
public class PDFViewer {
public JFrame frame;
public String pdf;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PDFViewer window = new PDFViewer("c:/wd/b.pdf");
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public PDFViewer(String pdf) {
this.pdf = pdf;
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setSize(1100, 1000);
frame.setLocationRelativeTo(null);
frame.getContentPane().setLayout(null);
SwingController controller = new SwingController();
SwingViewBuilder factory = new SwingViewBuilder(controller);
JPanel viewerComponentPanel = factory.buildViewerPanel();
controller.getDocumentViewController().setAnnotationCallback(
new org.icepdf.ri.common.MyAnnotationCallback(
controller.getDocumentViewController()));
controller.openDocument(pdf);
frame.setContentPane(viewerComponentPanel);
}
}

然后在主程序中调用:

new PDFViewer(fileName).frame.setVisible(true);

fileName是你生成的PDF文件路径

效果图:




java 怎么调acrobat打印 java打印pdf文件_java pdf生成


java 怎么调acrobat打印 java打印pdf文件_java 怎么调acrobat打印_02


弊端:不能修改右边距和下边距参数,对于打印位置要求精准的项目,不适用。

第二种:使用系统默认程序(浏览器)打开PDF文件

工具类:

package com.beiqisoft.cic.util;
import java.io.File;
import java.util.Iterator;
import java.util.Map;
public class googlepdf {
public static void opengoogle(String jurl,String fileName) {
if (java.awt.Desktop.isDesktopSupported()) {
try {
// 创建一个URI实例
String url="file:///"+jurl.replace(