1

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)
        {

        }
vivek
  • 11
  • 3
  • I think you might find that the print dialog is screwing your settings... – MadProgrammer Jul 31 '13 at 09:48
  • I commented print dialog, but then also same output. – vivek Jul 31 '13 at 09:49
  • Okay, verify the paper size before and after the validatePage call, make sure you retrieve the Paper object from the page format – MadProgrammer Jul 31 '13 at 09:56
  • Checked the size before & after validatePage. It is same. – vivek Jul 31 '13 at 10:23
  • Have you tried printing to a different printer? I tend to use the PDF output options (if available) to do my printing tests. Have you dumped the page format information just before the `job.print` and within the printable itself. You may find that the dot amtrix printer simply can't deal with custom page sizes... – MadProgrammer Jul 31 '13 at 11:15
  • we have tried on TVS and Wep printer. Both has same issue. Also we dumped the page format info on the console just before print and after print. It is same as what we are setting through code. – vivek Jul 31 '13 at 11:47
  • Then there is something else going on, cause all my tests work just fine – MadProgrammer Jul 31 '13 at 20:03
  • It is working on Windows system, On linux still same issue. When i print hexdump in linux then Escape command for page length is missing. – vivek Aug 05 '13 at 06:19
  • I could be a bug with the particular printer driver. Have you tried different printers – MadProgrammer Aug 05 '13 at 06:41
  • Yes we have tried with 2 different printers(TVS and Wep) with different drivers. But with any combination there is same issue in Linux – vivek Aug 06 '13 at 06:41
  • Do you know how set printer escape sequence or how to send hex data to printer through the API which we are using(PrinterJob.print())? – vivek Aug 06 '13 at 07:44

0 Answers0