Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Printer Setup error in Java Program!

Posted on 2003-11-30
19
Medium Priority
?
445 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
[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
  • 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
Technology Partners: 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!

 

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
 
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 50 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

722 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