Print with Java

Hi,
I have some documents to be printing by user.
I would like to print only with on click. I explain.
I have put a button in my web application that is 'Print'.
That i would like to do is, when the button is clicking by user, print start automaticaly whitout any other screen.
Is that possible ?
By default, when i want to print some documents, i always have a popup window that ask me which printer i want to choose. I want to specify it in code.
Thanks for reply
Imhotep

All right, i have got any solution to do what i want.
But is that possible to print the first page of a document in portrait, the second page in landscape and the last one in portrait ??

Similar Messages

  • Cannot print on a USB printer with Java PrintService in Windows 2000. Why?

    I want to print a simple ASCII file on a USB printer under Win2K and
    found 2 problems. Here is the 2nd one.
    Even though
    PrintServiceLookup.lookupPrintServices(<DocFlavor>, <AttributeSet>)
    returns nothing when I specify any AttributeSet, I can still print on the default printer, which I get by
    PrintService defServicedefService = PrintServiceLookup.lookupDefaultPrintService();
    but only on a parallel printer (HP Laserjet 4 Plus), either directly attached or over the network. When I try the very same code on a locally USB-attached printer, the printer is obviously reacting, but nothing is printed.
    In case of a Lexmark 730 (an ink printer), the driver even lies, it has printed 100%,
    on a Samsung CLP-510 (a color laser) the ready light flashes twice, that's all.
    The file to be printed has 5 lines and a "0C"X at the end in order to force a page eject at the end.
    I tried it with JDK 1.4.2 and with Java 6, same problem.
    I should mention that I can print that little file out of the editor Textpad without any problem.

    Thanks a lot for the quick hint, but it did not help.
    I used BasicPrint.java and PrintJobWatcher.java of the samples for testing.
    Printing a GIF file works both to a parallel printer and a USB printer.
    Then I changed only the DocFlavor from DocFlavor.INPUT_STREAM.GIF to DocFlavor.INPUT_STREAM.AUTOSENSE, and that one works with my parallel printer, but on the USB printer the Ready light flashes twice, that's all.
    The ASCII file must have a "0C"X at the end to force a page eject on the parallel printer
    Maybe it is a matter of the DocFlavor, but which one?
    DocFlavor("application/octet-stream", "java.io.InputStream") works like DocFlavor.INPUT_STREAM.AUTOSENSE.
    DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST and DocFlavor.INPUT_STREAM.TEXT_HTML_US_ASCII cause sun.print.PrintJobFlavorException, and with DocFlavor.INPUT_STREAM.TEXT_PLAIN_US_ASCII just nothing happens on either printer, not even an error message.
    I am getting the impression that nobody of the developers ever cared for testing such a simple thing as a plain ASCII file under Windows.
    My printers I am using for the tests are
    1. an old parallel-attached HP Laserjet 4 Plus and
    2. a borrowed USB-attached Samsung CLP-510 color laser

  • Cannot print on a USB printer with Java PrintService in Windows 2000

    I want to print a simple ASCII file on a USB printer under Win2K and
    found 2 problems. Here is the 2nd one.
    Even though
    PrintServiceLookup.lookupPrintServices(<DocFlavor>, <AttributeSet>)
    returns nothing when I specify any AttributeSet, I can still print on the default printer, which I get by
    PrintService defServicedefService = PrintServiceLookup.lookupDefaultPrintService();
    but only on a parallel printer (HP Laserjet 4 Plus), either directly attached or over the network. When I try the very same code on a locally USB-attached printer, the printer is obviously reacting, but nothing is printed.
    In case of a Lexmark 730 (an ink printer), the driver even lies, it has printed 100%,
    on a Samsung CLP-510 (a color laser) the ready light flashes twice, that's all.
    The file to be printed has 5 lines and a "0C"X at the end in order to force a page eject at the end.
    I tried it with JDK 1.4.2 and with Java 6, same problem.
    I should mention that I can print that little file out of the editor Textpad without any problem.
    Edited by: eSchobbert on Jan 24, 2008 11:34 AM
    By mistake I entered the same message another time after modifying the Subject instead of replacing the message. Sorry for that. If I knew how to delete it, I would. So please just ignore this one

    Thanks a lot for the quick hint, but it did not help.
    I used BasicPrint.java and PrintJobWatcher.java of the samples for testing.
    Printing a GIF file works both to a parallel printer and a USB printer.
    Then I changed only the DocFlavor from DocFlavor.INPUT_STREAM.GIF to DocFlavor.INPUT_STREAM.AUTOSENSE, and that one works with my parallel printer, but on the USB printer the Ready light flashes twice, that's all.
    The ASCII file must have a "0C"X at the end to force a page eject on the parallel printer
    Maybe it is a matter of the DocFlavor, but which one?
    DocFlavor("application/octet-stream", "java.io.InputStream") works like DocFlavor.INPUT_STREAM.AUTOSENSE.
    DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST and DocFlavor.INPUT_STREAM.TEXT_HTML_US_ASCII cause sun.print.PrintJobFlavorException, and with DocFlavor.INPUT_STREAM.TEXT_PLAIN_US_ASCII just nothing happens on either printer, not even an error message.
    I am getting the impression that nobody of the developers ever cared for testing such a simple thing as a plain ASCII file under Windows.
    My printers I am using for the tests are
    1. an old parallel-attached HP Laserjet 4 Plus and
    2. a borrowed USB-attached Samsung CLP-510 color laser

  • Printing with Java

    Well, this is the third time I post this message, but I don't seem to find anyone interested in it, so I decided to put on some dukes... :-)
    I have never printed directly with Java, I have rather created pdf files or web-pages. However, Now I might be needing to print directly with Java (I will be using a ticket-printer), so I have done some investigation but I don't seem to find anything that I really like.
    I will be printing tickets, so I might use a simple image and text, only text, no special formatting or something. Any experiences, recommendations?
    Regards.

    This code could be useful for you. I don't know if it is platform independent:
    PrintingUtilities class
    import java.awt.*;
    import javax.swing.*;
    import java.awt.print.*;
    /** A simple utility class that lets you very simply print
    *  an arbitrary component. Just pass the component to the
    *  PrintUtilities.printComponent. The component you want to
    *  print doesn't need a print method and doesn't have to
    *  implement any interface or do anything special at all.
    *  <P>
    *  If you are going to be printing many times, it is marginally more
    *  efficient to first do the following:
    *  <PRE>
    *    PrintUtilities printHelper = new PrintUtilities(theComponent);
    *  </PRE>
    *  then later do printHelper.print(). But this is a very tiny
    *  difference, so in most cases just do the simpler
    *  PrintUtilities.printComponent(componentToBePrinted).
    *  7/99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
    *  May be freely used or adapted.
    public class PrintUtilities implements Printable {
      private Component componentToBePrinted;
    /*  public static void printComponent(Component c, int device) {
        new PrintUtilities(c).print();
      public PrintUtilities(Component componentToBePrinted) {
        this.componentToBePrinted = componentToBePrinted;
      public void print(int device) {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        printJob.setPrintable(this);
        //if(printJob.printDialog())
         try {
            printJob.setPrintService(printJob.lookupPrintServices()[device]);
            printJob.setCopies(1);
            printJob.print();
            //System.out.println(printJob.getPrintService());
          } catch(PrinterException pe) {
            System.out.println("Error printing: " + pe);
      public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
        if (pageIndex > 0) {
          return(NO_SUCH_PAGE);
        } else {
          Graphics2D g2d = (Graphics2D)g;
          g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
          disableDoubleBuffering(componentToBePrinted);
          componentToBePrinted.paint(g2d);
          enableDoubleBuffering(componentToBePrinted);
          return(PAGE_EXISTS);
      /** The speed and quality of printing suffers dramatically if
       *  any of the containers have double buffering turned on.
       *  So this turns if off globally.
      public static void disableDoubleBuffering(Component c) {
        RepaintManager currentManager = RepaintManager.currentManager(c);
        currentManager.setDoubleBufferingEnabled(false);
      /** Re-enables double buffering globally. */
      public static void enableDoubleBuffering(Component c) {
        RepaintManager currentManager = RepaintManager.currentManager(c);
        currentManager.setDoubleBufferingEnabled(true);
    An Example of use:
    import java.awt.*;
    import java.awt.print.*;
    import java.io.*;
    import javax.swing.*;
    public class PrintExample {
      public static void main(String[] args) {
        JFrame F = new JFrame() {
             public void paint(Graphics g) {
                  g.drawString("Printing a simple line!",20,20);
                  g.drawString("I hope this was useful",20,40);
        PrintUtilities printing = new PrintUtilities(F);
        Object[] array = PrinterJob.getPrinterJob().lookupPrintServices();
        for (int i=0; i<array.length;i++) System.out.println(i+" : "+array);
    System.out.print("Choose Printer device#: ");
    int device = askInteger();
    printing.print(device);
    private static int askInteger() {
         InputStreamReader ip = new InputStreamReader(System.in);
         BufferedReader br = new BufferedReader(ip);
         try {
         while(!false) {
         String read = br.readLine();
         try {
         return Integer.parseInt(read);
              } catch (NumberFormatException ex) {
              System.out.print("Write an integer please > ");
         } catch (IOException exception) {
         return -1;
    Some problems appears when I'm printing pictures.
    Please post a message if you figure out what to do.

  • Reports/print with java

    How to create reports with Java?
    where can I find javabeans to create reports with JBuilder?
    Thanks.

    JFreeReport is an open source report generator:
    http://www.object-refinery.com/jfreereport/index.html
    It's not a Javabean though.
    Another open source option is JasperReport (there's a link on the JFreeReport page), also (I think) not a Javabean.
    Hope this helps,
    Dave Gilbert
    www.object-refinery.com

  • Send a txt file to the printer with java

    Hello,
    I want to sent a txt file to the printer in java. I' am using the USB port. I can try to setup a dos command (print c:\test.txt), but I don't know the exactly command.
    There is also another solution in java I thing but I don't know the code!
    Can somebody give me the exact command for DOS and/or the code I have to write in java?
    THANX!!!!

    in dos imo you are not able to use usb-port. if your printer would be on lpt you could use javacomm ( http://java.sun.com/products/javacomm/index.html ). there is also another thread talking about this (print text to lpt). for your usb-problem i don't have a solution. but you could use JNI (java native interface). write code in c, compile to dll and use it in java.
    tobias
    ps: the other thread ( http://forum.java.sun.com/thread.jsp?forum=31&thread=224460 )

  • Printing with java J9

    Hi
    I just installed java J9 on my brand new hp iPAQ 4240 (windows mobile 5.0). The install file I used was: ibm-weme-wm50-arm-ppro11_6.1.0.20060727-102926.exe. And it works fine. Im going to develop an application that requires printing. Is there a java class for printing or?

    I think you can print from an applet, so long as it's a signed applet, but the problem is I think it will only print the applet or whatever you write to it. It's not likely to print the browser's contents. Anyway, I think that the browser has some settings regarding how things are printed, for example to not print a black page with white text, but print it in reverse, since it'll drain the printer's ink.

  • Using printer with java

    Hi all,
    I want to take print out of a text file from java. Do we hava any class or any way through which we can do this.
    Thanks in advance
    Regards,
    Arunkumar

    Look at the javax.print package.
    Here's a tutorial link you'll want, too:
    http://www-106.ibm.com/developerworks/java/library/j-mer0322/
    MOD

  • Printing with java technology

    i am using paper size 8.5 x 11 and i need a very small margin in left side of the paper....i am not able to reduce the left-margin of the print paper...
    if i am using x and y coordinates less then (0,0) then its missing some of the text....
    help plz.
    thank you

    See Paper.setImageableArea(...)
    http://java.sun.com/j2se/1.5.0/docs/api/java/awt/print/Paper.html#setImageableArea(double, double, double, double)

  • Printing with java app

    Hi all, I'm making a program for printing on continuous paper.
    I have 2 steps to my program so far and issues in all of them:
    1) Creating the print job, setting the paper size, margins, sending to printer
    2) I'm printing to a file, so I can remove the last 4 byets of the file that make the printer scroll the page out.
    The issues in the first part is that I can't seem to start printing at the top of the page (it always scrolls down like 2 inches). Changing the imageable area and margins don't seem to help with that or I just don't understand them. If I set margins to 0, it's just clipping the text and not printing it out properly. I also can't print more than 1 page size (height-wise), which sucks, considering I'm printing on continious paper.
        @Override
        public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
            if (page > 0) {
                return NO_SUCH_PAGE;
            Paper paper = new Paper();
            paper.setSize(612, 10000);
            double margin = 72;
            paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight()
            - margin * 2);
            pf.setPaper(paper);
            Graphics2D g2d = (Graphics2D)g;
            g2d.translate(pf.getImageableX(), pf.getImageableY());
            Font big = new Font("Times New Roman", Font.PLAIN, 12);
            Font small = new Font("Times New Roman", Font.PLAIN, 8);
            int Xlocation = 20;
            int y = 20;
            int count = 20;
            int i = 1;
            while (i < count){
                g2d.setFont(small);
                g.drawString("1", Xlocation , y);
                y += 8;
                g.drawString("2", Xlocation , y);
                y+=12;
                g2d.setFont(big);
                g.drawString("3", Xlocation , y);
                y += 8;
                g2d.setFont(small);
                g.drawString("4", Xlocation , y);
                y += 20;
                i++;
            // tell the caller that this page is part of the printed document
            return PAGE_EXISTS;
        }In the second part I'm removing the last 4 bytes of the file and adding a new line so it wouldn't scroll the page out after it is done printing. This works fine.
    I would like it to not prompt the user for file name, but give it a default name of something like "print.prn" and then run command prompt and enter command "copy /b print1.prn lpt1" which would send it to printer and then close command prompt. Is this possible?
    the second part:
            try {
                File target = new File(filename);
                RandomAccessFile file = new RandomAccessFile(target,"rw");
                file.setLength(target.length()-4); // Strip off the last 4bytes
                file.seek(target.length()); // set pointer to the end of file
                file.writeUTF("\n"); // add new line at the end of file
                file.close();
                Runtime rt = Runtime.getRuntime();
                rt.exec("cmd.exe /c start copy /b " +filename+ " lpt1");
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Printing.class.getName()).log(Level.SEVERE, null, ex);
            }  

    Anyone have any additional updates on this problem? We've just upgraded to JRE 1.6 update 10 and started seeing this on Windows XP machine. The printer that the users are trying to print to is a Lexmark T644 with a PS driver. I checked the Lexmark site and compared the driver on the users' machines with what is available on the Lexmark site. It appears that the users' machines has the latest version of the driver. I saw there was a bug open with Sun on the same type of problem on a non windows platform and related to having either an * or - in the printer name. I've verified that neither character is in the printer name in this instance. It also sounded as if on this bug that the problem could be reproduced every time. We're not seeing that either. Sometimes the printer accepts the work and other times is doesn't. Any insight on this.

  • How does the SDK and Reader handle printing with orientation?

    I have a document that I want to print, and it contains a portrait page on Page 1, and a landscape page on Page 2.  Acrobat Reader correctly prints these pages (with a single print job), and I'm assuming it's because of the auto-rotate option on the print dialog.  Is this a feature of Reader, the API, or how the printer handles the request.  I've tried printing this PDF just using the JAva API, but the orientation remain portrait for all pages.  How does the Adobe Acrobat API deal with mixed orientation pages?  Ignore the fact that I did my test printing with Java, because my question is whether or not the auto orientation is a feature of the UI, the SDK, or the printer (or a combination).

    Same as the current Ipod Video or "Classic" there is an option under settings to toggle between original aspect ratio or Full Screen.
    Kenal0

  • How to print with a dot matrix printer?

    can anyone tell me how to print a document with a dot-matrix printer with java?

    try this...
    String printerUNC = "lpt1:";
    String report = "This is the text to print"
    try{
    FileWriter fw = new FileWriter(printerUNC);
    PrintWriter pw = new PrintWriter(fw);
    pw.println(report);//"This is the test report");
    pw.flush();
    fw.close();
    }catch(Exception e){
    System.out.println("Error during print");
    System.out.println(e);

  • Problem in printing pdf document with java code

    Hi All
    I want to print a pdf document with java code i have used PDFRenderer.jar to compile my code.
    Code:
    File f = new File("C:/Documents and Settings/123/Desktop/1241422767.pdf");
    FileInputStream fis = new FileInputStream(f);
    FileChannel fc = fis.getChannel();
    ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    PDFFile pdfFile = new PDFFile(bb); // Create PDF Print Page
    PDFPrintPage pages = new PDFPrintPage(pdfFile);
    // Create Print Job
    PrinterJob pjob = PrinterJob.getPrinterJob();
    PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
    pjob.setJobName(f.getName());
    Book book = new Book();
    book.append(pages, pf, pdfFile.getNumPages());
    pjob.setPageable(book);
    // System.out.println(pjob.getPrintService());
    // Send print job to default printer
    pjob.print();
    but when i am running my program i am getting error
    Exception in thread "main" java.awt.print.PrinterException: Invalid name of PrintService.
    Please anybody, knows the solution for this error?
    Thanks In Advance
    Indira

    It seems that either there is no default printer setup or you have too many printers or no printer setup at all. Try running the following code. It should print the list of available print services.
    import java.awt.print.*;
    import javax.print.*;
    public class PrintServiceNames{
         public static void main(String args[]) throws Exception {
              PrintService[] printServices = PrinterJob.lookupPrintServices();
              int i;
              for (i = 0; i < printServices.length; i++) {
                   System.out.println("P: " + printServices);
    }From the list pick one of the print service names and set it explicitly like "printerJob.setPrintService(printServices);" and then try running the program.

  • Problem with Java Print API ( awt ) : Migrated from 1.1 to 1.4

    We had migrated an application ( applet based) from Java 1.1.8 to java 1.4.
    We use the java.awt.print apis for printing data from our applications.
    There is difference in print quality after the migration. For e.g. if the data frame had a scroll bar when taken print out, the scroll bar was not visible in java1.1 version application but is being printed with java1.4 version.
    We are unable to find the cause of this problem. Any help in this regard would be of great help.
    Thanks in advance.

    Hi,
    thanks, this solved the problem.
    Thank you.
    Best regards,
    Peter

  • Printing local with JAVA Gui 7.10 on a MAC

    Hi,
    I installed the JAVA GUI 7.10 on my MacBook Pro and it all works fine. The only problem I have is local printing. Since I do not have SAPLPD anymore, I am not able to use local printing. Is there another possibility to do local printing with a Mac?
    Thanks Maarten

    Maarten,
    Yes.
    The following text is from Chapter 2. "Release Notes" / Section 2. "Missing Features" of the JavaGui HTML documentation:
    Front-End Printing Using SAPLPD (access method 'F') is only supported on Win32 platforms. For newer back-end versions, a new printing architecture for all platforms supported by SAP GUI for Java (access method 'G') is available, see SAP Note 821519 for details.

Maybe you are looking for