当我调用printable的打印方法时,默认情况下不以横向模式打印JPanel

时间:2019-01-05 19:28:51

标签: java swing printing jpanel

这是我打印面板的功能,默认情况下它以纵向模式打印,每次都必须将其更改为横向,我从未用Java完成打印,因此我无法理解以下大多数代码

    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;
        }
            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(this, pf);
            pj.setPageable(book);

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

  if (pj.printDialog() == false)
  return;

  try {
        pj.print();
  } catch (PrinterException ex) {
        // handle exception
  }
}

请帮助,我在做什么错?我尝试了许多不同的方法,但似乎无济于事。

1 个答案:

答案 0 :(得分:0)

嘿,我找到了解决方案,但对这种方式的工作方法并不了解,但是我在Print函数之外设置了所有paper,page格式和book参数,并为书添加了新的Printable(传递所有的PageFormat和Paper参数),我得到了想要的结果。这是可以帮助某人的代码。 默认情况下,以Landscae方向在纸上打印Jpanel。

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