一、java原生方式打印PDF文件

正反面都打印,还未研究出只打印单面的方法,待解决

public static void printFile(String path) throws Exception {
File file = new File(path);
File[] fies=file.listFiles();
for(File f:fies){
System.out.println("file "+f.getName());
String fileExt=f.getName().substring(f.getName().indexOf(".")+1,f.getName().length());
if("pdf".equalsIgnoreCase(fileExt)){
String filepath=path+File.separator+f.getName();
File pdfFile=new File(filepath);
//构建打印请求属性集
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//设置打印格式,因为未确定文件类型,这里选择AUTOSENSE
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//查找所有的可用打印服务
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
//定位默认的打印服务
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//显示打印对话框
//PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
if(defaultService!=null){
DocPrintJob job = defaultService.createPrintJob(); //创建打印作业
FileInputStream fis = new FileInputStream(pdfFile); //构造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das); //建立打印文件格式
job.print(doc, pras); //进行文件的打印
}
f.delete();
}
}
} public static void main(String[] args) {
//System.out.println("Value:"+test());
//打印pdf的一个方法,首先安装下PDFCreator软件 try {
printFile("D:"+File.separator);
} catch (Exception e) {
System.out.println("打印文件异常:"+e.getMessage());
e.printStackTrace();
} }

二、使用pdfBox方式此问题解决

1.maven依赖

        <dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.3</version>
</dependency>

2.代码(有打印预览)

import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.PageRanges;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;
import org.apache.pdfbox.printing.PDFPrintable; public final class PrintUtils
{
public static void main(String[] args) {
String fileURL = "http";
String fileName = ".pdf文件";
downloadFile(fileURL,fileName);
printWithDialog(getFilePath(fileName));
} public static void printWithDialog(String filePath) {
try {
PDDocument document = PDDocument.load(new File(filePath));
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(new PDFPageable(document));
if (job.printDialog()){
job.print();
}
} catch (IOException e) {
e.printStackTrace();
} catch (PrinterException e) {
e.printStackTrace();
}
} public static String getFilePath(String fileName){
File path = new File(System.getProperty("user.dir").concat("/downFile"));
if (!path.exists() && !path.isDirectory()) {
path.mkdir();
}
String filePath = path + "/" + fileName;
return filePath;
} public static boolean downloadFile(String fileURL,String fileName) {
try {
URL url = new URL(fileURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
DataInputStream in = new DataInputStream(connection.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream(getFilePath(fileName)));
byte[] buffer = new byte[];
int count = ;
while ((count = in.read(buffer)) > ) {
out.write(buffer, , count);
}
out.close();
in.close();
return true;
} catch (Exception e) {
return false;
}
} }

java从远程服务器获取PDF文件并后台打印(使用pdfFox)的更多相关文章

  1. SNF开发平台WinForm之十三-单独从服务器上获取PDF文件进行显示-SNF快速开发平台3.3-Spring.Net.Framework

    1运行效果: 2开发实现: 如果需要单独显示PDF文件时用下面代码去实现,指定url地址. 地址: . 获取附件管理的实体对象: List<KeyValuePair<string, obj ...

  2. 使用nodejs和Java访问远程服务器的服务

    既然这篇文章用的是nodejs和Java访问远程服务器的服务,那么咱们先用另一门编程语言,SAP的ABAP(我日常工作使用得最多的编程语言)来开发一个服务吧. 这是我用ABAP编程语言实现服务的类:Z ...

  3. dotnet获取PDF文件的页数

    #region 获取PDF文件的页数 private int BytesLastIndexOf(Byte[] buffer, int length, string Search) { if (buff ...

  4. xshell终端向远程服务器上传文件方法

    centos-7下在本地终端里向远程服务器上传文件,在命令行中执行的软件. 安装命令如下: 在终端里输入如下命令: 会弹出如下窗口 选择你要上传的文件即可上传成功.

  5. 在java程序当中怎么获取一个文件的路径

    在java程序当中怎么获取一个文件的路径? * 当这个文件在类路径下的时候(在src/bin目录下的时候): String absolutePath = Thread.currentThread(). ...

  6. 利用SSH在本机和远程服务器之间传输文件或文件夹

    1.从远程服务器上下载文件到本机 scp <服务器用户名>@<服务器地址>:<服务器中要下载的文件路径> <下载到本机的绝对路径> 2.从本机上传本地文 ...

  7. java ssh远程服务器并执行多条shell命令

    java ssh远程服务器并执行多条命令 import java.io.BufferedReader; import java.io.IOException; import java.io.Input ...

  8. 文档控件NTKO OFFICE 详细使用说明之预览PDF文件(禁止打印、下载、另存为、防抓包下载)

    1.在线预览PDF文件(禁止打印.下载.复制.另存为) (1) 运行环境 ① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox) ② IE工具栏-Internet 选项:将ww ...

  9. git服务器创建,冲突解决,远程仓库获取指定文件

    1.git服务器创建 在公司多人协作开发的情况下,不能简单地使用github,因为github是互联网公开的,这种情况公司的代码的保密性就会丧失了.这种情况下,需要创建git服务器. 登录服务器,使用 ...

