Not able to Set custom page size through java printing on dot matrix printer

1.5k Views Asked by At

We are trying to print through java on dot matrix printer. We have created the Paper object with custom size. But when it goes to printer, it doesn't take the custom size. It takes either 11 inches or 12 inch size. Below is the code which we are using. Please suggest the solution.

    PrinterJob job = PrinterJob.getPrinterJob();    // Get a PrinterJob.      

    PageFormat format = job.defaultPage();   // Get the default page format, then ask the user to customize it.

    Paper paper = format.getPaper();         // Note: Custom size of paper should be supported by attach Printer.

    paper.setSize((PaperWidth*72),
                    (PaperHeight*72));      // Set Custom size of the Paper.

    paper.setImageableArea(MarginLeft*72,  MarginTop*72,
                           paper.getWidth() - MarginRight*72 - MarginLeft*72,
                           paper.getHeight()- MarginBottom*72 - MarginTop*72);

    System.out.println(paper.getHeight());

    format.setPaper(paper);                                 // Set the paper.


    PageFormat pf = job.validatePage(format);

    Book bk = new Book();                                   // Set up a book, with exact no. of pages to be printable.
    bk.append(new TestClass(), pf, numPages);
    job.setPageable(bk);                                    // Pass the book to the PrinterJob

    ////// OR set printable without book.
    //// job.setPrintable(new TestClass(),format);

    if (job.printDialog())                                  // Put up the dialog box
    {    
        try  
        {
            job.print();                                    // Print the job if the user didn't cancel printing.
        } 
        catch (PrinterException ex)
        {

        }
0

There are 0 best solutions below