三种Java打印PDF文档的实例代码

所属分类: 软件编程 / java 阅读数: 103
收藏 0 赞 0 分享

以下内容归纳了通过Java程序打印PDF文档时的3种情形。即:

1 静默打印

2 显示打印对话框打印

3 打印PDF时自定义纸张大小

使用工具:Spire.PDF for Java

Jar文件获取及导入:

方法1:下载jar包。下载后,解压文件,并将lib文件夹下的Spire.Pdf.jar导入java程序。

方法2:可通过maven库导入。参考导入方法。

Java代码示例

【示例1】静默打印

即通过使用默认打印机直接打印PDF文档。打印时,我们可以设置打印份数,设置纸张打印页边距等。

import com.spire.pdf.*;
import java.awt.print.*; 
public class Print { 
 public static void main(String[] args) { 
 //加载文档 
 PdfDocument pdf = new PdfDocument(); 
 pdf.loadFromFile("Sample.pdf"); 
 PrinterJob loPrinterJob = PrinterJob.getPrinterJob(); 
 PageFormat loPageFormat = loPrinterJob.defaultPage(); 
 Paper loPaper = loPageFormat.getPaper(); 
 
 //删除默认页边距 
 loPaper.setImageableArea(0,0,loPageFormat.getWidth(),loPageFormat.getHeight()); 
 
 //设置打印份数 
 loPrinterJob.setCopies(2); 
 loPageFormat.setPaper(loPaper); 
 loPrinterJob.setPrintable(pdf,loPageFormat); 
 try { 
 loPrinterJob.print(); 
 } catch (PrinterException e) { 
 e.printStackTrace(); 
 } 
 }
 }

【示例2】显示打印对话框打印PDF文档

import com.spire.pdf.*;
import java.awt.print.*;
 
public class Print {
 public static void main(String[] args) {
 //加载文档
 PdfDocument pdf = new PdfDocument();
 pdf.loadFromFile("Sample.pdf");
 
 PrinterJob loPrinterJob = PrinterJob.getPrinterJob();
 PageFormat loPageFormat = loPrinterJob.defaultPage();
 Paper loPaper = loPageFormat.getPaper();
 
 //删除默认页边距
 loPaper.setImageableArea(0,0,loPageFormat.getWidth(),loPageFormat.getHeight());
 
 loPageFormat.setPaper(loPaper);
 loPrinterJob.setPrintable(pdf,loPageFormat);
 
 //显示打印对话框
 if (loPrinterJob.printDialog()) {
 try {
 loPrinterJob.print();
 } catch (PrinterException e) {
 e.printStackTrace();
 }
 }
 }
}

【示例3】打印时自定义纸张大小

import com.spire.pdf.*;
import java.awt.print.*;
public class Print {
 public static void main(String[] args) {
 //加载文档
 PdfDocument pdf = new PdfDocument();
 pdf.loadFromFile("Sample.pdf");
 
 PrinterJob loPrinterJob = PrinterJob.getPrinterJob();
 PageFormat loPageFormat = loPrinterJob.defaultPage();
 
 //设置打印纸张大小
 Paper loPaper = loPageFormat.getPaper();
 loPaper.setSize(500,600);
 loPageFormat.setPaper(loPaper);
 loPrinterJob.setPrintable(pdf,loPageFormat);
 try {
 loPrinterJob.print();
 } catch (PrinterException e) {
 e.printStackTrace();
 } 
 }
}

如果需要更详细的打印设置,可以在PrinterJob下设置相应的参数,可从参考:https://www.programcreek.com/java-api-examples/java.awt.print.PrinterJob

(本文完)

更多精彩内容其他人还在看

Java实现一个简单的文件上传案例示例代码

这篇文章主要介绍了Java实现一个简单的文件上传案例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解在spring中使用JdbcTemplate操作数据库的几种方式

这篇文章主要介绍了详解在spring中使用JdbcTemplate操作数据库的几种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

java中的switch case语句使用详解

这篇文章主要介绍了java中的switch case语句使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

深入理解Java 线程池

这篇文章主要介绍了Java 线程池的相关资料,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解mybatis #{}和${}的区别、传参、基本语法

这篇文章主要介绍了mybatis #{}和${}的区别、传参、基本语法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

MyBatis 中 ${}和 #{}的正确使用方法(千万不要乱用)

这篇文章主要介绍了MyBatis 中 ${}和 #{}的正确使用方法,本文给大家提到了MyBatis 中 ${}和 #{}的区别,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Mybatis中的 ${} 和 #{}区别与用法

这篇文章主要介绍了Mybatis中的 ${} 和 #{}区别与用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Maven安装过程图文详解

这篇文章主要介绍了Maven安装过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

SpringBoot登录拦截配置详解(实测可用)

这篇文章主要介绍了SpringBoot登录拦截配置详解(实测可用),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

JAVA 内存溢出案例汇总

这篇文章主要介绍了JAVA 内存溢出案例的汇总,文中讲解非常细致,帮助各位工作学习时避免内存溢出,感兴趣的朋友可以了解下
收藏 0 赞 0 分享
查看更多