운영 환경에 대한 간단한 소개 :
언어 : Java
도구 : 일식
시스템 : Windows 7
(인쇄 장치에는 아직 인쇄 장치가 없으므로 미리보기 사진 만 제공 할 수 있습니다)
최근 프로젝트는 쇼핑몰의 쇼핑 영수증을 인쇄해야합니다. 일상 생활에서, 우리는 물건을 사기 위해 슈퍼마켓에갑니다. 체크 아웃 할 때 계산원은 영수증을 인쇄합니다. 일반적으로 쇼핑몰에는 작은 기능이 필요합니다. 이 기사에 제공된 데모는 58mm 열 프린터의 예입니다. 다른 용지 유형의 프린터 인 경우 용지 너비를 조정하십시오.
패키지 테스트; import java.awt.*; import java.awt.print.*;/*** 프린터 테스트 클래스 (58mm)* 1. 대상 프린터는 기본 프린터* 2로 설정되어야합니다. 인쇄 페이지의 너비는 특정 프린터와 관련이 있습니다. 일반적으로 인쇄 용지의 너비이며 시스템 매개 변수* 3으로 구성되어야합니다. 중국어의 너비는 약 12 점*/public class printtest {public static void main (string [] args) {if (printerjob.lookupprintservices () {/* 길이*/pageformat pageformat = new print format (print format) (); 코너, 왼쪽에서 오른쪽으로, 위쪽으로, pageformat.setorientation (pageformat.portrait);/* page 형식 설정*/paper paper = new paper (); // 인쇄 너비 (특정 프린터와 관련) 및 높이 (실제 인쇄 내용과 관련된) paper.setsize (140, 450); 0, width 및 width 및 print. 135, 450); pageformat.setpaper (paper); // 인쇄 문서 책 작성 책 = new book (); book.append (new printable () {@override public int print (그래픽 그래픽, pageformat pageformat, int pageindex) printerexception {if (pageindex> 0) {return no_such_page; font) font = new Font ( "安体", font.plain, 5); Graphics2d.setfont (font); DrawString (Graphics2d, //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////e ////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////// 119, 8); font = new font ( "宋体", font.plain, 7); graphics2d.setfont (font); int yindex = 30; int lineHeight = 10; int lineWidth = 120; color defaultColor = Graphics2d.getColor () GetColor = new Color (145, 145, 145); // indivic yindex = new color (145, 145); "Consignee : Passerby A", 10, Yindex, Linewidth, LineHeight); yindex = DrawString (Graphics2d, "주소 : Baidu Building, No. 10 Shangdi Street, Haidian District, Beijing", 10, Yindex + LineHeight, Linewidth, LineHeight); Basictroke.cap_butt, Basictroke.join_bevel, 0, New Float [] {4, 4}, 0); Graphics2d.setStroke (Stroke); Graphics2d.DrawRect (5, 10, 129, Yindex); // 약국 이름 라인 width = 129; LineHeight = 8; Graphics (new font ( ", on ontfont). font.bold, 8)); Graphics2d.setColor (defaultColor); yindex = drawString (Graphics2d, "Beijing Pharmacy Retail Ticket", 5, Yindex + LineHeight + 20, LineWidth, 12); Graphics2d.Setfont ( "宋体", font.plain, 6); DrawString (Graphics2d, "Operator : Xiaoqing", 5, Yindex + LineHeight + 2, LineWidth, LineHeight); Yindex = DrawString (DAGPICS2D, "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 = groudString (Graphics2d, "rinewidth/10)*8, linewidth, linewidth, yindex, yindex, yindex, yindex =); DRAWSTRING (Graphics2d, "수량", (linewidth/10)*10, yindex, linewidth, lineHeight); for (int i = 0; i <5; i ++) {graphics2d.setfont ( "宋体", font.plain, 7); 정제 ", 5, yindex + 15, (linewidth/10)*7, 10); Graphics2d.setfont (새 글꼴 ("安安 ", font.plain, 6)); graphics2d.setcolor (회색); graphics2d,"100 tablets/box ", 5, yindex + 11, linewidth, linewidth, linewidth); drawString (Graphics2d, "14.50", (linewidth/10)*8, yindex, linewidth, lineHeight); yindex = drawString (Graphics2d, "2", (linewidth/10)*10, yindex, linewidth, lineHeight); graphics2d.setfont (새 글 ( ") yindex + 2; Graphics2d.drawline (5, yindex, 5 + linewidth, yindex);} graphics2d.setcolor (defaultColor); yindex = drawString ( "멤버 이름 : Xiaoqing", 5, yindex + lineHeight * 2, linewidth, lineHeight); 5, yindex + lineHeight, linewidth, lineHeight); yindex = drawString (Graphics2d, "Total : 55.30", 5, yindex + lineHeight, linewidth, lineHeight); yindex = groudString (그래픽 2D, "Cash : 100.00", 5, Yindex + LineHeight, Linewidth, Linewidth, Linewight =); DrawString (Graphics2d, "Change : 44.70", 5, Yindex + LineHeight, LineWidth, LineHeight); Graphics2d.SetFont (New Font ( "宋体", font.plain, 6); Graphics2d.setColor (Gray); yindex = dawsString (graphics2d, 5). 2, linewidth, lineHeight); yindex = drawString (Graphics2d, "주소 : Baidu Building, Baidu Building, No. 10 Shangdi Street, Beijing, Beijing", 5, Yindex + LineHeight, Linewidth, LineHeight); yindex = yindex + 20; graphics2d.wrawline (0, yindex, 140); pageformat); // 기본 프린터 프린터 job printerjob = printerjob.getprinterjob (); printpagable (book); try {printerjob.print ();} catch (printerexception e) {e.printstacktrace (); system.out.println ( "print express")} elpint.out.out.out.out.out.out.out.out.out. ");}}/*** 문자열 출력* @param Graphics2d 브러시* @param 텍스트 인쇄 텍스트* @param x 인쇄 지점 X 좌표* @param y 인쇄 시작점 y coordinate* @param linewidth line width* @param lineheight line le en lineWidth, int lineHeight) {fontMetrics fontMetrics = graphics2d.getFontMetrics (); if (fontMetrics.stringWidth (text) <lineWidth) {graphics2d.DrawString (text, x, y); else {chars = 0.tobray (); int charwid. StringBuffer (); for (int i = 0; i <chars.length; i ++) {if ((charswidth + fontmetrics.charwidth (chars [i]))> linewidth) {Graphics2d.DrawString (sb.toString (), x, y); fontmetrics.charWidth (chars [i]); sb.append (chars [i]);} else {charswidth = charswidth + fontmetrics.charwidth (chars [i]); sb.append (chars [i]);}} if (sb.length ()> 0) {sb.tring (sb.tostring) y); y = y + lineHeight;} return y -lineHeight;}}}실행 결과 :
효과 미리보기 :
요약
간단히 말해서, Java 프로그램을 작성하고 출력 결과를 "*.xps" 형식 파일로 저장하고 프린터로 출력하는 것이 매우 간단합니다. 모든 사람에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다.