Привет, я нашел решение. Не знаю, как это работает, но я установил все параметры бумаги, формата страницы и книги вне функции «Печать» и добавил книгу с новой возможностью печати (передача всех параметров PageFormat и Paper. ) и я получил результаты, которые хотел. Вот код, который может кому-то помочь. Печать Jpanel на бумаге в Landscae Orientation по умолчанию.
public static void Print(JPanel component){
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setJobName(" Print Component ");
pj.setPrintable (new Printable() {
public int print(Graphics pg, PageFormat pf, int pageNum){
if (pageNum > 0){
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) pg;
g2.translate(pf.getImageableX() + pf.getImageableWidth() / 2 - component.getWidth() / 2, pf.getImageableY() + pf.getImageableHeight() / 2 - component.getHeight() / 2);
//g2.scale(pf.getImageableWidth()/component.getWidth(), pf.getImageableHeight()/component.getHeight());
component.paint(g2);
return Printable.PAGE_EXISTS;
}
});
PageFormat pf = pj.defaultPage();
pf = pj.defaultPage();
Paper paper = pf.getPaper();
paper.setSize(8.5 * 72, 14 * 72);
paper.setImageableArea(0.5 * 72, 0.0 * 72, 8 * 72, 14 * 72);
pf.setPaper(paper);
pf.setOrientation(PageFormat.LANDSCAPE);
Book book = new Book();//java.awt.print.Book
book.append((new Printable() {
public int print(Graphics pg, PageFormat pf, int pageNum){
if (pageNum > 0){
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) pg;
g2.translate(pf.getImageableX() + pf.getImageableWidth() / 2 - component.getWidth() / 2, pf.getImageableY() + pf.getImageableHeight() / 2 - component.getHeight() / 2);
//g2.scale(pf.getImageableWidth()/component.getWidth(), pf.getImageableHeight()/component.getHeight());
component.paint(g2);
return Printable.PAGE_EXISTS;
}
}), pf);
pj.setPageable(book);
if (pj.printDialog() == false)
return;
try {
pj.print();
} catch (PrinterException ex) {
// handle exception
}
}