在一个文档中打印两个Jpanel

时间:2009-06-22 11:45:59

标签: java swing

我有两个不同的面板,但我需要将它们一个文档发送到两个页面。第一页打印在前面,第二页打印在背面可以任何人请帮助我,我已发送一个jpanel但如何发送第二个与它。这是我的代码

private void printCard() {

        PrinterJob printjob = PrinterJob.getPrinterJob();
        printjob.setJobName(" CARD ");
        Printable printable = new Printable() {
                public int print(Graphics pg, PageFormat pf, int pageNum) {
                    if (pageNum > 0) {
                        return Printable.NO_SUCH_PAGE;
                        }

                        Dimension imageDimension = jLayeredPane2.getSize();
                        BufferedImage bufferedImage = new BufferedImage(imageDimension.width, imageDimension.height, BufferedImage.TYPE_INT_RGB);
                        jLayeredPane2.print(bufferedImage.getGraphics());
                        Graphics2D g2 = (Graphics2D) pg;
                        g2.translate(pf.getImageableX(), pf.getImageableY());
                        g2.drawImage(bufferedImage, 0, 0, (int) pf.getWidth(), (int) pf.getHeight(), null);

                        Dimension backimage=jLayeredPane4.getSize();
                        BufferedImage bufferedImage1 = new BufferedImage(backimage.width, backimage.height, BufferedImage.TYPE_INT_RGB);
                        jLayeredPane4.print(bufferedImage1.getGraphics());
                        g2.drawImage(bufferedImage1, 0, 0, (int) pf.getWidth(), (int) pf.getHeight(), null);
                        return Printable.PAGE_EXISTS;

                }
        };

        Paper paper = new Paper();
        paper.setImageableArea(0, 0, 153, 243);
        paper.setSize(243, 153);
        PageFormat format = new PageFormat();
        format.setPaper(paper);
        format.setOrientation(PageFormat.REVERSE_LANDSCAPE);
        printjob.setPrintable(printable,format);

        try {
               printjob.print();
        } catch (PrinterException ex) {
                System.out.println("Sorry No Image Found" + ex);
        }

由于     }

1 个答案:

答案 0 :(得分:1)

而不是使用java.awt.print.Printable,而是使用java.awt.print.Pageable。这将允许您指定两个页面,然后以双面打印以获得正面和背面。 PrinterJob有一个setPageable()函数以及一个setPrintable()。为了使它更容易,使用Book并只添加两个Printables,每页一个。