両面プリンターの横向きモードで印刷する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互換の印刷要求に変換することはできません。
私の質問は、この状況に直面したことがある人はいますか? どの解決策が与えられましたか?
私の観察から、これらの可能な解決策を見つけました:
AWTにページをめくらせて縦向きの印刷要求を送信させる代わりに、強制的に横向きの印刷要求として送信させます。
ページがランドスケープ モードのときに、LONG_EDGE コマンドと SHORT_EDGE コマンドを逆にする方法を見つけます。この場合、両方がLONG_EDGEリクエストとして扱われるという問題を修正する義務があることに注意してください。
SWT印刷APIを使用するようにJasperReportsを変換します。