Solved

Java PrintJob works on Windows and Linux but incomplete on Mac

Posted on 2004-08-23
8
1,862 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

688 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