当前位置:网站首页 > 编程语言 > 正文

pdfView为什么打印不了(pdf为什么没办法打印)



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的三种方法_java

然后在主程序中调用:

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

fileName是你生成的PDF文件路径

效果图:

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

Java预览及打印PDF的三种方法_java_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的三种方法_swing_04

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



技术成就梦想

关注我们一起成长

生活不知眼前的苟且,

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

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

到此这篇pdfView为什么打印不了(pdf为什么没办法打印)的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • 好玩的代码游戏火柴人(可以选秘籍的火柴人游戏)2025-04-28 15:18:10
  • 104协议总召(101 104协议)2025-04-28 15:18:10
  • non sbatte non batte翻译(non-statement翻译)2025-04-28 15:18:10
  • .pem文件(.pem文件如何导入)2025-04-28 15:18:10
  • 程序卸载快捷键(程序的卸载)2025-04-28 15:18:10
  • tpami投稿要求(tpami审稿)2025-04-28 15:18:10
  • 104协议报文解析(104soe报文)2025-04-28 15:18:10
  • 来自远方作品推荐(来自远方推文)2025-04-28 15:18:10
  • 共享打印机0x0000709补丁(0x00000709共享打印机)2025-04-28 15:18:10
  • 删除虚拟环境(删除虚拟环境后包会一起删除吗)2025-04-28 15:18:10
  • 全屏图片