java 本地打印机_JAVA实现连接本地打印机并打印文件的实现代码

实现代码一

import javax.print.*;

import javax.print.attribute.DocAttributeSet;

import javax.print.attribute.HashDocAttributeSet;

import javax.print.attribute.HashPrintRequestAttributeSet;

import javax.print.attribute.PrintRequestAttributeSet;

import java.io.File;

import java.io.FileInputStream;

public class PrintDemo1 {

public void printPdf(String fileName) {

//构造一个文件选择器,默认为当前目录

File file = new File(fileName);//获取选择的文件

//构建打印请求属性集

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 (service != null) {

try {

DocPrintJob job = service.createPrintJob(); // 创建打印作业

FileInputStream fis; // 构造待打印的文件流

fis = new FileInputStream(file);

DocAttributeSet das = new HashDocAttributeSet();

Doc doc = new SimpleDoc(fis, flavor, das);

job.print(doc, pras);

} catch (Exception e) {

e.printStackTrace();

}

}

}

public static void main(String args[]) {

PrintDemo1 pic = new PrintDemo1();

pic.printPdf("F:\\java资源2\\Docker视频教程\\赠送3-从Docker到Kubernetes之技术实战\\01.为什么你需要学习Docker\\01.pdf");

}

}

代码二

package com.iba.cxx.adm.controller;

import javax.print.*;

import javax.print.attribute.DocAttributeSet;

import javax.print.attribute.HashDocAttributeSet;

import javax.print.attribute.HashPrintRequestAttributeSet;

import javax.swing.*;

import java.io.File;

import java.io.FileInputStream;

/**

* Created by Administrator on 2017/9/8 0008.

*/

public class TestController {

public static void main(String[] args) {

JFileChooser fileChooser = new JFileChooser(); //创建打印作业

int state = fileChooser.showOpenDialog(null);

if(state == fileChooser.APPROVE_OPTION){

// File file = new File("D:/haha.txt"); //获取选择的文件

File file = fileChooser.getSelectedFile();//获取选择的文件

//构建打印请求属性集

HashPrintRequestAttributeSet 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(service != null){

try {

DocPrintJob job = service.createPrintJob(); //创建打印作业

FileInputStream fis = new FileInputStream(file); //构造待打印的文件流

DocAttributeSet das = new HashDocAttributeSet();

Doc doc = new SimpleDoc(fis, flavor, das);

job.print(doc, pras);

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值