Talha Bin Shakir
Talha Bin Shakir

Reputation: 2561

Printing two Jpanels in one document

I have two different Panels but i have need to send them in one document in to two pages. first page print at front and the second will print at the back side can anyone please help me i have send one jpanel but how to send second with it. Here is my code

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

Thanks }

Upvotes: 0

Views: 455

Answers (1)

CarlG
CarlG

Reputation: 1666

Instead of using a java.awt.print.Printable, use java.awt.print.Pageable instead. This will let you specify two pages, and then print in duplex to get front and back. PrinterJob has a setPageable() function as well as a setPrintable(). To make it even easier, use a Book and just add two Printables, one for each page.

Upvotes: 1

Related Questions