熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

用Java代碼生成打印收據

2022-06-13   來源: Java核心技術 

  首先來看看效果如下圖

  用java代碼生成打印收據 - 任我行 - 任我行java博客

  代碼如下

  PrintTestjava

  import javaawtBasicStroke;

  import javaawtColor;

  import javaawtFont;

  import javaawtGraphics;

  import javaawtGraphicsD;

  import javaawtprintBook;

  import javaawtprintPageFormat;

  import javaawtprintPaper;

  import javaawtprintPrintable;

  import javaawtprintPrinterException;

  import javaawtprintPrinterJob;

  import javatextSimpleDateFormat;

  import javautilDate;

  import snjpmodelRentDetail;

  import snjpparameterRentRecordParameter;

  public class PrintTest implements Printable

  {

  private String roomID;

  private Date printDate;

  private Integer rentType;

  private Float waterFee;

  private Float electricityFee;

  private Float rent;

  private String remark;

  private static int RECEIPT_NO =;

  public PrintTest()

  {

  }

  public PrintTest printTest;

  public PrintTest(RentRecordParameter prentRecordRentDetail prentDetail)

  {

  thisroomID = prentRecordgetRoomId()+房間;

  thisprintDate = prentRecordgetRegisterDate();

  thisrentType = prentRecordgetRentType();

  thiswaterFee = prentDetailgetWaterBill();

  thiselectricityFee = prentDetailgetElectricityBill();

  thisrent = prentDetailgetRent();

  thisremark = prentDetailgetRemark();

  }

  /** */

  /**

  * @param Graphic指明打印的圖形環境

  * @param PageFormat指明打印頁格式(頁面大小以點為計量單位點為英才的/英寸為毫米A紙大致為×點)

  * @param pageIndex指明頁號

  **/

  public int print(Graphics graphics PageFormat pf int pageIndex)

  throws PrinterException

  {

  //print string

  String receiptTitle = 收款收據;

  String content=今收到;

  String feeItem = null;

  Float rentTotal = f;

  String content=總計人民幣(大寫);

  //收款的內容(根據租賃類型只有房租房租水費電費兩種值)

  switch(thisrentType)

  {

  case :

  feeItem = 房租押金;

  rentTotal=thisrent;

  break;

  case :

  feeItem = 房租押金水費電費;

  rentTotal=thisrent+thiselectricityFee+thiswaterFee;

  break;

  default:

  feeItem = 未知收費項目;

  }

  //轉換成GraphicsD

  GraphicsD graphicsD = (GraphicsD) graphics;

  //設置打印顏色為黑色

  graphicsDsetColor(Colorblack);

  //打印起點坐標x=

  double x = pfgetImageableX();

  double y = pfgetImageableY();

  Systemoutprintln(打印起點坐標值是{+x++y+});

  switch (pageIndex)

  {

  case :

  //設置打印字體(字體名稱樣式和點大小)(字體名稱可以是物理或者邏輯名稱)

  //Java平台所定義的五種字體系列SerifSansSerifMonospacedDialog 和 DialogInput

  Font font = new Font(新宋體 FontTRUETYPE_FONT );

  graphicsDsetFont(font);//設置字體

  BasicStroke   basicStroke=new   BasicStroke(f);

  float[] dash = { f };

  //設置打印線的屬性

  //線寬; ;;空白的寬度;虛線的寬度;偏移量

  //                graphicsDsetStroke(new BasicStroke(f

  //                                BasicStrokeCAP_BUTT BasicStrokeJOIN_MITER

  //                                f dash f));

  //graphicsDsetStroke(new BasicStroke(f));

  graphicsDsetStroke(basicStroke);//設置線寬

  float height = fontgetSizeD();//字體高度

  Systemoutprintln(字體高度是+height);

  //繪制收據標題

  graphicsDdrawString(receiptTitle (float) y + * height);

  //繪制收據編號

  String receiptNo = StringvalueOf(RECEIPT_NO);

  graphicsDdrawString(No:+GlobalMethodgetString(receiptNo) (float)x* (float) y + * height);

  //繪制打印時間

  SimpleDateFormat myDateFormat = new SimpleDateFormat(yyyy年MM月dd日);

  graphicsDdrawString(myDateFormatformat(printDate) (float)x* (float) y + * height);

  //繪制收據標題下面的第一條橫線

  graphicsDdrawLine(

  

  (int) (y + * height+)

  

  (int) (y + * height+)

  );

  //繪制收據標題下面的第二條橫線

  graphicsDdrawLine(

  

  (int) (y + * height+)

  

  (int) (y + * height+)

  );

  //正文——今收到

  graphicsDdrawString(content(float) x* (float) y + * height);

  //收費項目名稱

  graphicsDdrawString(thisroomID+feeItem(float) x* (float) y + * height);

  //繪制正文下劃線

  graphicsDdrawLine(

  (int)x*

  (int) (y + * height+)

  (int)x+

  (int) (y + * height+)

  );

  //人民幣(大寫)

  graphicsDdrawString(content+MoneyNumToRMBStr(rentTotal)(float) x* (float) y + * height);

  //繪制人民幣(大寫)下劃線

  graphicsDdrawLine(

  (int)x*

  (int) (y + * height+)

  (int)x+

  (int) (y + * height+)

  );

  //(小寫)

  graphicsDdrawString((小寫)+rentTotal+(float) x* (float) y + * height);

  //繪制(小寫)下劃線

  graphicsDdrawLine(

  (int)x*

  (int) (y + * height+)

  (int)x+

  (int) (y + * height+)

  );

  //附注

  graphicsDdrawString(附注:+thisremark(float) x* (float) y + * height);

  //繪制附注下劃線

  graphicsDdrawLine(

  (int)x*+

  (int) (y + * height+)

  (int)x+

  (int) (y + * height+)

  );

  // 簽字(收款人)

  graphicsDdrawString(簽字(收款人)(float) x*+ (float) y + * height);

  //簽字(收款人)下劃線

  graphicsDdrawLine(

  (int)x*

  (int) (y + * height+)

  (int)x+

  (int) (y + * height+)

  );

  return PAGE_EXISTS;

  default:

  RECEIPT_NO=RECEIPT_NO;

  return NO_SUCH_PAGE;

  }

  }

  //方法調用

  public  void printReceipt(PrintTest printTest){

  //通俗理解就是書文檔

  Book book = new Book();

  //設置成豎打

  PageFormat pf = new PageFormat();

  pfsetOrientation(PageFormatPORTRAIT);

  //通過Paper設置頁面的空白邊距和可打印區域必須與實際打印紙張大小相符

  Paper p = new Paper();

  psetSize( );//紙張大小

  psetImageableArea( );//A( X )設置打印區域其實應該是因為A紙的默認XY邊距是

  pfsetPaper(p);

  //把 PageFormat 和 Printable 添加到書中組成一個頁面

  bookappend(printTest pf);

  //獲取打印服務對象

  PrinterJob job = PrinterJobgetPrinterJob();

  //設置打印類

  jobsetPageable(book);

  try

  {

  //可以用printDialog顯示打印對話框在用戶確認後打印也可以直接打印

  //            boolean isShowing=jobprintDialog();

  //            if(isShowing)

  //            {

  //              Systemoutprintln(thisprintDate+*************);

  RECEIPT_NO=RECEIPT_NO+;

  jobprint();

  //            }

  }

  catch (PrinterException e)

  {

  eprintStackTrace();

  }

  }

  public static void main(String[] args)

  {

  //    通俗理解就是書文檔

  Book book = new Book();

  //    設置成豎打

  PageFormat pf = new PageFormat();

  pfsetOrientation(PageFormatPORTRAIT);

  //    通過Paper設置頁面的空白邊距和可打印區域必須與實際打印紙張大小相符

  Paper p = new Paper();

  psetSize( );//紙張大小

  psetImageableArea( );//A( X )設置打印區域其實應該是因為A紙的默認XY邊距是

  pfsetPaper(p);

  //    把 PageFormat 和 Printable 添加到書中組成一個頁面

  bookappend(new PrintTest() pf);

  //獲取打印服務對象

  PrinterJob job = PrinterJobgetPrinterJob();

  // 設置打印類

  jobsetPageable(book);

  try

  {

  //可以用printDialog顯示打印對話框在用戶確認後打印也可以直接打印

  //boolean a=jobprintDialog();

  //if(a)

  //{

  jobprint();

  //}

  }

  catch (PrinterException e)

  {

  eprintStackTrace();

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25778.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.