随机推荐

  1. Git使用列表(四)

    最近,由于自己的一个项目,导致自己的关于自己的要使用Git的很多的命令,突然发现自己的git的还有许多不知道的东西 不过,在这个工作的过程中,也发现自己的一些很大的缺陷,就是自己题目理解力有限,明明就 ...

  2. Java设计模式(13)——结构型模式之桥梁模式(Bridge)

    一.概述 概念 将抽象与实现脱耦,使得抽象和实现可以独立运行 UML图 角色: 角色关系 二.实践 按照上面的角色建立相应的类 抽象化角色 /** * 抽象化角色 * * @author Admini ...

  3. 全国Uber优步司机奖励政策 (1月4日-1月10日)

    本周已经公开奖励整的城市有:北 京.成 都.重 庆.上 海.深 圳.长 沙.佛 山.广 州.苏 州.杭 州.南 京.宁 波.青 岛.天 津.西 安.武 汉.厦 门,可按CTRL+F,搜城市名快速查找. ...

  4. 【LG3527】[POI2011]MET-Meteors

    [LG3527][POI2011]MET-Meteors 题面 洛谷 题解 整体二分. 每次二分\(mid\),如果到时间\(mid\)以收集过\(P_i\)就存入子序列\(L\),否则存入子序列\( ...

  5. Python:TypeError: 'range' object doesn't support item deletion

    报错代码: dataIndex = range(m) del (dataIndex[randIndex]) 报错信息: 错误原因: python3 range返回的是range对象,不是数组对象 解决 ...

  6. LimitedConcurrencyLevelTaskScheduler

    //-------------------------------------------------------------------------- // // Copyright (c) Mic ...

  7. apache和IIS共存,服务器对外统一使用80端口

    apache和IIS共用80端口为了PHP与ASP各自的执行效率,要在服务器上安装iis与Apache,但是无法同时使用80端口,否则其中必定有一个启动不了.让它们共存的并且访问网站不需要加端口号,解 ...

  8. `Facebook.Unity.Settings' has already been imported error solution

    after import facebook sdk to unity, i get the '`Facebook.Unity.Settings' has already been imported' ...

  9. 打造移动应用与游戏安全防线,腾讯WeTest安全服务全线升级

    当移动互联网渗透到千家万户,与工业控制.智慧交通.实时社交.休闲娱乐紧密结合时,应用安全就变得尤为重要. 尤其在网络强相关的APP流行年代,当APP应用客户端上传与获取信息,大多通过接口在服务器双向通 ...

  10. nginx支持php配置

    location / { root /wwwroot/phptest; index index.html index.htm index.php; } location ~ \.(php|php5)$ ...