本文介绍了第一页上的页面打印帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 myPrintableObjectPrintable 类,并且打印方法以下列方式被覆盖:

I have a Printable class named myPrintableObject and print method is over-ridden in following manner:

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;}

     }

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

I wanted to print multiple pages in landscape orientation in same document. It is printing except the first page. It is always getting printed in portrait orientation.

我该如何解决这个问题?

How can I fix this?

推荐答案

你在这里:

    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 */
            }
        }
    }
}

这篇关于第一页上的页面打印帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:52