• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 459
  • Last Modified:

Printer Setup error in Java Program!

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
sureshbvs
Asked:
sureshbvs
  • 10
  • 9
1 Solution
 
Howie_LyCommented:
Could try using this instead :

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



0
 
sureshbvsAuthor Commented:
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
 
Howie_LyCommented:
> 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
sureshbvsAuthor Commented:
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
 
sureshbvsAuthor Commented:
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
 
sureshbvsAuthor Commented:
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
 
Howie_LyCommented:
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
 
Howie_LyCommented:
One other thing.. can you tell me what is the fileformat of jtp? (e.g. File("C:\\test.doc") etc)

0
 
sureshbvsAuthor Commented:
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
 
Howie_LyCommented:
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
 
sureshbvsAuthor Commented:
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
 
Howie_LyCommented:
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
 
sureshbvsAuthor Commented:
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
 
Howie_LyCommented:
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
 
Howie_LyCommented:
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
 
sureshbvsAuthor Commented:
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
 
Howie_LyCommented:
So this was your problem??

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

0
 
sureshbvsAuthor Commented:
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
 
Howie_LyCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 10
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now