JAVA Call System printer print string __java

Source: Internet
Author: User
Tags print object

With the Java implementation of printing, java.awt provides some of the printing API, to achieve printing, first to obtain the print object, and then inherit printable implementation interface method print, so that the printer to print, and finally with graphics2d direct output directly.
The following code implements a simple printing function:


Import Java.awt.BasicStroke;
Import Java.awt.Color;
Import java.awt.Component;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;

Import Java.awt.Image;
Import Java.awt.Toolkit;
Import java.awt.RenderingHints;
Import Java.awt.font.FontRenderContext;
Import Java.awt.font.LineBreakMeasurer;
Import Java.awt.font.TextAttribute;
Import Java.awt.font.TextLayout;
Import Java.awt.geom.Point2D;
Import Java.awt.image.BufferedImage;
Import Java.awt.print.Book;
Import Java.awt.print.PageFormat;
Import Java.awt.print.Paper;
Import java.awt.print.Printable;
Import java.awt.print.PrinterException;
Import Java.awt.print.PrinterJob;

Import java.text.AttributedString;



Import Javax.swing.JApplet; public class Printtest implements printable{/** * @param graphic indicates the printed graphics environment * @param pageformat indicates the printed page format (page size is measured by dots) Unit, 1 points for 1 talents of 1/72, 1 inches to 25.4 mm. A4 paper is approximately 595x842 point) * @param pageIndex indicates page number **/public int print (Graphics gra, Pageformat pf, int pageIndex) throws Pr Interexception {System.out.println ("pageindex=" +pageindex);
      Component c = null; Print string String str = "The Chinese nation is a great nation of diligence, courage and wisdom."
      ";
      Convert to graphics2d graphics2d g2 = (graphics2d) GRA;

      Set the print color to Black g2.setcolor (Color.Black);
      Print start coordinates double x = Pf.getimageablex ();
       
      Double y = Pf.getimageabley (); Switch (pageIndex) {case 0://Set the print font (font name, style and point size) (the font name can be physical or logical name) The five font families defined by the//java platform: Serif,
           Sansserif, monospaced, Dialog and dialoginput font font = new Font ("Neo-Arial", Font.plain, 9);   
           G2.setfont (font),//Setting Fonts//basicstroke bs_3=new Basicstroke (0.5f); 
           Float[] Dash1 = {2.0f};
           Sets the properties of the print line. 1. Line width 2, 3, do not know, 4, blank width, 5, dashed width, 6, offset g2.setstroke (new Basicstroke (0.5f, Basicstroke.cap_butt, Basicstroke .  
           Join_miter, 2.0f, Dash1, 0.0f); G2.setstroke (Bs_3)//Set line width float heigth = font.getsize2d ();/font height System.out.println ("x=" +x); 
           
           The baseline (lower right) of the graphics2d Direct output//first character is located in the user space at (x, y) position//g2.drawline (10,10,200,300); 
           Image src = Toolkit.getdefaulttoolkit (). GetImage ("d:\\eclipseworkspace3.1\\kfc-wuxi\\webroot\\image\\kfc.jpg");
           G2.drawimage (SRC, (int) x, (int) y,c);
           int Img_height=src.getheight (c);
           int Img_width=src.getwidth (c);
           
           System.out.println ("img_height=" +img_height+ "img_width=" +img_width);
           G2.drawstring (str, (float) x, (float) y+1*heigth+img_height);
           
           G2.drawline ((int) x, (int) (Y+1*HEIGTH+IMG_HEIGHT+10), (int) x+200, (int) (Y+1*HEIGTH+IMG_HEIGHT+10));
           
         G2.drawimage (SRC, (int) x, (int) (Y+1*HEIGTH+IMG_HEIGHT+11), c);
         return page_exists;
      Default:return No_such_page;
    } public static void Main (string[] args) {//popular understanding is the book, the Document books = new (); //    Set to vertical hit Pageformat pf = new Pageformat ();
    Pf.setorientation (pageformat.portrait); Sets the blank margin and printable area of the page by paper.
    Must match the actual printed paper size.
    Paper p = new Paper ();
    P.setsize (590,840);//paper size P.setimageablearea (10,10, 590,840),//A4 (595 X 842) Set the print area, in fact, 0,0 should be 72, 72, because A4 paper's default x,y margin is 72
    Pf.setpaper (P);

     Add Pageformat and printable to the book to form a page book.append (new Printtest (), PF);      
     Get Print Service object PrinterJob job = Printerjob.getprinterjob ();
     
     Sets the Print class job.setpageable (book);
         try {//can display the print dialog with PrintDialog, print after the user confirms, or directly print//boolean a=job.printdialog ();
         if (a)//{job.print ();
     } catch (PrinterException e) {e.printstacktrace (); }
   }
}


This example implements the print string, line (including dashed lines), and prints the picture. And through the paper Setimageablearea can set the printing area and margin, let the developer arbitrarily set the location of printing.
The following print code does not set the print area, defaults to the area and margins of the printed paper, such as the A4 paper we normally use, and the print start x and y coordinates are 72, 72.
No locale code:

Import Java.awt.BasicStroke;
Import Java.awt.Color;
Import java.awt.Component;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;

Import Java.awt.Image;
Import Java.awt.Toolkit;
Import java.awt.RenderingHints;
Import Java.awt.font.FontRenderContext;
Import Java.awt.font.LineBreakMeasurer;
Import Java.awt.font.TextAttribute;
Import Java.awt.font.TextLayout;
Import Java.awt.geom.Point2D;
Import Java.awt.image.BufferedImage;
Import Java.awt.print.Book;
Import Java.awt.print.PageFormat;
Import Java.awt.print.Paper;
Import java.awt.print.Printable;
Import java.awt.print.PrinterException;
Import Java.awt.print.PrinterJob;

Import java.text.AttributedString;



Import Javax.swing.JApplet; public class PrintTest1 implements printable{/** * @param graphic indicates the printed graphics environment * @param pageformat indicates the printed page format (page size is measured in points Unit of measure, 1 points for 1 talents of 1/72, 1 inches to 25.4 mm. A4 paper is approximately 595x842 point) * @param pageIndex indicates page number **/public int print (Graphics gra, Pageformat pf, int pageIndex) throws Pr InterexceptiOn {System.out.println ("pageindex=" +pageindex);
      Component c = null; Print string String str = "The Chinese nation is a great nation of diligence, courage and wisdom."
      ";
      Convert to graphics2d graphics2d g2 = (graphics2d) GRA;
      
      Set the print color to Black g2.setcolor (Color.Black);  /*paper Paper = Pf.getpaper ()//page format paper paper.setsize (500,500);//paper size Paper.setimageablearea (0,0,500,500);
      Set the size of the print area System.out.println (Paper.getwidth ()); 
      System.out.println (Paper.getheight ());
      Pf.setpaper (paper);//The paper as a format///print start coordinates double x = Pf.getimageablex ();
       
      Double y = Pf.getimageabley (); Switch (pageIndex) {case 0://Set the print font (font name, style and point size) (the font name can be physical or logical name) The five font families defined by the//java platform: Serif,
           Sansserif, monospaced, Dialog and dialoginput font font = new Font ("Neo-Arial", Font.plain, 9);  
           G2.setfont (font),//Setting Fonts//basicstroke bs_3=new Basicstroke (0.5f); 
   Float[] Dash1 = {4.0f};        G2.setstroke (New Basicstroke (0.5f, Basicstroke.cap_butt, Basicstroke.join_miter, 4.0f, Dash1, 0.0f));
           float heigth = font.getsize2d ();//font height System.out.println ("x=" +x); Complete text rendering using antialiasing mode/*g2.setrenderinghint (renderinghints.key_text_antialiasing, Renderinghints.value_text _ANTIALIAS_ON); The baseline (lower right) of the graphics2d Direct output//first character is located in the user space (x, y) position//g  
           
           2.drawLine (10,10,200,10);
           Image src = Toolkit.getdefaulttoolkit (). GetImage ("D://logo.gif");
           G2.drawimage (SRC, (int) x, (int) y,c);
           int Img_height=src.getheight (c);
           int Img_width=src.getwidth (c);
           
           System.out.println ("img_height=" +img_height+ "img_width=" +img_width);
           G2.drawstring (str, (float) x, (float) y+1*heigth+img_height);
           G2.drawline ((int) x, (int) (Y+1*HEIGTH+IMG_HEIGHT+10), (int) x+200, (int) (Y+1*HEIGTH+IMG_HEIGHT+10));
           G2.drawimage (SRC, (int) x, (int) (Y+1*HEIGTH+IMG_HEIGHT+11), c);
           Direct structure TextLayout print/*fontrendercontext FRC = G2.getfontrendercontext ();
           TextLayout layout = new TextLayout (str, font, FRC); Layout.draw (G2, (float) x, (float) y+2*heigth);///I print with LineBreakMeasurer/*attributedstring Tex
           t = new attributedstring (str);
           Text.addattribute (Textattribute.font, FONT);
           LineBreakMeasurer linebreaker = new LineBreakMeasurer (Text.getiterator (), FRC);
           Each line of characters shows length (dots) Double width = pf.getimageablewidth ();
           The baseline of the first character is located at (x, y) position in user space point2d.double pen = new Point2d.double (y+3*heigth); while ((Layout = Linebreaker.nextlayout ((float) width))!= null) {Layout.draw (G2, (float) x, (float) pen.y)
              ;
           PEN.Y + + layout.getascent ();
         }*/return page_exists; Default:return No_such_page; } public static void Main (string[] args) {//Get Print Service object PrinterJob job = Printerjob.getprinterj     
     OB ();
         Pageformat Pageformat = Job.defaultpage ()//Get default page format job.setprintable (new PrintTest1 ());/Set Print class try {
         You can use PrintDialog to display the Print dialog box, print after the user confirms, or directly print//boolean a=job.printdialog ();
         if (a)//{job.print ();
     } catch (PrinterException e) {e.printstacktrace (); }
   }
}




Related Article

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.