***使用pdfbox打印pdf***

本文介绍如何使用Java代码实现PDF文档的打印,包括直接打印和自定义纸张规格的打印方法。通过加载PDF文档到PDDocument对象,设置打印服务和打印参数,可以轻松完成打印任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接贴代码

	public static void printPdf() throws Exception {
		//获取pdf文档流
		InputStream in = new FileInputStream("d:\\pdfTest.pdf");
		//将pdf加载为PDDocument对象
		PDDocument document = PDDocument.load(in); 
		
		// 加载成打印文件 Scaling.ACTUAL_SIZE 为指定打印pdf的实际大小
		PDFPrintable printable = new PDFPrintable(document,Scaling.ACTUAL_SIZE);
		
		//获取默认的打印机
		PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
		
		//创建PrinterJob
		PrinterJob job = PrinterJob.getPrinterJob();
		
		//将PrinterJob指定打印机
		job.setPrintService(printService);
		
		//set 打印文件的对象
		job.setPrintable(printable);
		
		//设置打印份数
		job.setCopies(1);
		
		job.print();
	}

自定义纸张规格写法

public static void printPdf(double width, double height) throws Exception {
	InputStream in = new FileInputStream("d:\\de.pdf");
	PDDocument document = PDDocument.load(in);
	
	//获取默认的打印机
	PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
	
	// 加载成打印文件
	PDFPrintable printable = new PDFPrintable(document,Scaling.SCALE_TO_FIT);
	
	Book book = new Book();
	
	Paper paper = new Paper();
	paper.setSize(width, height);
	
	// 自定义页面设置
	PageFormat pageFormat = new PageFormat();
	
	// 设置打印页面横纵向
	//pageFormat.setOrientation(PageFormat.PORTRAIT);
	
	pageFormat.setPaper(paper);
	
	book.append(printable, pageFormat);
	
	PrinterJob job = PrinterJob.getPrinterJob();
	
	//设置带打印格式的打印文件
	job.setPageable(book);
	
	job.setPrintService(printService);
	
	job.setPrintable(printable);
	job.print();
}

本人小白一个,有不足之处欢迎指正

### Java 使用 PDFBox 打印 PDF 文件并实现自动翻页 为了使用 JavaPDFBox 库来打印 PDF 文档,并确保能够正确处理翻页问题,可以按照如下方式编写代码: ```java import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.printing.PDFPageable; import javax.print.DocFlavor; import javax.print.PrintService; import javax.print.PrintServiceLookup; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.standard.MediaSizeName; import javax.print.attribute.standard.OrientationRequested; import java.awt.print.PageFormat; import java.awt.print.PrinterException; import java.awt.print.PrinterJob; public class PrintPdfWithFlip { public static void main(String[] args) throws Exception { String filePath = "example.pdf"; try (PDDocument document = PDDocument.load(new File(filePath))) { PrinterJob job = PrinterJob.getPrinterJob(); // 设置页面描述 PageFormat pageFormat = new PageFormat(); PDFPageable pageable = new PDFPageable(document); job.setPageable(pageable); // 查找默认打印机服务 DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE; PrintService service = PrintServiceLookup.lookupDefaultPrintService(); if (service != null) { job.setPrintService(service); // 创建打印请求属性集 PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet(); // 添加双面打印选项 attributes.add(MediaSizeName.ISO_A4); attributes.add(OrientationRequested.PORTRAIT); // 自动翻页设置 attributes.add(javax.print.attribute.standard.Sides.DUPLEX); // 提交作业进行打印 job.print(attributes); } } catch (PrinterException e) { System.err.println("Error printing the file."); e.printStackTrace(); } } } ``` 这段程序展示了如何通过 `PDFBox` 加载 PDF 文件以及配置 `PrinterJob` 来执行打印操作[^1]。 对于翻页功能,在上述代码中的 `attributes.add()` 方法里指定了 `Sides.DUPLEX` 参数用于启用双面打印模式。这会指示打印机在支持的情况下采用双面纸张路径完成连续两页内容的同时输出,从而实现了所谓的“自动翻页”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值