JAVA 打印,自定义纸张大小

[java]  view plain copy
  1. package news;  
  2.   
  3.   
  4. import java.awt.Color;  
  5. import java.awt.Font;  
  6. import java.awt.Graphics;  
  7. import java.awt.Graphics2D;  
  8. import java.awt.print.Book;  
  9. import java.awt.print.PageFormat;  
  10. import java.awt.print.Paper;  
  11. import java.awt.print.Printable;  
  12. import java.awt.print.PrinterException;  
  13. import java.awt.print.PrinterJob;  
  14. import java.text.DecimalFormat;  
  15. import java.text.NumberFormat;  
  16. import javax.print.PrintService;  
  17. import javax.print.PrintServiceLookup;  
  18. import javax.print.attribute.HashAttributeSet;  
  19. import javax.print.attribute.standard.PrinterName;  
  20.   
  21.   
  22. public class PrintWarranty implements Printable {  
  23.   
  24.   
  25. /** 
  26. *   * @param Graphic指明打印的图形环境   * @param 
  27. * PageFormat指明打印页格式(页面大小以点为计量单位,1点为1英寸的1/72,1英寸为25.4毫米。A4纸大致为595×842点)   * @param 
  28. * pageIndex指明页号 
  29. **/  
  30. int PageNumbers;  
  31. String StartSN;  
  32. String Model;  
  33. String Format;  
  34. public PrintWarranty(int PageNumbers,String StartSN,String Model,String Format)  
  35. {  
  36.     this.PageNumbers=PageNumbers;  
  37.     this.StartSN=StartSN;  
  38.     this.Model=Model;  
  39.     this.Format=Format;  
  40. }  
  41. public void SetPageNum(int PageNumbers)  
  42. {  
  43.     this.PageNumbers=PageNumbers;  
  44. }  
  45. public int getPageNum()  
  46. {  
  47.     return PageNumbers;  
  48. }  
  49. public void SetStartSN(String StartSN)  
  50. {  
  51.     this.StartSN=StartSN;  
  52. }  
  53. public String getStartSN()  
  54. {  
  55.     return StartSN;  
  56. }  
  57. public void SetModel(String Model)  
  58. {  
  59.     this.Model=Model;  
  60. }  
  61. public String getModel()  
  62. {  
  63.     return Model;  
  64. }  
  65. public void SetFormat(String Format)  
  66. {  
  67.     this.Format=Format;  
  68. }  
  69. public String getFormat()  
  70. {  
  71.     return Format;  
  72. }  
  73. public int print(Graphics gra, PageFormat pf, int pageIndex) throws PrinterException {  
  74.    Graphics2D g2 = (Graphics2D) gra;  
  75.    // 设置打印颜色为黑色  
  76.    g2.setColor(Color.black);  
  77.    Font font = new Font("MS PGothic", Font.PLAIN, 13);  
  78.    g2.setFont(font); // 设置字体  
  79.    if (pageIndex >= PageNumbers)  
  80.         return NO_SUCH_PAGE;  
  81.    String SN=StartSN.substring(610);  
  82.    int intSN=Integer.parseInt(SN);  
  83.    intSN=intSN+pageIndex;  
  84.    NumberFormat formattern = new DecimalFormat("0000");  
  85.    SN=formattern.format(intSN);  
  86.    System.out.println(SN);  
  87.   
  88.   
  89.           g2.drawString(Model, (float134, (float145 ); //第二排  
  90.           g2.drawString(StartSN.substring(06)+SN, (float266, (float145 ); //第三排   
  91.      
  92.     return PAGE_EXISTS;  
  93. }  
  94.   
  95.   
  96. public void PrintLabel(String PrintQ) {  
  97.    //ReadData();  
  98.    // 通俗理解就是书、文档  
  99.    Book book = new Book();  
  100.    // 设置成竖打  
  101.    PageFormat pf = new PageFormat();  
  102.    pf.setOrientation(PageFormat.PORTRAIT); // LANDSCAPE表示竖打;PORTRAIT表示横打;REVERSE_LANDSCAPE表示打印空白  
  103.    // 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。  
  104.    Paper p = new Paper();  
  105.    p.setSize(567252); // Warranty Paper Size,A4 590, 840  
  106.    p.setImageableArea(1010590840); // Print Area  
  107.    pf.setPaper(p);  
  108.    // 把 PageFormat 和 Printable 添加到书中,组成一个页面  
  109.    book.append(this, pf,this.getPageNum());  
  110.    String printerName=PrintQ;    
  111.    HashAttributeSet hs = new HashAttributeSet();   
  112.    hs.add(new PrinterName(printerName,null));  
  113.    // 获取打印服务对象  
  114.    PrintService[] printService =PrintServiceLookup.lookupPrintServices(null, hs);    
  115.    PrinterJob job = PrinterJob.getPrinterJob();  
  116.    try {  
  117.      if(printService.length>0){  
  118.            job.setPrintService(printService[0]);  
  119.      }  
  120.      //  设置打印类  
  121.      job.setPageable(book);  
  122.      //  Print  
  123.      job.print();  
  124.   
  125.   
  126.    } catch (PrinterException e) {  
  127.      e.printStackTrace();  
  128.    }  
  129. }  
  130. public static void main(String[] args) {  
  131.       
  132.     (new PrintWarranty(1,"2132300001","FTH-18","0")).PrintLabel("\\\\SHA1APFPSW02\\sha1aplj5sIT");  
  133. }  
  134. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
打印自定义纸张尺寸,你需要按照以下步骤进行操作: 1. 获取打印机的可用尺寸。你可以使用 `PrintService` 类的 `getSupportedAttributeValues()` 方法来获取打印机支持的所有纸张尺寸。 2. 创建一个 `Paper` 对象,并设置它的尺寸和边距。例如,如果你要打印一个 8"x 4" 的纸张,你可以这样设置: ``` Paper paper = new Paper(); double width = 8 * 72.0; // 1 inch = 72 points double height = 4 * 72.0; paper.setSize(width, height); paper.setImageableArea(0, 0, width, height); ``` 3. 创建一个 `PageFormat` 对象,并设置它的纸张类型为自定义纸张,然后将 `Paper` 对象设置为该 `PageFormat` 的纸张。 ``` PageFormat pageFormat = new PageFormat(); pageFormat.setPaper(paper); pageFormat.setOrientation(PageFormat.PORTRAIT); ``` 4. 创建一个 `Printable` 对象,并实现它的 `print()` 方法。在 `print()` 方法中,你可以使用 `Graphics` 对象来绘制自定义内容。 ``` Printable printable = new Printable() { @Override public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex > 0) { return NO_SUCH_PAGE; } // 绘制自定义内容 graphics.drawString("Hello, World!", 100, 100); return PAGE_EXISTS; } }; ``` 5. 创建一个 `PrinterJob` 对象,并将 `Printable` 对象和 `PageFormat` 对象设置为它的属性。最后,调用 `PrinterJob` 的 `print()` 方法开始打印。 ``` PrinterJob printerJob = PrinterJob.getPrinterJob(); printerJob.setPrintable(printable, pageFormat); if (printerJob.printDialog()) { printerJob.print(); } ``` 注意:在设置自定义纸张大小时,你需要确保打印机支持该尺寸。如果打印机不支持该尺寸,打印作业将无法正常完成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值