亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

JAVA實(shí)現(xiàn)PDF打印的方式有很多,話不多說,我這里將給出J三種打開PDF的方法。

第一種:Java Swing自帶的組件預(yù)覽、打印PDF

首先需要一個(gè)工具類:

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

}

}

然后在主程序中調(diào)用:

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

fileName是你生成的PDF文件路徑

效果圖:

Java預(yù)覽及打印PDF的三種方法
Java預(yù)覽及打印PDF的三種方法

弊端:不能修改右邊距和下邊距參數(shù),對于打印位置要求精準(zhǔn)的項(xiàng)目,不適用。

第二種:使用系統(tǒng)默認(rèn)程序(瀏覽器)打開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 {

// 創(chuàng)建一個(gè)URI實(shí)例

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

// 獲取當(dāng)前系統(tǒng)桌面擴(kuò)展

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

// 判斷系統(tǒng)桌面是否支持要執(zhí)行的功能

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

// 獲取系統(tǒng)默認(rèn)瀏覽器打開鏈接

dp.browse(uri);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

主程序調(diào)用工具類:

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;

}

}

效果圖因不同系統(tǒng)PDF默認(rèn)程序不同,這里就不展示了

第三種:Java指定瀏覽器預(yù)覽及打印PDF

工具類與第二種方式一樣,將里面的默認(rèn)程序打開的代碼換成

//瀏覽器位置

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瀏覽器安裝到了我的項(xiàng)目路徑下,這樣,不同的用戶使用該客戶端都不需要重新安裝我指定的瀏覽器。

效果圖:

Java預(yù)覽及打印PDF的三種方法

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

分享到:
標(biāo)簽:打印 Java PDF
用戶無頭像

網(wǎng)友整理

注冊時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評(píng)定2018-06-03

通用課目體育訓(xùn)練成績評(píng)定