java 横向 打印出来_在双工模式下使用Java打印横向文档

本文介绍了在Java AWT中使用JasperReports进行横向打印时遇到的问题,主要涉及PCL5和PCL6打印驱动。在PCL5下,LONG_EDGE和SHORT_EDGE设置被忽略,导致页面旋转错误。作者提出了三种可能的解决方案:强制AWT发送横向打印请求,反转LONG_EDGE和SHORT_EDGE命令,或者切换到SWT打印API。

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

我有一个JasperReports报告要在双面打印机上以横向模式打印.在此我要支持PCL5和PCL6打印驱动程序.

在互联网上搜索,我发现了以下代码片段来完成这项工作:

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.print.Book;

import java.awt.print.PageFormat;

import java.awt.print.Printable;

import java.awt.print.PrinterException;

import java.awt.print.PrinterJob;

import javax.print.PrintService;

import javax.print.attribute.HashPrintRequestAttributeSet;

import javax.print.attribute.PrintRequestAttributeSet;

import javax.print.attribute.standard.OrientationRequested;

import javax.print.attribute.standard.Sides;

public class PrintingTest {

public static void main(String[] args) {

try {

//This are for configuration purpose

String orientation = "LANDSCAPE";

String duplexMode = "LONG_EDGE";

int pageOrientation = 0;

PrintRequestAttributeSet atr = new HashPrintRequestAttributeSet();

if ("Landscape".equals(orientation)) {

atr.add(OrientationRequested.LANDSCAPE);

pageOrientation = PageFormat.LANDSCAPE;

} else if ("Reverse_Landscape".equals(orientation)) {

atr.add(OrientationRequested.REVERSE_LANDSCAPE);

pageOrientation = PageFormat.REVERSE_LANDSCAPE;

} else {

atr.add(OrientationRequested.PORTRAIT);

pageOrientation = PageFormat.PORTRAIT;

}

if ("LONG_EDGE".equals(duplexMode)) {

atr.add(Sides.TWO_SIDED_LONG_EDGE);

} else {

atr.add(Sides.TWO_SIDED_SHORT_EDGE);

}

//Printing to the default printer

PrintService printer = javax.print.PrintServiceLookup

.lookupDefaultPrintService();

//Creating the printing job

PrinterJob printJob = PrinterJob.getPrinterJob();

printJob.setPrintService(printer);

Book book = new Book();

PageFormat pageFormat = printJob.defaultPage();

pageFormat.setOrientation(pageOrientation);

// Appending a exampledocument to the book

book.append(new ExampleDocument(), pageFormat);

// Appending another exampledocument to the book

book.append(new ExampleDocument(), pageFormat);

// Setting the Pageable to the printjob

printJob.setPageable(book);

try {

// Here a could show the print dialog

// printJob.printDialog(atr);

// Here I pass the previous defined attributes

printJob.print(atr);

} catch (Exception PrintException) {

PrintException.printStackTrace();

}

} catch (PrinterException ex) {

ex.printStackTrace();

}

}

public static final int MARGIN_SIZE = 72;

private static class ExampleDocument implements Printable {

public int print(Graphics g, PageFormat pageFormat, int page) {

Graphics2D g2d = (Graphics2D) g;

g2d.translate(pageFormat.getImageableX(),

pageFormat.getImageableY());

// Only on the first two documents...

if (page <= 1) {

// Prints using one inch margin

g2d.drawString("Printing page " + page + " - duplex...",

MARGIN_SIZE, MARGIN_SIZE);

return (PAGE_EXISTS);

}

return (NO_SUCH_PAGE);

}

}

}

这在PCL6上工作正常,但是,当在PCL5上测试时,我注意到LONG_EDGE和SHORT_EDGE规则被简单地忽略了.在这两种情况下,作业都以LONG_EDGE的形式发送.这不会有问题,除了Java AWT打印API通过逆时针旋转所有页面90º来解决横向打印,从而导致它在SHORT_EDGE模式下打印.

Obs:我能够使用SWT API以纵向和横向两种配置SHORT_EDGE和LONG_EDGE正确打印.但是我无法在兼容SWT的打印请求中转换jasper打印请求.

我的问题是:有没有人遇到过这种情况?给出了哪种解决方案?

根据我的观察,我发现了这些可能的解决方案:

>不要让AWT翻页并发送纵向打印请求,而是强制它将其作为横向打印请求发送;

>找到一种方法,当页面处于横向模式时,反转LONG_EDGE和SHORT_EDGE命令.请注意,对于这个,我有义务纠正两者都被视为LONG_EDGE请求的问题.

>转换JasperReports以使用SWT打印API.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值