Some time ago, a student ID card was printed as needed.
The requirement is very simple, that is, to print the specified content in the specified coordinate point.
In order to achieve this, we have done a lot of work, but fortunately, with the help of everyone, we finally achieved the desired function.
The Code is as follows:
Package yourpackage; </P> <p> Import Java. AWT. color; <br/> Import Java. AWT. font; <br/> Import Java. AWT. graphics; <br/> Import Java. AWT. graphics2d; <br/> Import Java. AWT. print. book; <br/> Import Java. AWT. print. pageformat; <br/> Import Java. AWT. print. paper; <br/> Import Java. AWT. print. printable; <br/> Import Java. AWT. print. printerexception; <br/> Import Java. AWT. print. printerjob; <br/> Import Java. util. list; </P> <P> Import javax. print. printservice; <br/> Import javax. print. printservicelookup; <br/> Import javax. print. attribute. hashattributeset; <br/> Import javax. print. attribute. standard. printername; </P> <p>/** <br/> * print the Service Program <br/> * @ author administrator <br/> */<br/> @ suppresswarnings ("unchecked ") <br/> public class printservicetest implements printable {</P> <p> private string printinfo; // print the content <br/> priv Ate int rowspace; // print the line spacing <br/> private int rownum = 0; // The number of printed rows starts from 0 <br/> private int [] regnums; // register the set of serial numbers and obtain the number of rows to be typed, and the y-axis coordinates of each line <br/>/** <br/> * the array below encapsulates an array object, and the array object encapsulates three child objects, A printed content <br/> * x coordinate of the printed content, and Y coordinate of the printed content. Note that the coordinates are measured in pixels (points) <br/> * point = 1/72 inch, 1 inch = 25.4 <br/> */<br/> private list infolist; <br/> private int printnum; // The number of printed copies starts from 0 </P> <p> Public int [] getregnums () {<br/> return regnums; <br />}</P> <p> public list getinfolist () {<br/> return infolist; <br/>}< br/> Public void setinfolist (list infolist) {<br/> This. infolist = infolist; <br/>}< br/> Public void setregnums (INT [] regnums) {<br/> This. regnums = regnums; <br/>}< br/> Public int getprintnum () {<br/> return printnum; <br/>}< br/> Public void setprintnum (INT printnum) {<br/> This. printnum = printnum; <br/>}< br/> Public int getrows PACE () {<br/> return rowspace; <br/>}< br/> Public void setrowspace (INT rowspace) {<br/> This. rowspace = rowspace; <br/>}< br/> Public String getprintinfo () {<br/> return printinfo; <br/>}< br/> Public void setprintinfo (string printinfo) {<br/> This. printinfo = printinfo; <br/>}< br/> Public int getrownum () {<br/> return rownum; <br/>}< br/> Public void setrownum (INT rownum) {<br/> This. rownum = rownum; <Br/>}< br/>/** <br/> * only the dead content is printed, but multiple contents can be organized according to the infolist above. <Br/> * Note: This method does not take the printing of a redundant page into account, that is, each printing will be on one page. <Br/> */<br/> Public int print (Graphics graphics, pageformat, int pageindex) <br/> throws printerexception {</P> <p> // print the specified number of copies <br/> If (this. getprintnum ()! = 0) {<br/> // The value of pageindex starts from scratch <br/> If (pageindex> (this. getprintnum ()-1) {<br/> return printable. no_such_page; <br/>}< br/>} else {<br/> // The value of pageindex starts from 0. A print copy has been set below, after a copy is printed, pageindex is increased by 1. Therefore, when the value is greater than 0, the first copy is completed. <br/> If (pageindex> 0) {<br/> return printable. no_such_page; <br/>}</P> <p> graphics2d g2 = (graphics2d) graphics; </P> <p> // g2.setpaint (color. black); // set the color <br/> g2.translate (pageformat. getimag Eablex (), pageformat. getimageabley (); </P> <p> Font font = new font ("", Font. bold, 11); <br/> g2.setfont (font); </P> <p> g2.drawstring ("----------------- The test print coordinate is: x = 50, y = 50 ---------", 50, 10); <br/> g2.drawstring ("the first line to be printed", 0, 20); </P> <p> return printable. page_exists; // only when this page exists, <br/>}</P> <p>/** <br/> * print method <br/> */<br/> Public void print () throws printerexception {</P> <p> printerjob = Printerjob. getprinterjob (); </P> <p> book = New Book (); </P> <p> pageformat pF = printerjob. defaultpage (); // get the default page format of printerjob. </P> <p> paper p = PF. getpaper (); // obtain the response page </P> <p> // system. out. println ("Page width" + P. getwidth () + "; Page Height:" + P. getheight (); </P> <p>/* <br/> * Note that the following settings remove the margin settings, do not perform this operation if you do not consider the influence of margins. <Br/> */<br/> P. setimageablearea (0, 0, P. getimageablewidth () + 70, p. getimageablewidth () + 70); </P> <p> // system. out. println ("Photo width" + P. getimageablewidth () + "; height of the image:" + P. getimageableheight (); </P> <p> PF. setpaper (p); </P> <p> // book. append (this, Pf); <br/> // printerjob. setpageable (book); </P> <p> // search for the default printservice in this environment <br/> printservice PS = printservicelookup. lookupdefaprintprintservice (); </P> <p> If (PS = NULL) {</P> <p> system. out. println ("unable to find printer:"); </P> <p> return; <br/>}< br/> printerjob. setprintservice (PS); </P> <p> // printerjob. pagedialog (PF); // set the attributes of the print page <br/> // printerjob. printdialog (); // set printer attributes </P> <p> printerjob. setcopies (1); // print only one copy <br/> printerjob. setprintable (this, Pf); <br/> printerjob. print (); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> printservicetest myprinttest = new printservicetest (); <br/> try {<br/> myprinttest. print (); <br/>}catch (printerexception e) {<br/> E. printstacktrace (); <br/>}</P> <p>/** <br/> * convert millimeters to DOTS (rounded up) <br/> * @ Param mm <br/> * @ return Points <br/> */<br/> Public static int returnpointno (INT mm) <br/>{< br/> return 72 * mm/25; <br/>}< br/>
The commented out part above can be released to view the effect. The default printer on the server used by this code can also be used as needed.
For more information about the methods, see printservicelookup.