Solved

Printer Setup error in Java Program!

Posted on 2003-11-30
19
428 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to install java on RHEL image on EC2 4 41
ejb mdb examples 1 8
ejb entity bean example issue 2 16
Glassfish admin console not working 1 32
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…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

860 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