惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
Y
Y Combinator Blog
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Martin Fowler
Martin Fowler
博客园_首页
大猫的无限游戏
大猫的无限游戏
美团技术团队
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MyScale Blog
MyScale Blog
小众软件
小众软件
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
N
Netflix TechBlog - Medium
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
博客园 - 【当耐特】
J
Java Code Geeks
F
Fortinet All Blogs
宝玉的分享
宝玉的分享
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美

祈雨的笔记

安全多方计算MPC spark原理解析 kueue执行源码分析 spark on k8s执行源码分析 spark-operator源码解析 系统压测遇到的缓存击穿问题 我的世界PC与安卓联机 蚂蚁金服流量投放平台的AIG改造 G1大对象致Old区占用率高 日志打印导致接口响应率下跌分析 Groovy加载类导致OOM分析 ERROR日志打印导致CPU满载 记OceanBase死锁超时 应用发版期间服务响应超时 Ark Serverless初探 系统优化复盘一二三 The user specified as a definer does not exist Kong网关初探 API网关选型调研 CPU火焰图常用工具 配置中心选型调研 root操作Nginx导致用户组错误 基于Proxifier使用代理 FastJSON字段智能匹配踩坑 Nacos初探 记一次Nginx服务器CPU满荷载故障 基于券系统分库分表的思考 limit不参与SQL成本计算致索引失效 Linux常用性能监控命令 golang低版本http2偶现400
java连接打印机打印PDF
祈雨的笔记 · 2018-03-02 · via 祈雨的笔记

核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class PrintService {

private static final Logger logger = LoggerFactory.getLogger(PrintService.class);









public void print(InputStream in,boolean isDuplex,int copies,boolean isPortrait) throws Exception {
if(in == null) {
throw new TXException("文件流为空");
}
if(copies <= 0) {
throw new TXException("打印份数不应小于0");
}

logger.info("打印文件: 是否双页打印: "+isDuplex+",份数: "+copies+"是否竖打"+isPortrait);

try(PDDocument document = PDDocument.load(in);) {

Book book =new PDFPageable(document);
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(book);

HashPrintRequestAttributeSet pars = new HashPrintRequestAttributeSet();
pars.add(MediaName.ISO_A4_WHITE);


if(isDuplex) {
if(isPortrait){

pars.add(Sides.DUPLEX);
} else {

pars.add(Sides.TWO_SIDED_LONG_EDGE);
}
}


if(isPortrait){
pars.add(OrientationRequested.PORTRAIT);
} else {
pars.add(OrientationRequested.LANDSCAPE);
}





for(int i=0;i<copies;i++) {
job.print(pars);
}
} catch (Exception e) {
throw e;
}
}

public static void main(String[] args) throws Exception {
InputStream in = new FileInputStream("D:/test.pdf");
new PrintService().print(in, false,1,false);
in.close();
}
}