0

I am trying to print a page with custom size. But its not working. I have made variable for width and height I want my page to be.

See my code ones and suggest some changes

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setJobName("Print Data");
        
    job.setPrintable(new Printable(){
        public int print(Graphics pg,PageFormat pf, int pageNum){
            //Paper paper = pf.getPaper();
            Paper paper = new Paper();
            double width = 8d * 22d;
            double height = 4d * 22d;
            double margin = 0.2d * 22d;
            paper.setSize(width, height);
            paper.setImageableArea(
                    margin,
                    margin,
                    width - (margin * 2),
                    height - (margin * 2));
            job.setPrintable(this, pf);
            
            if(pageNum > 0){
                return Printable.NO_SUCH_PAGE;
            }
            
            Graphics2D g2 = (Graphics2D)pg;

            g2.translate(pf.getImageableX(), pf.getImageableY());
            
            System.out.println("width1 = " + pf.getWidth());
            System.out.println("height1 = " + pf.getHeight());

            g2.setColor(Color.BLACK);
            g2.draw(new Rectangle2D.Double(0, 0, pf.getWidth(), pf.getHeight()));

            
            jPanel3.printAll(g2);
     
            return Printable.PAGE_EXISTS; 
        }
    });
    
    System.out.println("1");
    boolean ok = job.printDialog();
    
    if(ok){
        try{
            System.out.println("2");
            job.print();
            System.out.println("3");
        }
        catch (PrinterException ex){
            ex.printStackTrace();
        }
    }

I am stuck at this problem. Whatever size I set. I get print in ISO A4 size. I think their's some mistake with my code.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • Oh, printing, what fun . Okay, the printing API works at a basic level of 72dpi. So if you want to set the paper size to 10x10 cm, you're going to need to convert that to pixels based on 72dpi for [example](https://stackoverflow.com/questions/11803741/printing-in-java-to-label-printer/11805237#11805237) and [example](https://stackoverflow.com/questions/47147662/changing-print-margins-on-jtextpane/47148096#47148096) (it's been a long time since I needed to worry about this so it might have changed) – MadProgrammer Apr 21 '21 at 10:28
  • Okay, also don't try changing the paper inside the `print` method ... kind of to later as the page format and paper details have already been established and the details have probably already been sent to the printer – MadProgrammer Apr 21 '21 at 10:30

1 Answers1

2

There are a number of possible issues, but I'm going to address the big glary one

This...

job.setPrintable(new Printable(){
    public int print(Graphics pg,PageFormat pf, int pageNum){
        //Paper paper = pf.getPaper();
        Paper paper = new Paper();
        double width = 8d * 22d;
        double height = 4d * 22d;
        double margin = 0.2d * 22d;
        paper.setSize(width, height);
        paper.setImageableArea(
                margin,
                margin,
                width - (margin * 2),
                height - (margin * 2));
        job.setPrintable(this, pf);

is a horrible, horrible idea. Just like when you're painting a component, you don't modify the state, it can have unexpected consequences.

Instead, in this case, you should establish the Paper size ahead of time. This should then be applied to a PageFormat and passed to the PrintJob via the setPrintable(Printable, PageFormat) method.

That means, it might look something more like...

PrinterJob job = PrinterJob.getPrinterJob();
job.setJobName("Print Data");
//Paper paper = pf.getPaper();
PageFormat pageFormat = job.defaultPage();
Paper paper = new Paper();
double width = 8d * 22d;
double height = 4d * 22d;
double margin = 0.2d * 22d;
paper.setSize(width, height);
paper.setImageableArea(
        margin,
        margin,
        width - (margin * 2),
        height - (margin * 2));

pageFormat.setPaper(paper);

job.setPrintable(new Printable() {
    @Override
    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.getImageableY());

        System.out.println("width1 = " + pf.getWidth());
        System.out.println("height1 = " + pf.getHeight());

        g2.setColor(Color.BLACK);
        // Consider using pf.getImageableWidth() and pf.getImageableHeight() instead
        g2.draw(new Rectangle2D.Double(0, 0, pf.getWidth(), pf.getHeight()));

        // Maybe consider drawing your border first, as `printAll` 
        // might fill in the whole page
        jPanel3.printAll(g2);

        return Printable.PAGE_EXISTS;
    }
}, pageFormat);

System.out.println("1");
boolean ok = job.printDialog();

if (ok) {
    try {
        System.out.println("2");
        job.print();
        System.out.println("3");
    } catch (PrinterException ex) {
        ex.printStackTrace();
    }
}

I say might, as there's a bunch of validation and other system junk which might still have it end up not been exactly what you want.

I do suggest having a look at ...

as some basic examples, all doing similar things - changing the paper size :D

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Can You give me a program with which I can print my image with specific page format. I converted the jPanel to buffered image and then save it in png form and loaded it in Image class. Now I want this image to be printed with particular size and aspect ratio on print page – Anmol Dhall Apr 21 '21 at 13:01
  • BufferedImage img = new BufferedImage(jPanel3.getWidth(),jPanel3.getHeight(),BufferedImage.TYPE_INT_RGB); jPanel3.paint(img.getGraphics()); try { ImageIO.write(img,"png",new File("saved.png")); Image image = ImageIO.read(new File("saved.png")); } catch (IOException ex) { Logger.getLogger(generate_id_card.class.getName()).log(Level.SEVERE, null, ex); } – Anmol Dhall Apr 21 '21 at 13:04
  • @AnmolDhall Regarding _"Can You give me a program with which I can print my image with specific page format"_, the answer is no. Please note that this is a site which assists developers with their programming problems, but it is not appropriate to ask others to write your code for you. – skomisa Apr 21 '21 at 21:00
  • @AnmolDhall There's at least two linked examples which deal with printing images and almost all the linked examples which change the page size/page format – MadProgrammer Apr 21 '21 at 21:38
  • @AnmolDhall And now you have an [example](https://stackoverflow.com/questions/32000888/how-to-print-whole-jpanel-with-a-footer-in-every-page-in-java-swing/32001433#32001433) which even deals with multiple page printing – MadProgrammer Apr 21 '21 at 21:39