Anmol Dhall
Anmol Dhall

Reputation: 1

Page formating not working for printing a page using Java in Netbeans

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.

Upvotes: 0

Views: 606

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347332

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

Upvotes: 2

Related Questions