Solved

Printer Setup error in Java Program!

Posted on 2003-11-30
19
423 Views
Last Modified: 2012-08-13
Hi Experts,
  With the help of an experts opinion (question posted earlier in Experts-exchange), I created a Print option in my java program and it works perfectly well.  But when I try to adjust the printable area using our Java Program it is showing some error.
The code is as follows :
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(OrientationRequested.PORTRAIT);
pras.add(MediaSizeName.ISO_A4);
pras.add(new MediaPrintableArea(5,6,200,285,1000));
pras.add(new Copies(1));
pras.add(new JobName("My job", null));
PrintService printServices[] =
PrintServiceLookup.lookupPrintServices(flavor, pras);

Before adding the line :
pras.add(MediaSizeName.ISO_A4);
pras.add(new MediaPrintableArea(5,6,200,285,1000));
it works perfectly well.  Just I added the above 2 lines in my program and the program is compiling but print out is not coming.  Suggest a solution.

Regrds
Suresh Babu
0
Comment
Question by:sureshbvs
  • 10
  • 9
19 Comments
 
LVL 4

Expert Comment

by:Howie_Ly
ID: 9848334
Could try using this instead :

pras.add( new MediaPrintableArea(5, 5, 200, 300, MediaPrintableArea.MM ));



0
 

Author Comment

by:sureshbvs
ID: 9848833
Howie_Ly,
  I tried that option also.. still printout is not coming.  Compiling and when I click Print option nothing is appearing.

REgards
Suresh
0
 
LVL 4

Expert Comment

by:Howie_Ly
ID: 9849262
> PrintService printServices[] =
> PrintServiceLookup.lookupPrintServices(flavor, pras);

Does your print service allow 5,5,200,300 mm?  Do you have multiple trays on your printer?

Could you loop through the printservices array and tell me if the correct printer/tray is found.
0
 

Author Comment

by:sureshbvs
ID: 9849305
I need the printout to be in A4 size paper and that tray is available in the printer.  Without giving
pras.add(new MediaPrintableArea(5,6,200,285,1000));
or
pras.add(new MediaPrintableArea(5,6,200,285,MediaPrintableArea.MM ));

the printout is coming perfectly well with default left, right, top and bottom margin.

I just need to adjust this margin using our Java program, because I am trying to take the printout of  a  Bill which should print after 5 - 10 mm from the top and left in A4 size paper.

Regards
Suresh
0
 

Author Comment

by:sureshbvs
ID: 9849317
Code used for taking print is as follows :
If we are not using the line
pras.add(new MediaPrintableArea(10, 10, 200, 200, MediaPrintableArea.MM ));
the print is coming perfectly well....
Is there any other suggestions for setting the unprintable area or printable area....
-----------------------------------------
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(OrientationRequested.PORTRAIT);
pras.add(MediaSizeName.ISO_A4);
pras.add(new MediaPrintableArea(10, 10, 200, 200, MediaPrintableArea.MM ));
pras.add(new Copies(1));
pras.add(new JobName("My job", null));
PrintService printServices[] =
            PrintServiceLookup.lookupPrintServices(flavor, pras);
for (int i=0;i<printServices.length;i++)
{
System.out.println("Checking : "+i+" > "+printServices[i].getName());
// "printer" is a String which contains the name of the printer.....
if (printer.equals(""+printServices[i].getName()))
{                
    DocPrintJob job = printServices[i].createPrintJob();
    Doc doc = new SimpleDoc(jtp, flavor,null);
    try {
          job.print(doc, pras);
          System.out.println(printer);
    } catch (PrintException ex) {
    ex.printStackTrace();   }
break;
}
 }

Regards
suresh
0
 

Author Comment

by:sureshbvs
ID: 9850417
Hi..
  Please give a suitable solution for the above problem.. Also let me know if there is any other alternative to solve this problem.. Please...

Regards
Suresh Babu
0
 
LVL 4

Expert Comment

by:Howie_Ly
ID: 9853726
If this question is urgent, please up your points according to EE standards.

- This question is urgent or extremely difficult (500 points)
- This question is important or difficult (250 points)
- This question is not important and moderately difficult (125 points)
- This question is not important and easy (50 points)

0
 
LVL 4

Expert Comment

by:Howie_Ly
ID: 9853924
One other thing.. can you tell me what is the fileformat of jtp? (e.g. File("C:\\test.doc") etc)

0
 

Author Comment

by:sureshbvs
ID: 9855668
Hi Howie_Ly,
  Available points in my bag is just 150.  ie., the reason for puting the points as 25.  About the fileformat I am using html file for printing ie., C:\\io.html.  Please help me.
