Solved

Java PrintJob works on Windows and Linux but incomplete on Mac

Posted on 2004-08-23
8
1,821 Views
Last Modified: 2009-07-29
Java application 1.4.1-04 prints a multi-page graphic and text fine on Windows and Linux but produces incomplete output on Mac OSX 10.3.5 (jre 1.4.2-05).  Different bits of the page are missing each time it runs!  No problems on the Windows or Linux boxes so is there something different about the Mac java I'm not aware of?  Looks like the graphic is not completly drawn before the actual print ocurs.

The codJava application 1.4.1-04 prints a multi-page graphic and text fine on Windows and Linux but produces incomplete output on Mac OSX 10.3.5 (jre 1.4.2-05).  Different bits of the page are missing each time it runs!  No problems on the Windows or Linux boxes so is there something different about the Mac java I'm not aware of?  Looks like the graphic is not completly drawn before the actual print ocurs.

The code is basically:

printJob = printJob.getPrinterJob();
printJob.setPrintable(this, pf);
printBook = new Book();
for(int i = 0; i < pages; i++)
{
    printBook.append((Printable)this, pf);
}
printJob.setPageable(printBook);
if(printJob.printDialog())
        printJob.print();

Thankse is basically:

printJob = printJob.getPrinterJob();
printJob.setPrintable(this, pf);
printBook = new Book();
for(int i = 0; i < pages; i++)
{
    printBook.append((Printable)this, pf);
}
printJob.setPageable(printBook);
if(printJob.printDialog())
        printJob.print();

Thanks
0
Comment
Question by:John4343
  • 4
  • 3
8 Comments
 
LVL 12

Expert Comment

by:Giant2
ID: 11878308
I see you use 2 different JRE to execute. Try to use the same JRE on both machine (Linux, Windows, Mac OS).
Do the error remains?
0
 
LVL 20

Accepted Solution

by:
Venabili earned 500 total points
ID: 11878760
Yes,
In the J2SE 1.4.1 or 1.4.2 on Mac OS X javax.print is not fully implementeed and is not fully working (don't ask me... tok me a week to find a url that proves what I already suspectred.\

Here we go:
http://developer.apple.com/java/faq/issues.html#anchor6
The workaround is to use the old print methods from jawa.awt.print:
              PrintRequestAttributeSet  pras = new HashPrintRequestAttributeSet();
               PrintService service = null;
               java.awt.print.PrinterJob pjob = java.awt.print.PrinterJob.getPrinterJob();
               PrintService printService[] =  pjob.lookupPrintServices();
               if (printService == null || printService.length == 0) {
                   ERROR = 1;
                   return null;
              }
           if ( pjob.printDialog()){
               service = pjob.getPrintService();
               pJob = pjob;
               service = servicePrint;
           }else{
               service = null;
           }
if (servicePrint!=null){
               DocAttributeSet das = new HashDocAttributeSet();
              Doc doc = new SimpleDoc(book, DocFlavor.SERVICE_FORMATTED.PAGEABLE, das);
                DocPrintJob job1 = servicePrint.createPrintJob();
                job1.print(doc, pras);
}


Let me know if you need assitance on this (I am not sure if it compiles as it is as I removed some of the code taht is specific for my application - if there are troubles let me know.

As a whole I just check if it is a MacOS or not and depending on the case use different print code (this one is for MacOS - your is ok for any other)
Checking for MacOS:
             System.getProperty("mrj.version")  is null if you are not on Mac ;)
0
 

Author Comment

by:John4343
ID: 11882640
I'm not quite certain about this so please bare with me.  This code works in Windows but crashes on the indicated line on the Mac with a null pointer exception!

import java.awt.print.*;

javax.print.attribute.PrintRequestAttributeSet pras = new javax.print.attribute.HashPrintRequestAttributeSet();
javax.print.PrintService service = null;
PrinterJob pjob = PrinterJob.getPrinterJob();
javax.print.PrintService printService[] =  pjob.lookupPrintServices();
if (printService == null || printService.length == 0)
{
    System.out.println ("ERR");
    return;
}
                  
PageFormat pf = pageFormat.convertToPageFormat();
Book book = new Book();
int pages = 3;
for(int i = 0; i < pages; i++)
{
    book.append((Printable)this, pf);
}
pjob.setPageable(book);
if (pjob.printDialog())
{
   service = pjob.getPrintService();
}
else
{
   service = null;
}
if (service!=null)
{
   javax.print.attribute.DocAttributeSet das = new javax.print.attribute.HashDocAttributeSet();
   javax.print.Doc doc = new javax.print.SimpleDoc(book, javax.print.DocFlavor.SERVICE_FORMATTED.PAGEABLE, das);
   javax.print.DocPrintJob job1 = service.createPrintJob();
   try
   {
      job1.print(doc, pras);  <<=== NPE
   }
   catch(Exception ee)
   {
      System.out.println ("Error 1");
      ee.printStackTrace();
   }
}

Thanks
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 20

Expert Comment

by:Venabili
ID: 11882746
The StackTrace?
0
 
LVL 20

Expert Comment

by:Venabili
ID: 11882824
Oops... I think I need coffee.  This is half the Win code, half the Mac code :(

Sorry

THe actual printing should be:
if (service!=null)
{
   try
   {
       pJob.print();
   }
   catch(Exception ee)
   {
      System.out.println ("Error 1");
      ee.printStackTrace();
   }
}

The null pointer is exactly from the not implemented method I was trying to warn you about :(

Which returns us in the almost initial position...

Venabili
0
 

Author Comment

by:John4343
ID: 11886841
Here is the current code.  Works OK on Windows but still incomplete output on Mac.

Thanks for the help.  I'll be out of town till late Friday and will try not to think too hard...

import java.awt.print.*;

javax.print.attribute.PrintRequestAttributeSet pras = new javax.print.attribute.HashPrintRequestAttributeSet();
javax.print.PrintService service = null;
PrinterJob pjob = PrinterJob.getPrinterJob();
javax.print.PrintService printService[] =  pjob.lookupPrintServices();
if (printService == null || printService.length == 0)
{
   System.out.println ("ERR");
   return;
}
PageFormat pf = pageFormat.convertToPageFormat();
Book book = new Book();
int pages = 3;
for(int i = 0; i < pages; i++)
{
   book.append((Printable)this, pf);
}
pjob.setPageable(book);
if (pjob.printDialog())
{
   service = pjob.getPrintService();
}
else
{
   service = null;
}
if (service!=null)
{
   try
   {
      pjob.print();
   }
   catch(Exception ee)
   {
      System.out.println ("Error 1");
      ee.printStackTrace();
   }
}
0
 

Author Comment

by:John4343
ID: 11931157
Ah! Got it!!!  

awt.print runs in a separate thread so needed to do a synchronize so the page will be rendered completly.

Thanks
0
 
LVL 20

Expert Comment

by:Venabili
ID: 11931199
If you prefer you can get your points back? After all you found it itself. However - if you think that I had helped, it's ok

Venabili
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now