1. Printing examples
The key to Java printing is the starting coordinates (X,Y) of each drawing function g2.drawstring (str, x, y)
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.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; /** * Print pictures and text * * @author WFG */public class Printsalesslip implements printable {/**//** * @param Gra Phic indicates the printed graphics environment * @param * Pageformat indicates the printed page format (the page size is measured in dots, with 1 points being 1/72 for 1 of the elite, and 1 inches for 25.4 millimeters). A4 paper is approximately 595x842 point) * @param pageindex specified page number (80 mm: 226.7 point, 58 mm: 164.4) * * public int print (Graphics gra, Pageformat pf, int
PageIndex) throws PrinterException {Component c = null;
Print string//convert to graphics2d graphics2d g2 = (graphics2d) GRA;
Set the print color to Black g2.setcolor (Color.Black); Print start coordinate double x = Pf.getimageablex ()//To return the imageable region of the Paper object associated with this PageformatThe x-coordinate of the upper-left point.
Double y = Pf.getimageabley (); Switch (pageIndex) {case 0://Set the print font (font name, style and point size) (font name can be physical or logical name)//Five font families defined by the Java platform: Serif, Sansserif, monospace
D, Dialog and dialoginput font font = new Font ("Song Body", Font.plain, 12);
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_mi
TER, 2.0f, Dash1, 0.0f);
G2.setstroke (Bs_3)//Set line width float fontHeigth1 = font.getsize2d ();
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);
String str1 = "xxxxxxxxxxxxxxxxxx";
G2.drawstring (str1, (int) x, (int) y + fontHeigth1);
String str2 = "139";
G2.drawstring (str2, (int) x, (int) y + fontheigth1*2+10);
Font font2 = new Font ("Song Body", Font.plain, 9); G2.setfont (Font2)//Set Font G2.setstroke (new Basicstroke (0.5f, Basicstroke.cap_buTT, Basicstroke.join_miter, 2.0f, Dash1, 0.0f);
float fontHeigth2 = (int) font.getsize2d ();
int tempheight = (int) (FONTHEIGTH1*2+10);
String STR3 = "On-line, row-and-row sympathetic";
G2.drawstring (STR3, (int) x, (int) y + fontheigth2+10 +tempheight);
String STR4 = "Xxxxxxxxxxxxxx Hee XI XI";
G2.drawstring (STR4, (int) x, (int) y + fontheigth2*2+10 +tempheight);
String STR5 = "New line row row row line";
G2.drawstring (STR5, (int) x, (int) y + fontheigth2*3+10 +tempheight);
String STR6 = "to rest and rest";
G2.drawstring (STR6, (int) x, (int) y + fontheigth2*4+20 +tempheight);
String STR7 = "xxxxxxxx";
G2.drawstring (STR7, (int) x, (int) y + fontheigth2*5+20 +tempheight);
String str8 = "Xxxxxxxxxxxxxxxxx";
G2.drawstring (str8, (int) x, (int) y + fontheigth2*6+20 +tempheight);
Image src = Toolkit.getdefaulttoolkit (). GetImage ("f:/barcode2.jpg"); G2.drawimage (SRC, (int) x, (int) (Y + fontheigth2*6+50 +tempheight+10), 150,70, c);//print barcode int img_height = Src.getheigh
T (c); int Img_width= Src.getwidth (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);
The/*static Int landscape origin is located at the lower left of the paper, the direction of X is from bottom to top, and Y is from left to right.
The static int portrait The origin at the top left of the paper, X points to the right, and y points below. The static int Reverse_landscape origin is at the top right of the paper, and the direction of X is from top to bottom, and Y is from right to left. *///(page size is measured in dots, 1 points are 1 inches 1/72, 1 inches is 25.4 mm.) A4 paper is approximately 595x842 point)//Set the blank margins and printable area of the page by paper.
Must match the actual printed paper size.
Paper p = new Paper ();
System.out.println ("1 High:" +p.getheight () + "-Wide:" +p.getwidth ());
P.setsize (227, 300);//paper size sets the width and height of this Paper object, which indicates the properties of the page to be printed (10, 10, 227, 300);//sets the imageable area for this Paper.
A4 (595 X 842) Sets the print area, in fact the 0,0 should be 72, 72, because A4 paper's default x,y margin is the System.out.println ("2 High:" +p.getheight () + "-Wide:" +p.getwidth ());
Pf.setpaper (P); Add Pageformat and printable to the book to form a page book.append (new Printsalesslip (), PF);
Get Print Service object PrinterJob job = Printerjob.getprinterjob ();
Sets the Print class job.setpageable (book);
try {//You can display the print dialog with PrintDialog, print after the user confirms, or you can print a Boolean a=job.printdialog () directly;
if (a) {//system.out.println (a);
Job.print ();
} catch (Exception e) {e.printstacktrace ();
}
}
}
2. String wrapping in Print
Train of thought: according to the pixel to determine whether to change lines, according to string length line words will have English and man problem, width is different.
int strpixelwidth = metrics.stringwidth (str); String length (pixels) str the string int linesize = (int) Math.ceil (strpixelwidth*1.0/width) to be printed;//How many rows//fontmetrics metrics = n
EW FontMetrics (font) {};
Rectangle2D bounds = metrics.getstringbounds (str, NULL);
int widthinpixels = (int) bounds.getwidth ();
System.out.println (strpixelwidth+ "---:");
if (Width < strpixelwidth) {//page width (width) is less than the length of the string linenum = (int) math.ceil (strpixelwidth*1.0/linesize);
StringBuilder SB =new StringBuilder ();//store string int j=0 for each row;
int tempstart = 0; String tempstrs[] =new string[linesize];//for each row after the store is wrapped for (int i1 =0;i1<str.length (); i1++) {Char ch = s
Tr.charat (I1);
Sb.append (CH);
Rectangle2D bounds2 = Metrics.getstringbounds (sb.tostring (), NULL);
int tempstrpi1exlwi1dth = (int) bounds2.getwidth (); if (Tempstrpi1exlwi1dth > width) {tempstrs[j++] = str.substring (TempstarT,I1);
Tempstart = I1;
Sb.delete (0, Sb.length ());
Sb.append (CH);
} if (I1 = = Str.length ()-1) {//last line tempstrs[j] = str.substring (Tempstart); }
}