当前位置:评测网 > 电脑 > 正文

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

导语:JAVA实现PDF打印的方式有很多,话不多说,我这里将给出J三种打开PDF的方法。第一种:Java Swing自带的组件预览、打印PDF首先需要一个工具类:package com.beiqisoft.cic;import ja

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文件路径

效果图:


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

第二种:

使用系统默认程序(浏览器)打开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:Userswangdong-surfaceDesktopcicGoogleChromeApplicationchrome.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打印方法,希望能帮到需要的你。

免责申明:以上内容属作者个人观点,版权归原作者所有,不代表评测网立场!登载此文只为提供信息参考,并不用于任何商业目的。如有侵权或内容不符,请联系我们处理,谢谢合作!
当前文章地址:https://www.pcapqz.com/diannao/142962.html 感谢你把文章分享给有需要的朋友!
上一篇:线程间通信 java中使用wait和notify进行单线程之间的通信 下一篇: jvm调优 JVM调优:基本概念