1. Print the example
The most important thing in Java printing is the starting coordinate (x, y) in each plotting 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 the image and text ** @ Author WFG **/public class printsalesslip implements printable {/*** // *** @ Param graphic indicates the graphic environment for printing * @ Param * pageformat indicates the format of the printed page (The page size is measured by points, 1 point is 1/72 of 1 Talent, 1 inch is 25.4mm. A4 paper is roughly 595 × 842 points) * @ Param pageindex indicates the page number (80mm: 226.7, 58mm: 164.4) */Public int print (Graphics gra, pageformat PF, int pageindex) throws printerexception {component C = NULL; // print string // convert to graphics2dgraphics2d g2 = (graphics2d) GRA; // set the print color to black g2.setcolor (color. black); // print the Starting Point Coordinate Double X = PF. getimageablex (); // returns the X coordinate of the paper object related to this pageformat to the point in the upper left corner of the image area. Double Y = PF. getimageabley (); Switch (pageindex) {Case 0: // set the print font (font name, style, and vertex size) (the font name can be a physical or logical name) // five types of fonts defined by the Java platform: Serif, sansserif, monospaced, dialog, and dialoginputfont font = new font ("", Font. plain, 12); g2.setfont (font); // set the font // basicstroke bs_3 = new basicstroke (0.5f); float [] dash1 = {2.0f }; // set the print line attributes. // 1. line width 2, 3, unknown, 4, blank width, 5, dotted line 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 the float fontheigth1 = font. getsize2d (); //-1-use graphics2d to directly output // The baseline with the first character (lower right) located at the (x, y) position in the user space // g2.drawline (10, 10, 200,300); string str1 = "xxxxxxxxxxxxxxxx"; g2.drawstring (str1, (INT) x, (INT) Y + fontheigth1); string str2 = "139"; g2.drawst Ring (str2, (INT) x, (INT) Y + fontheigth1 * 2 + 10); font font2 = new font ("", Font. plain, 9); g2.setfont (font2); // set the font g2.setstroke (New basicstroke (0.5f, basicstroke. cap_butt, basicstroke. join_miter, 2.0f, dash1, 0.0f); float fontheigh2 = (INT) font. getsize2d (); int tempheight = (INT) (fontheigth1*2 + 10); string str3 = "Row-based row-Based String"; g2.drawstring (str3, (INT) X, (INT) Y + fontheigth1 + 10 + tempheight); string Str 4 = "xxxxxxxxxxxxxx "; g2.drawstring (str4, (INT) x, (INT) Y + fontheigh2 * 2 + 10 + tempheight); string str5 = "new row "; g2.drawstring (str5, (INT) x, (INT) Y + fontheigh2 * 3 + 10 + tempheight); string str6 = "likes to rest"; g2.drawstring (str6, (INT) x, (INT) Y + fontheigh2 * 4 + 20 + tempheight); string str7 = "XXXXXXXX"; g2.drawstring (str7, (INT) x, (INT) Y + fontheigh2 * 5 + 20 + tempheight); string str8 = "xxxxxxxxxxxxxxx"; G 2. drawstring (str8, (INT) x, (INT) Y + fontheigh2 * 6 + 20 + tempheight); image src = toolkit. getdefatooltoolkit (). getimage ("F:/barcode2.jpg"); g2.drawimage (SRC, (INT) x, (INT) (Y + fontheigh2 * 6 + 50 + tempheight + 10, c); // print the barcode int img_height = SRC. getheight (c); int img_width = SRC. getwidth (c); Return page_exists; default: Return no_such_page;} public static void main (string [] ARGs) {// generally, this is a book or book. = New Book (); // set it to pageformat pF = new pageformat (); PF. setorientation (pageformat. portrait);/* Static int landscape origin is located at the bottom left of the paper, X direction from bottom to top, y direction from left to right. The static int portrait origin point is in the upper left corner of the paper, X points to the right, and y points to the bottom. The static int reverse_landscape origin is located in the upper-right corner of the paper. The X direction is from top to bottom, and the Y direction is from right to left. * /// (The page size is measured by points. The page size ranges from 1 to 1 inch, and the page size ranges from 1/72 to 1 inch. A4 paper is about 595 × 842 points) // you can use paper to set the blank margin and printable area of the page. It must be consistent with the actual size of the printed paper. Paper P = new paper (); // system. out. println ("1 high:" + P. getheight () + "-width:" + P. getwidth (); p. setsize (227,300); // specifies the width and height of the paper object. It indicates the property P of the page to be printed. setimageablearea (10, 10,227,300); // you can specify the image area of the paper. // A4 (595x842) sets the printing area. In fact, 0 and 0 should be, because the default X and Y margin of A4 paper are 72system. out. println ("2 high:" + P. getheight () + "-width:" + P. getwidth (); PF. setpaper (p); // Add pageformat and printable to the book to form a page book. append (New printsalesslip (), Pf); // gets the print service object printerjob job = printerjob. getprinterjob (); // sets the print class job. setpageable (book); try {// you can use printdialog to display the Print dialog box and print it after the user confirms it. You can also print Boolean A = job directly. printdialog (); if (a) {// system. out. println (a); job. print () ;}} catch (exception e) {e. printstacktrace ();}}}
2. Print string line breaks
Idea: Determine whether to wrap the line by pixel. If you wrap the line by string length, there will be a problem between English and man. The width is different.
Int strpixelwidth = metrics. stringwidth (STR); // String Length (in pixels) int linesize = (INT) math. ceil (strpixelwidth * 1.0/width); // number of rows required // fontmetrics metrics = new fontmetrics (font) {}; // rectangle2d bounds = metrics. getstringbounds (STR, null); // int widthinpixels = (INT) bounds. getwidth (); // system. out. println (strpixelwidth + "---:"); If (width <strpixelwidth) {// The page width (width) is smaller than the string length linenum = (INT) math. ceil (strpixelwidth * 1.0/linesize); // calculate the number of rows stringbuilder sb = new stringbuilder (); // store the string of each row Int J = 0; int tempstart = 0; string tempstrs [] = new string [linesize]; // stores the string for (INT I1 = 0; I1 <Str. length (); I1 ++) {char CH = Str. 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) {// the last row of tempstrs [J] = Str. substring (tempstart );}}