list = new ArrayList<>();
HashPrintRequestAttributeSet requestAttributeSet = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//查找所有的可用的打印服务
PrintService[] printService = PrintServiceLookup.lookupPrintServices(flavor, requestAttributeSet);
if (printService == null || printService.length == 0) {
log.info("打印获取失败,未找到可用打印机,请检查。");
if (printService != null) {
for (PrintService print : printService) {
list.add(print.getName());
return list;
PFD文件打印
<!--打印-->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.6</version>
</dependency>
需要获得打印机的名称
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPrintable;
import org.apache.pdfbox.printing.Scaling;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.standard.Sides;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
public class DYController {
public static void main(String[] args) throws Exception {
String pdfFile = "E:\\a.pdf";//文件路径
File file = new File(pdfFile);
String printerName = "HP MFP M436 PCL6";//打印机名包含字串
PDFprint(file,printerName);
public static void PDFprint(File file , String printerName) throws Exception {
PDDocument document = null;
try {
document = PDDocument.load(file);
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setJobName(file.getName());
if (printerName != null) {
// 查找并设置打印机
//获得本台电脑连接的所有打印机
PrintService[] printServices = PrinterJob.lookupPrintServices();
if(printServices == null || printServices.length == 0) {
System.out.print("打印失败,未找到可用打印机,请检查。");
return ;
PrintService printService = null;
//匹配指定打印机
for (int i = 0;i < printServices.length; i++) {
System.out.println(printServices[i].getName());
if (printServices[i].getName().contains(printerName)) {
printService = printServices[i];
break;
if(printService!=null){
printJob.setPrintService(printService);
}else{
System.out.print("打印失败,未找到名称为" + printerName + "的打印机,请检查。");
return ;
//设置纸张及缩放
PDFPrintable pdfPrintable = new PDFPrintable(document, Scaling.ACTUAL_SIZE);
//设置多页打印
Book book = new Book();
PageFormat pageFormat = new PageFormat();
//设置打印方向
pageFormat.setOrientation(PageFormat.PORTRAIT);//纵向
pageFormat.setPaper(getPaper());//设置纸张
book.append(pdfPrintable, pageFormat, document.getNumberOfPages());
printJob.setPageable(book);
printJob.setCopies(1);//设置打印份数
//添加打印属性
HashPrintRequestAttributeSet pars = new HashPrintRequestAttributeSet();
pars.add(Sides.DUPLEX); //设置单双页
printJob.print(pars);
}finally {
if (document != null) {
try {
document.close();
} catch (IOException e) {
e.printStackTrace();
public static Paper getPaper() {
Paper paper = new Paper();
// 默认为A4纸张,对应像素宽和高分别为 595, 842
int width = 595;
int height = 842;
// 设置边距,单位是像素,10mm边距,对应 28px
int marginLeft = 10;
int marginRight = 0;
int marginTop = 10;
int marginBottom = 0;
paper.setSize(width, height);
// 下面一行代码,解决了打印内容为空的问题
paper.setImageableArea(marginLeft, marginRight, width - (marginLeft + marginRight), height - (marginTop + marginBottom));
return paper;
excel转换成pdf
生成excel请看下面的链接:
java导出Excel_Mr_Jin.的博客-CSDN博客_java导出excel方法
https://blog.csdn.net/Java_Mr_Jin/article/details/124076055?spm=1001.2014.3001.5501
当然我们的excel要想打印可以转换成pdf的形式,再用上面的进行打印:
关于word转换pdf需要用到jaboc,所以在这需要先下载jaboc.jar包,链接及提取码如下:
链接: 链接: https://pan.baidu.com/s/17nLuDAYC33Yu8KvwS2J2gA
提取码: qwer
将jar包解压后
1.将Jacob.jar包复制到程序的jdk的lib文件夹下
2.将dll文件复制到jdk的bin目录下
然后在idea中引入jacob.jar包
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>jacob</artifactId>
<version>1.18</version>
</dependency>
在java中进行实现excel转换成pdf:
package com.sangeng.controller;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import java.io.File;
public class DYPdfController {
//word转化pdf,传入转换前的文件路径(例:"E:\\a.docx")和转换后的文件路径(例:"E:\\a.pdf")
public static void main(String[] args) {
String sFilePath = "D:\\java-demo\\zzzzz\\qqq.doc";//文件路径
String toFilePath = "D:\\java-demo\\zzzzz\\qqq.pdf";
wordToPDF(sFilePath,toFilePath);
public static void wordToPDF(String sFilePath,String toFilePath) {
System.out.println("启动 Word...");
long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
doc = Dispatch.call(docs, "Open", sFilePath).toDispatch();
System.out.println("打开文档:" + sFilePath);
System.out.println("转换文档到 PDF:" + toFilePath);
File tofile = new File(toFilePath);
if (tofile.exists()) {
tofile.delete();
Dispatch.call(doc, "SaveAs", toFilePath,17);//17是pdf格式 // FileName
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
Dispatch.call(doc, "Close", false);
System.out.println("关闭文档");
if (app != null)
app.invoke("Quit", new Variant[]{});
// 如果没有这句话,winword.exe进程将不会关闭
ComThread.Release();
当然转换打印完成后。记得删除生成的pdf文件:
File file=new File(toFilePath);
if(file.exists()&&file.isFile())
file.delete();
直接excel进行打印
缺点:就是不能更改打印参数
package com.sangeng.controller;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class WordController {
public static void main(String[] args) {
String filePath = "E:\\a.docx";//文件路径
String printerName = "HP MFP M436 PCL6";//打印机名包含字串
printWord(filePath,printerName);
public static void printWord(String filePath, String printerName){
// 初始化线程
ComThread.InitSTA();
ActiveXComponent word = new ActiveXComponent("Word.Application");
//设置打印机名称
word.setProperty("ActivePrinter", new Variant(printerName));
// 这里Visible是控制文档打开后是可见还是不可见,若是静默打印,那么第三个参数就设为false就好了
Dispatch.put(word, "Visible", new Variant(false));
// 获取文档属性
Dispatch document = word.getProperty("Documents").toDispatch();
// 打开激活文挡
Dispatch doc=Dispatch.call(document, "Open", filePath).toDispatch();
//Dispatch doc = Dispatch.invoke(document, "Open", Dispatch.Method,
// new Object[] { filePath }, new int[1]).toDispatch();
Dispatch.callN(doc, "PrintOut");
System.out.println("打印成功!");
}catch (Exception e){
e.printStackTrace();
System.out.println("打印失败");
}finally {
try {
if (doc != null) {
Dispatch.call(doc, "Close", new Variant(0));//word文档关闭
} catch (Exception e2) {
e2.printStackTrace();
word.invoke("Quit", new Variant[0]);
//释放资源
ComThread.Release();
ComThread.quitMainSTA();
导入C盘失效
文件不能直接导入c盘,因为权限问题。所以先创建一个文件夹,再导入即可
File dir=new File("C:\\nmgbzj");
File file2=new File(dir.getPath()+"/");
if(!dir.exists()){
dir.mkdir();
file2.createNewFile();
}else{
if(file2.exists()){
file2.delete();
file2.createNewFile();
}else{
file2.createNewFile();
如果Java AWT Printing未注册到运行打印应用程序的Windows / Active Directory用户,则它将无法通过路径找到该打印机.您必须通过Windows“设备和打印机”将打印机路径注册为该用户的打印机,以使其可见.然后,您必须运行lookupPrintServices以查看可用的打印机列表,并按列出的确切名称String检索正确的PrintService./*** Ret...
import javax.imageio.ImageIO;
import javax.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
打印PDF文档是我们日常生活中的一个关键功能。您可以通过打印文档来制作 PDF 文件的有形副本,以便离线阅读和使用。这对于需要手动填写或签署的文书工作(例如合同、报告和表格)特别有用。借助 Spire.PDF,您可以在 Java 应用程序中以编程方式轻松打印 PDF 文件。Spire.PDF for Java 是一个 PDF API,使 Java 应用程序能够在不使用 Adobe Acrobat 的情况下读取、写入、保存和打印 PDF 文档。
1. 加载模板
JasperPrint jasperPrint = JasperFillManager.fillReport("WebRoot/report/test.jasper", new HashMap(),new JREmptyDataSource());
//false/true 表示在打印的时候是否显示打印机设置
JasperPrintManager.printReport(jasp...
i require to send a pdf document to print on the server side of a web app, the printer fully supports pdf printing etc, it is networked as well to the server. The pdf is also stored on the server.what...
//设置打印属性 构造一个新的空打印请求属性集。PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();pras.add(new Copies(3));//打印份数,3份//设置打印数据的格式DocFlavor.BYTE_ARRAY.PNG MIME 类型 = "image/png",打印数据表示形式类名 = "...
一、Java的打印简介
在我们的实际工作中,经常需要实现打印功能。但由于历史原因,Java 提供的打印功能一直都比较弱。实际上最初的 jdk 根本不支持打印,直到 jdk1.1 才引入了很轻量的打印支持。实际上,SUN 公司也一直致力于 Java 打印功能的完善,而 Java2 平台则终于有了一个健壮的打印模式的开端, jdk1.4 则提供了一套完整的"Java 打印服务 API" (Java ...
通过对网络的更多研究,我解决了我的问题.我在这里给那些可能需要它的人;我从这个网站得出了解决方案:注意:您需要在项目中安装PdfRenderer .jar库以运行网站中给出的代码:最初在PrintPdf.java中的代码不提供我的解决方案,但作者在注释部分添加了一个方法来设置不同的打印机在运行时打印.方法是:/*** Sets the printer service to be used for ...
Java读取打印机列表
//查找所有打印服务
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
System.out.println("*****从配置文件读取的打印机型号*****");
System.out.println(services.length);
System.out.println("**********");
Java读取默认打印机
PrintService
无论采用哪种语言来开发应用系统,打印功能总是少不了的。为了节省Java程序开发人员的工作量,在Java语言中专门提供了一个PrinterJob类来帮助开发人员实现打印控制的功能。本文将给大家介绍一下如何使用PrintJob类来实现打印作业。一、PrinterJob类的功能。在Java语言中,实现打印控制的类也很多。但是笔者比较倾向于使用这个PrintJob类。它是在Java语言中控制打印作业的主要...
简单介绍运行环境:语言:Java工具:eclipse系统:Windows7(打印设备暂时没有,所以只能提供预览图)最近,项目需要为商城做一个购物小票的打印功能,日常我们去超市买东西,结账的时候收银员都会打印一个小票,一般的商城也都需要这样的一个小功能,本文给出的 demo 是在 58mm 的热敏打印机下的例子,如果是其他纸张类型的打印机,调整纸张宽度即可。package test;import j...
我正在尝试使用打印机TSC TTP-342E Pro工业热敏打印机打印标签。 但无法打印。 我使用相同的程序在激光打印机HP LaserJet M1530中进行打印,并且工作正常。 在这两种情况下,打印都是从pdf开始的。 该程序正在与打印机通信,但未打印任何内容。 我使用过pdfbox,是否应该使用bartenter之类的工具?在下面的程序中,程序将从某个位置读取pdf并与打印机通信以...
JAVA获取本机的所有打印机
// 获取本机打印机
PrintService[] pss = PrinterJob.lookupPrintServices();
PrintService ps = null;
for (int i = 0; i < pss.length; i++) {
System.out.println(ps...