1

我有一个Printable名为的类myPrintableObject,打印方法以下列方式被覆盖:

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

     }

我想在同一个文档中横向打印多页。除了第一页外,它正在打印。它总是以纵向打印。

我怎样才能解决这个问题?

4

1 回答 1

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 */
            }
        }
    }
}
于 2012-08-30T09:38:16.210 回答