Solved

Printer Setup error in Java Program!

Posted on 2003-11-30
19
420 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
Comment Utility
Could try using this instead :

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



0
 

Author Comment

by:sureshbvs
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 4

Expert Comment

by:Howie_Ly
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
So this was your problem??

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

0
 

Author Comment

by:sureshbvs
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

772 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

16 Experts available now in Live!

Get 1:1 Help Now