java Specifies the printer name for printing

Source: Internet
Author: User

Import Java.awt.print.PrinterJob;

Import Java.awt.print.Book;

Import java.awt.print.Printable;

Import Java.awt.Graphics;

Import Java.awt.print.PageFormat;

Import java.awt.print.PrinterException;

Import Javax.print.PrintService;

Import Javax.print.PrintServiceLookup;

Import Java.awt.Graphics2D;

Import Java.awt.Color;

Import Java.awt.Font;

Import Javax.print.attribute.HashAttributeSet;



Import Javax.print.attribute.standard.PrinterName; public class Myprinttest implements printable {public int print (Graphics Graphics, Pageformat pageformat, int pa

    Geindex) throws PrinterException {graphics2d g2 = (graphics2d) graphics;

    G2.setpaint (Color.Black);

    G2.translate (Pageformat.getimageablex (), Pageformat.getimageabley ());

    Font font = new Font ("Song Body", Font.Bold, 10);

    G2.setfont (font);

    G2.drawstring ("Afasdfad", 10, 10);

    Font = new Font ("bold", Font.Italic, 18);

    G2.setfont (font);

    G2.drawstring ("Second line:", 10, 40); Return PriNtable.

  page_exists;

    public static void Main (string[] args) {myprinttest myprinttest = new Myprinttest ();

  Myprinttest.print ();

    public void print () {PrinterJob printerjob = Printerjob.getprinterjob ();

    Book book = new book ();

    Book.append (New Myprinttest (), Printerjob.defaultpage ());

    Printerjob.setpageable (book);

    Hashattributeset hs = new Hashattributeset ();

    String printername= "EPSON tm-u220 Receipt";

    Hs.add (New PrinterName (Printername,null));

    printservice[] pss = printservicelookup.lookupprintservices (null, HS);

      if (pss.length==0) {System.out.println ("Unable to find printer:" +printername);

    return;

      try {printerjob.setprintservice (pss[0]);

    Printerjob.print ();

    catch (PrinterException ex) {System.out.println (Ex.getmessage ());

 }

  }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.