第一页上的页面打印帮助

时间:2012-05-08 09:15:47

标签: java printing

我有一个名为Printable的{​​{1}}类,打印方法以下列方式覆盖:

myPrintableObject

我想在同一文档中横向打印多个页面。它是除第一页以外的打印。它始终以纵向打印。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

你在这里:

    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    pf.setOrientation(PageFormat.LANDSCAPE);
    job.setPrintable(myPrintableObject, pf);

工作示例:

public class MyPrintable implements Printable {

    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if (pageIndex < 5) {
            graphics.drawString("HELLO FRIEND", 100, 180);
            return PAGE_EXISTS;
        } else {
            return NO_SUCH_PAGE;
        }
    }

    public static void main(String[] args) {
        PrinterJob job = PrinterJob.getPrinterJob();
        PageFormat pf = job.defaultPage();
        pf.setOrientation(PageFormat.LANDSCAPE);
        job.setPrintable(new MyPrintable(), pf);

        boolean ok = job.printDialog();
        if (ok) {
            try {
                job.print();
            } catch (PrinterException ex) {
                /* The job did not successfully complete */
            }
        }
    }
}