在双面打印模式下使用Java打印横向文档
我有一份JasperReports报告要在双面打印机上以横向模式打印。在这方面,我必须支持PCL5和PCL6打印驱动程序 在internet上搜索时,我发现了以下代码片段来完成此工作:在双面打印模式下使用Java打印横向文档,java,printing,jasper-reports,duplex,Java,Printing,Jasper Reports,Duplex,我有一份JasperReports报告要在双面打印机上以横向模式打印。在这方面,我必须支持PCL5和PCL6打印驱动程序 在internet上搜索时,我发现了以下代码片段来完成此工作: import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.print.Book; import java.awt.print.PageFormat; import java.awt.print.Printable; import j
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);
}
}
}
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.print.Book;
导入java.awt.print.PageFormat;
导入java.awt.print.Printable;
导入java.awt.print.PrinterException;
导入java.awt.print.PrinterJob;
导入javax.print.PrintService;
导入javax.print.attribute.HashPrintRequestAttributeSet;
导入javax.print.attribute.PrintRequestAttributeSet;
导入javax.print.attribute.standard.orientationrequired;
导入javax.print.attribute.standard.Sides;
公共类打印测试{
公共静态void main(字符串[]args){
试一试{
//这是用于配置的
字符串方向=“横向”;
字符串duplexMode=“长边”;
int pageOrientation=0;
PrintRequestAttributeSet atr=新的HashPrintRequestAttributeSet();
如果(“横向”。等于(方向)){
atr.add(方向请求。横向);
pageOrientation=PageFormat.scape;
}else if(“反向_横向”。等于(方向)){
atr.add(要求方向。反转方向);
pageOrientation=PageFormat.REVERSE\u横向;
}否则{
atr.add(方向请求的肖像);
pageOrientation=PageFormat.PORTRAIT;
}
if(“长边”。等于(双工模式)){
atr.添加(侧面、双面、长边);
}否则{
atr.添加(侧面、双面、短边);
}
//打印到默认打印机
PrintService printer=javax.print.PrintServiceLookup
.lookupDefaultPrintService();
//创建打印作业
PrinterJob printJob=PrinterJob.getPrinterJob();
printJob.setPrintService(打印机);
书=新书();
PageFormat PageFormat=printJob.defaultPage();
pageFormat.setOrientation(pageOrientation);
//将示例文档附加到书中
book.append(新示例文档(),页面格式);
//在书中附加另一个示例文档
book.append(新示例文档(),页面格式);
//将页面设置为可打印作业
打印作业。可设置页面(书本);
试一试{
//这里可以显示打印对话框
//printJob.printDialog(atr);
//这里我传递前面定义的属性
打印作业。打印(atr);
}捕获(异常打印异常){
PrintException.printStackTrace();
}
}捕获(打印例外){
例如printStackTrace();
}
}
公共静态最终整型余量=72;
私有静态类ExampleDocument实现可打印{
公共整型打印(图形g、页面格式、页面格式、整型页面){
Graphics2D g2d=(Graphics2D)g;
g2d.translate(pageFormat.GetImageTablex(),
getImageTabley());
//仅在前两个文档上。。。
如果(page不确定是否有帮助,但我在6年前就遇到了这个问题,唯一的解决方案是使用打印机(Xerox)功能通过.xpf文件接收打印指令。示例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xpif SYSTEM "xpif-v02012.dtd">
<xpif cpss-version="2.01" version="1.0" xml:lang="en">
<xpif-operation-attributes>
<job-name syntax="name" xml:lang="en" xml:space="preserve">job name</job-name>
<requesting-user-name syntax="name" xml:space="preserve">domain user</requesting-user-name>
</xpif-operation-attributes>
<job-template-attributes>
<copies syntax="integer">1</copies>
<finishings syntax="1setOf">
<value syntax="enum">3</value>
</finishings>
<job-recipient-name syntax="name" xml:lang="en-US" xml:space="preserve">domain user</job-recipient-name>
<job-save-disposition syntax="collection">
<save-disposition syntax="keyword">none</save-disposition>
</job-save-disposition>
<media-col syntax="collection">
<media-type syntax="keyword">stationery</media-type>
<media-hole-count syntax="integer">0</media-hole-count>
<media-color syntax="keyword">blue</media-color>
<media-size syntax="collection">
<x-dimension syntax="integer">21000</x-dimension>
<y-dimension syntax="integer">29700</y-dimension>
</media-size>
</media-col>
<orientation-requested syntax="enum">3</orientation-requested>
<sheet-collate syntax="keyword">collated</sheet-collate>
<sides syntax="keyword">one-sided</sides>
<x-side1-image-shift syntax="integer">0</x-side1-image-shift>
<y-side1-image-shift syntax="integer">0</y-side1-image-shift>
</job-template-attributes>
</xpif>
职务名称
域用户
1.
3.
域用户
没有一个
文具
0
蓝色
21000
29700
3.
整理
单面的
0
0
它与需要打印的文件一起发送到打印机。为什么需要PCL5?由于我们的项目正在从Delphi平台迁移到Java,我们必须保持对旧版本中的功能的支持。我们希望避免将PCL6打印机驱动程序作为要求添加到Java版本中。今天,我们准备让系统运行tSWT
、PCL5
或AWT
、PCL6中的打印例行程序用于双面横向报表。SWT
中的问题是,要打印,我需要将报表页面在内存中处理为图像,这对于某些用户站来说非常沉重。