Regards
Suresh
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:Howie_Ly
ID: 9855706
Suresh, i'm beginning to suspect io.html is not a SERVICE_FORMATTED.PRINTABLE type.  Does your exception look like this?

> java.lang.IllegalArgumentException: data is not of declared type

If so you may have to create a new type which implements printable.




0
 

Author Comment

by:sureshbvs
ID: 9855900
But sir, without giving the line
pras.add(new MediaPrintableArea(10, 10, 200, 200, MediaPrintableArea.MM ));
or
pras.add(new MediaPrintableArea(10, 10, 200, 200, 1000));

the printout is coming perfectly well with all formats.  If SERVICE_FORMATTED.PRINTABLE type is having problem, can u suggest any other type for replacing this line.



0
 
LVL 4

Expert Comment

by:Howie_Ly
ID: 9855940
One other thing you could check for is that the options

> pras.add(new MediaPrintableArea(10, 10, 200, 200, MediaPrintableArea.MM ));
or
> pras.add(new MediaPrintableArea(10, 10, 200, 200, 1000));

are available in your print services.

A simple

> System.out.println(printServices.length);

after this line

> PrintService printServices[] =
            PrintServiceLookup.lookupPrintServices(flavor, pras);

will tell you.  On my system, it's come up with length zero.  So i cannot continue.
0
 

Author Comment

by:sureshbvs
ID: 9855953
When I give System.out.println(printServices.length); without adding the line  
pras.add(new MediaPrintableArea(10, 10, 200, 200, 1000));
shows 3 and it is taking print out.

But when i add the line : pras.add(new MediaPrintableArea(10, 10, 200, 200, 1000));
the length is showing 0.

Suresh


0
 
LVL 4

Expert Comment

by:Howie_Ly
ID: 9855977
Seems as though you've got the same problem also.  Last resort before you may have to give up on the MediaPrintableArea option.

Try this

>        PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);

And input the page setup margins manually there.


0
 
LVL 4

Accepted Solution

by:
Howie_Ly earned 25 total points
ID: 9855980
SAMPLE
=====



import javax.print.*;
import javax.print.attribute.*;
import java.io.*;

public class printGIF {
   
    public static void main(String args[]) throws Exception {
        //String filename = args[0];
        String filename = "C:\\test.gif";
        PrintRequestAttributeSet pras =
        new HashPrintRequestAttributeSet();
        DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
       
        PrintService printService[] =
        PrintServiceLookup.lookupPrintServices(flavor, pras);
       
        PrintService defaultService =
        PrintServiceLookup.lookupDefaultPrintService();
       
        PrintService service = ServiceUI.printDialog(null, 200, 200,
        printService, defaultService, flavor, pras);
       
        if (service != null) {
            System.out.println("Creating Print Job");
            DocPrintJob job = service.createPrintJob();
            //DocPrintJob job = defaultService.createPrintJob();
            FileInputStream fis = new FileInputStream(filename);
            DocAttributeSet das = new HashDocAttributeSet();
           
            Doc doc = new SimpleDoc(fis, flavor, das);
            System.out.println("Printing");
            job.print(doc, pras);
            System.out.println("Printing Complete?");
            //Thread.sleep(10000);
        }else{
            System.out.println("it's null!");
        }
        System.exit(0);
    }
}
0
 

Author Comment

by:sureshbvs
ID: 9909787
Moderator,
  I got the solution for the above problem :

Object[] o = (Object[]) printServices[i].getSupportedAttributeValues(MediaPrintableArea.class, flavor, pras);
MediaPrintableArea pa = (MediaPrintableArea) o[0];
pa =      new MediaPrintableArea(0,0,150,150,MediaPrintableArea.MM);
pras.add(pa);

So I kindly request the moderator to refund my points and requested to close this question.

Suresh Babu
0
 
LVL 4

Expert Comment

by:Howie_Ly
ID: 9909938
So this was your problem??

> pras.add(new MediaPrintableArea(10, 10, 200, 200, 1000));

0
 

Author Comment

by:sureshbvs
ID: 10232710
Moderator,
  I got the solution for the above problem :

Object[] o = (Object[]) printServices[i].getSupportedAttributeValues(MediaPrintableArea.class, flavor, pras);
MediaPrintableArea pa = (MediaPrintableArea) o[0];
pa =     new MediaPrintableArea(0,0,150,150,MediaPrintableArea.MM);
pras.add(pa);

So I kindly request the moderator to refund my points and requested to close this question.

Suresh Babu
0
 
LVL 4

Expert Comment

by:Howie_Ly
ID: 10232762
I thought the previous post

<quote>And input the page setup margins manually there.</quote>

implied that you should play around with the margins.  I feel as though this question was answered.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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.

863 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

23 Experts available now in Live!

Get 1:1 Help Now