代码之家  ›  专栏  ›  技术社区  ›  WelcomeTo

打印时设置景观方向

  •  1
  • WelcomeTo  · 技术社区  · 14 年前

    我需要打印一张图片。当我将方向设置为

    printRequestAttributeSet.add(OrientationRequested.LANDSCAPE);

    一切都很好。

    但当我在里面设定方向时 print() 方法 Printable 以下为:

    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
                if (pageIndex >= images.size()) 
                    return Printable.NO_SUCH_PAGE;
    
                image = images.get(pageIndex);
                // if image width>height --> Landscape, else --> Protrait
                if (image.getWidth(null) > image.getHeight(null)) 
                    pageFormat.setOrientation(PageFormat.LANDSCAPE);
                else 
                    pageFormat.setOrientation(PageFormat.PORTRAIT);
    
                graphics2D = (Graphics2D) graphics;
                graphics.drawImage(image, 0, 0, image.getWidth(null), image.getHeight(null),   null);
    
                return PAGE_EXISTS;
    };
    

    它不适用于首页。即它以横向模式打印除第1页以外的所有页。

    2 回复  |  直到 14 年前
        1
  •  4
  •   MadProgrammer    14 年前

    当您已经尝试打印页面时,无法更改方向。

    如果您需要提供多个具有不同方向的页面,您需要查看 Book Pageable 接口,请参阅 Printing in Java 例如。

    唯一的其他解决方案是在中旋转图像 Printable ,这充其量也很麻烦。

    ps-打印很有趣。。。工作时;)

        2
  •  0
  •   user2807083    8 年前

    位置 pf.setOrientation(PageFormat.LANDSCAPE) 打电话之前 printJob

    前。

    if(pay_type.getSelectedItem().equals("EMI"))
      {
          EMI();
      }
      else{
           validation();  
      }
       PrinterJob job = PrinterJob.getPrinterJob();
         job.setPrintable((Printable) this);
         boolean ok = job.printDialog();
         if (ok) {
             try {
                 //here set page oriatetion
                  job.print();
             } catch (PrinterException ex) {
              /* The job did not successfully complete */
             }
         }
    
    推荐文章