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);

}

}


Java预览及打印PDF的三种方法_swing

然后在主程序中调用:

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

fileName是你生成的PDF文件路径


效果图:

Java预览及打印PDF的三种方法_swing_02

Java预览及打印PDF的三种方法_awt_03

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




第二种:


使用系统默认程序(浏览器)打开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("\\", "/")+"/"+fileName.substring(2);

String url="file:///D:/cic/"+fileName.substring(2);

java.net.URI uri = java.net.URI.create(url);

// 获取当前系统桌面扩展

java.awt.Desktop dp = java.awt.Desktop.getDesktop();

// 判断系统桌面是否支持要执行的功能

if (dp.isSupported(java.awt.Desktop.Action.BROWSE)) {

// 获取系统默认浏览器打开链接

dp.browse(uri);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

主程序调用工具类:

 googlepdf.opengoogle(getLujing(),fileName);

 public String getLujing(){

 //获取类加载的根路径

//

    File file3 = new File(this.getClass().getResource("/").getPath());

//

    String fileurl=file3.toString();

//

     String url=fileurl.substring(0,fileurl.length()-14);

//

      return url.replace("\\", "/");

     

        File file3 = new File(".");

        String fileurl = "";

try {

fileurl = file3.getCanonicalPath().toString();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

        return fileurl;

  }

}



效果图因不同系统PDF默认程序不同,这里就不展示了



第三种:Java指定浏览器预览及打印PDF





工具类与第二种方式一样,将里面的默认程序打开的代码换成

//浏览器位置

String google=jC:\\Users\\wangdong-surface\\Desktop\\cic\\Google\\Chrome\\Application\\chrome.exe";

Map map = System.getenv();

for (Iterator itr = map.keySet().iterator(); itr.hasNext();) {

String value = (String) map.get((String) itr.next());

if (value.contains("firefox.exe")) {

google = value;

break;

}

}

Runtime.getRuntime().exec(new String[] { google, url });

我将Google浏览器安装到了我的项目路径下,这样,不同的用户使用该客户端都不需要重新安装我指定的浏览器。


效果图:

Java预览及打印PDF的三种方法_icepdf_04


以上就是三种Java PDF打印方法,希望能帮到需要的你。



技术成就梦想

关注我们一起成长


生活不知眼前的苟且,

还有课本里的诗和到不了的远方!


本文分享自微信公众号 - 释然IT杂谈