A Java sample that can be printed on a stylus printer

Source: Internet
Author: User

Code:

import Java.awt.Color;import Java.awt.Font;import java.awt.Graphics;import Java.awt.Graphics2D;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;  Public classPrintImplementsPrintable {Private intPageSize;//Total pages printed   Private Doublepaperw=0;//Printed paper width   Private DoublePaperh=0;//Printed paper height   //implementation of Java.awt.print.Printable interface printing method   //pageindex: The current page of the print, this parameter is automatically maintained by the system, does not require manual maintenance, the system will automatically increment    Public int Print(Graphics graphics, Pageformat Pageformat,intPageIndex)throwsPrinterException {if(PageIndex >= pageSize)//exit Print        returnPrintable.No_such_page;Else{graphics2d g2 = (graphics2d) graphics; G2.SetColor(Color.BLUE); Paper p =NewPaper ();//The PAPERW and Paperh here are obtained from the feed specifications of the target printer, and the printable area of the actual pin printer is limited,        //distance from the top and bottom of the paper 1inch (inch) in the middle of the distance box is the actual printable area, out of range of content will not be printed (no offset)        //If you set the offset, then the range is also printable, here Pagew and Pageh I am directly to obtain the printer's feed specifications and high        ///can also be manually specified, from if the manually specified width and target printer feed specifications vary significantly, will default to A4 paper as the print templateP.Setimageablearea(0,0, Paperw, Paperh);//Set printable areaP.setSize(PAPERW,PAPERH);//Set the size of the paperPageformat.Setpaper(p);Drawcurrentpagetext(G2, Pageformat);//How to call print content        returnpage_exists; }   }//print content   Private void Drawcurrentpagetext(graphics2d G2, Pageformat PF) {Font font =NULL;//Set fonts for printingFont =NewFont ("New song Body", Font.BOLD, One); G2.SetFont(font);//Set Font      //Print a sentence here, the print start position is (200,200), indicating that the coordinates (200,200) from Pf.getpaper () Start printing      //Here 200 of the unit is 1/72 (inch), inch: inches, so here the length, after the measurement needs to be convertedG2.DrawString(" print test print test print test printing tests print test print test print test print test print test print test print test print tests", $, $); }//Connect printer, pop up print dialog box    Public void Starprint() {Try{PrinterJob prnjob = printerjob.Getprinterjob(); Pageformat Pageformat =NewPageformat (); Pageformat.setorientation(Pageformat.PORTRAIT); Prnjob.setprintable( This);//Pop-up Print dialog box, or you can choose not to eject the print prompt box, print directly        if(!prnjob.PrintDialog())return;//Get the width of the feed specification for the attached target printer, in 1/72 (inch)Paperw=prnjob.Getpageformat(NULL).Getpaper().getwidth();//Get the width of the feed specification for the attached target printer, in 1/72 (inch)Paperh=prnjob.Getpageformat(NULL).Getpaper().getheight();//system.out.println ("Paperw:" +paperw+ ";p aperh:" +paperh);Prnjob.Print();//Start printing work}Catch(PrinterException ex) {Ex.Printstacktrace(); System.Err.println("Print Error:"+ ex.toString()); }   }//Entrance method    Public Static void Main(string[] args) {Print pm =New Print();//instanced print classPm.pageSize=2;//Print two pagesPm.Starprint(); }}

Reference: http://blog.sina.com.cn/s/blog_8f54b9f901014fl9.html

A Java sample that can be printed on a stylus printer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.