運用環境の簡単な紹介:
言語:Java
ツール:日食
システム:Windows 7
(印刷デバイスにはまだありないため、プレビュー写真のみを提供できます)
最近、プロジェクトはモールのショッピングレシートを印刷する必要があります。日常生活では、スーパーマーケットに行って物を買います。チェックアウトすると、レジ係が領収書を印刷します。一般的に、モールにはこのような小さな機能も必要です。この記事に記載されているデモは、58mmサーマルプリンターの例です。他の紙のプリンターの場合は、紙の幅を調整してください。
パッケージテスト; java.awt。*;インポートJava.awt.print。*;/***プリンターテストクラス(58mm)* 1。ターゲットプリンターはデフォルトのプリンター* 2として設定する必要があります。印刷ページの幅は特定のプリンターに関連しています。通常、印刷用紙の幅であり、システムパラメーター* 3として構成する必要があります* 3。漢字の幅は約12ポイント*/public class printtest {public static void main(string [] args){if(printerjob.lookupprintservices()。コーナー、左から右、上から下まで、pageformat.setOrientation(pageformat.portrait);/* printページ形式設定*/紙紙= new Paper(); //印刷幅(固定、特定のプリンターに関連)と高さ(実際の印刷コンテンツに関連)を設定します。 135、450); pageformat.setpaper(Paper); //印刷ドキュメントの作成book book = new book(); book.append(new Printable(){@Override public int Print(Graphics Graphics、PageFormat PageFormat、Int PageIndex)スローPrinterException {if(pageindex> 0){return no_such_page;} graphic2dgraghtion font = new font( "安体"、font.plain、5); graphics2d.setfont(font); drawstring(graphics2d、 "//////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// 119、8); font = new font( "宋体"、font.plain、7); graphics2d.setfont(font); int yindex = 30; int lineheight = 10; int linewidth = 120; color defaultcolor = graphics2d.getcolor(); 「荷受人:通行人a」、10、yindex、linewidth、lineheight); yindex = drawstring(graphics2d、 "受信住所:バイドゥ・ビル、ナンバー10シャンディ・ストリート、ハイディア地区、北京"、10、yindex + lineheight、linewidth、lineheight); BasicsTroke.cap_butt、basicstroke.join_bevel、0、new float [] {4、4}、0); graphics2d.setstroke(stroke); graphics2d.drawrect(5、10、129、yindex); font.bold、8)); graphics2d.setcolor(defaultcolor); yindex = drawstring(graphics2d、 "北京薬局の小売チケット"、5、yindex + lineheight + 20、linewidth、12); graphics2d.setfont( "、font.plain、6); font.plain、6); DrawString(Graphics2d、 "operator:xiaoqing"、5、yindex + lineheight + 2、linewidth、lineheight); yindex = drawstring( "date:2017-01-05"、5 + linewidth/2、yindex、linewidth、lineheight); lineheight*2-5、linewidth、lineheight); yindex = drawstring(graphics2d、 "specification"、(linewidth/10)*4、yindex、linewidth、lineheight); yindex = drawstring(graphics2d、 "und price"、(linewidth/10)*8、yindex、lineheight、lineheight、lineheight drawstring(graphics2d、 "数量"、(linewidth/10)*10、yindex、linewidth、lineheight); forタブレット "、5、yindex + 15、(linewidth/10)*7、10); graphics2d.setfont(new font("安安 "、font.plain、6)); graphics2d.setcolor(灰色); yindex = drawstring(graphics2d、" 100 tablet/box); DrawString(Graphics2d、 "14.50"、(linewidth/10)*8、yindex、linewidth、lineheight); yindex = drawstring(graphics2d、 "2"、(linewidth/10)*10、yindex、linewidth、lineheight); yindex + 2; graphics2d.drawline(5、yindex、5 + linewidth、yindex);} graphics2d.setcolor(defaultcolor); yindex = drawstring(graphics2d、 "メンバー名:xiaoqing"、5、yindex + lineheight * 2、yindex2、yindex2、 " 5、yindex + lineheight、linewidth、lineheight); yindex = drawstring(graphics2d、 "合計:55.30"、5、yindex + lineheight + lineheight + lineheight + lieneheight + lieneheight + lineheight、linewidth、lineheight); DrawString(Graphics2d、 "Change:44.70"、5、yindex + lineheight、linewidth、lineheight); graphics2d.setfont(new Font( "宋体"、font.plain、6)); graphics2d.setcolor(灰色); yindex = drawstring(yind2d、 "5、020-12456" * 2、LineWidth、LineHeight); yindex = drawstring(graphics2d、 "dresdu:baidu Building、No。10Shangdi Street、Haidian District、Haidian District、Beijing"、5、yindex + Lineheight、linewidth、lineheight); yindex = yindex + 20; Graphics2d.drawline(yindex = yindex + 20; page_exists;}、pageformat); //デフォルトのプリンターPrinterjob printerjob = printerjob.getprinterjob(); printpageable(book); try {printerexcepte e)(printerexception e){e.printstacktrace(); out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.Out else {system.out.println( "印刷サービスは発見できません");}/*** string output* @param graphics2d brush* @param text print text* @param x print x coordinate* @param y print y print y coldinate* @param linewidth line width* @param line heightd(@return retring dock drater Graphics2d、string text、int x、int y、int linewidth、int lineheight){fontmetrics fontmetrics = graphics2d.getfontmetrics(); if(fontmetrics.stringwidth(text)<linewidth){graphics2d.drawstring(text、x、yot y; text.tochararray(); int charswidth = 0; stringbuffer sb = new Stringbuffer(); for(int i = 0; i <chars.length; i ++){if((charswidth+fontmetrics.charwidth(chars [i]))> linewidth){graph2d.drawstring(sb.tostring( y); sb.setlength(0); y = y + lineheight; charswidth = fontmetrics.charwidth(chars [i]); sb.append(chars [i]);} else {charswidth = charswidth + + + fontmetrics.charwidth(chars [i]); sb.append(chars [i]);}} if(sb.length()> 0){graphics2d.drawstring(sb.tostring()、x、y); y = y + lineheight;} return y -lineheight;}}}}}}}}実行結果:
効果プレビュー:
要約します
簡単に言えば、Javaプログラムを作成し、出力結果を「*.xps」フォーマットファイルとして保存し、プリンタから出力することです。これは非常に簡単です。それがすべての人に役立つことを願っています。ご質問がある場合は、メッセージを残して指摘してください。このサイトへのご支援をありがとうございました。