java打印机
java实现打印机功能,过长处理方式

java实现打印机功能,过长处理方式在Java中实现打印机功能时,可以使用字符串的截取方法来处理过长的文本。
以下是一个示例:```javapublic class Printer {private static final int MAX_LINE_LENGTH = 50; // 打印机每行的最大长度public static void main(String[] args) {String text = "这是一段过长的文本,需要进行处理以适应打印机的行宽。
";printText(text);}public static void printText(String text) {int textLength = text.length();if (textLength <= MAX_LINE_LENGTH) {System.out.println(text); // 文本长度小于等于行宽,直接打印} else {int startIndex = 0;while (startIndex < textLength) {int endIndex = Math.min(startIndex +MAX_LINE_LENGTH, textLength); // 取该行的结束索引String line = text.substring(startIndex, endIndex); // 截取该行的文本System.out.println(line);startIndex = endIndex; // 更新起始索引}}}}```在上述示例中,`printText()`方法用于打印文本。
当文本长度小于等于行宽时,直接将文本打印出来。
当文本长度超过行宽时,通过循环逐行截取文本,并打印出截取的每一行。
每次截取的截止索引都是起始索引加上行宽和剩余文本长度的较小值,以保证不会超出文本的长度。
TSC条码打印机java调用

TSC条码打印机java调用以下是为大家整理的Tsc条码打印机java调用的相关范文,本文关键词为Tsc,条码,打印机,java,调用,package,dll,您可以从右上方搜索框检索更多相关文章,如果您觉得有用,请继续关注我们并推荐给您的好友,您可以在综合文库中查看更多范文。
packagedlltest;importcom.sun.jna.Library;importcom.sun.jna.native;publicclassmain{publicinterfaceTscLibDllextendsLibrary{TscLibDllInsTAnce=(TscLibDll)native.loadLibrary(\TscLibDll.class);intabout();intopenport(stringpirntername);intcloseport();intsendcommand(stringprintercommand);intsetup(stringwidth,stringheight,stringspeed,stringdensity,stringsensor,s tringvertical,stringoffset);intdownloadpcx(stringfilename,stringimage_name);intbarcode(stringx,stringy,stringtype,stringheight,stringreadable,stringrot ation,stringnarrow,stringwide,stringcode);intprinterfont(stringx,stringy,stringfonttype,stringrotation,stringxmul,stri ngymul,stringtext);intclearbuffer();intprintlabel(stringset,stringcopy);intformfeed();intnobackfeed();intwindowsfont(intx,inty,intfontheight,introtation,intfontstyle,intfontund erline,content);stringszFacename,string}publicstaticvoidmain(string[]args){//TscLibDll.InsTAnce.about();TscLibDll.InsTAnce.openport(\*****\DLLTest!!\\90\180\270\}//TscLibDll.InsTAnce.downloadpcx(\TscLibDll.InsTAnce.sendcommand(\*****ThisisatestbyJAVA.TscLibDll.InsT Ance.setup(\TscLibDll.InsTAnce.clearbuffer();//TscLibDll.InsTAnce.sendco mmand(\TscLibDll.InsTAnce.printerfont(\\\\\\\VA)TscLibDll.InsTAnce.barc ode(\\\\\\\\TscLibDll.InsTAnce.windowsfont(400,200,48,0,3,1,\TscLibDll.I nsTAnce.windowsfont(400,200,48,90,3,1,\\TscLibDll.InsTAnce.windowsfo nt(400,200,48,180,3,1,\\TscLibDll.InsTAnce.windowsfont(400,200,48,270 ,3,1,\\TscLibDll.InsTAnce.printlabel(\TscLibDll.InsTAnce.closeport();}最后,小编希望文章对您有所帮助,如果有不周到的地方请多谅解,更多相关的文章正在创作中,希望您定期关注。
java单例模式的实际应用

java单例模式的实际应用Java单例模式是一种常用的设计模式,它可以保证在一个JVM中只存在一个实例对象。
在实际应用中,单例模式可以解决一些特定场景下的问题,提高系统的性能和效率。
一、数据库连接池在使用数据库连接时,每次都需要创建连接对象和释放连接对象,这个过程比较耗时。
如果每次都创建新的连接对象,会导致系统性能下降。
这时可以使用单例模式来创建一个数据库连接池,保证系统中只有一个连接池对象,通过连接池管理连接的创建和释放,提高了系统的性能和效率。
二、日志记录器在系统开发中,通常需要记录系统的运行日志,以供日后排查问题或者监控系统运行状态。
使用单例模式创建一个日志记录器对象,可以保证系统中只有一个日志记录器实例,所有的日志记录都通过这个实例进行操作,便于集中管理和统一处理。
三、配置文件管理器在系统开发中,通常需要读取配置文件中的参数或者属性,以便灵活配置系统的行为。
使用单例模式创建一个配置文件管理器对象,可以保证系统中只有一个配置文件管理器实例,所有的配置文件读取操作都通过这个实例进行,便于统一管理和维护。
四、线程池在多线程编程中,线程的创建和销毁都需要耗费一定的系统资源。
如果每次都创建新的线程,会导致系统资源的浪费。
使用单例模式创建一个线程池对象,可以保证系统中只有一个线程池实例,通过线程池管理线程的创建和销毁,提高了系统的性能和效率。
五、缓存管理器在系统开发中,通常需要缓存一些数据,以提高系统的访问速度。
使用单例模式创建一个缓存管理器对象,可以保证系统中只有一个缓存管理器实例,通过缓存管理器来管理缓存的读取、存储和更新,提高了系统的性能和效率。
六、打印机管理器在某些场景下,需要使用打印机进行打印操作。
如果每次都新建一个打印机对象,会导致系统资源的浪费。
使用单例模式创建一个打印机管理器对象,可以保证系统中只有一个打印机管理器实例,通过打印机管理器来管理打印机的使用,提高了系统的性能和效率。
七、窗口管理器在图形界面编程中,通常需要使用窗口进行用户交互。
JavaPrintService_Java打印API_用户手册_中文版

目录第一章介绍Java平台打印的历史JDK 1.3JDK 1.2JDK 1.1Java Print Service API能做什么Java Print Service 构架javax.print包发现打印服务指定打印数据格式创建打印工作javax.print.event包应用程序如何使用JPS一个基本的例子第二章属性属性的类别和值属性角色属性集如何指定属性标准属性OrientationRequestedCopiesMediaMediaSizeMediaPrintableAreaDestinationSheetCollateSidesFidelity使用JPS属性第三章指定文档类型用户格式打印数据预定义数据格式的MIME类型文本数据页面描述语言文档图像数据自适应打印数据表示类字符编码的重要性服务格式打印数据怎样使用DocFlavor第四章打印及流化文档比较StreamPrintService与PrintService定位服务发现打印服务发现流打印服务获得一个打印工作创建DOC注册事件打印服务事件打印工作事件PrintJobAttributeListenerPrintJobListener提交打印工作向打印机提交打印工作向流提交打印工作打印服务提供商第五章打印及流化2D图像使用打印工作打印或流化图像打印2D图像流化2D图像使用服务格式数据打印服务格式数据流化服务格式打印数据示例:PrintPS.java示例:PrintGIFtoStream.java示例:Print2DPrinterJob.java示例:Print2DGraphics.java示例:Print2DtoStream.java示例:PrintGIF.javaJava Print Service 词汇表第一章介绍Java Print Service (JPS)是一个Java打印API,设计此API是为了支持在所有Java 平台上的打印工作——包括微空间平台,并且支持现有的API。
java批量打印图片

J ava批量打印图片解决方案有两个解决方法:直接打印一个图片集合;将多个图片放到一个文件里,再打印这个文件。
一、直接打印图片集合首先实现单个图片的打印(用到javax下的print包)。
根据图片格式选择不同的打印方式(支持gif、jpg、png格式的图片)。
其次实现多个图片的打印。
用多次调用打印程序的方式实现图片的打印,但是会出现问题:每次打印都是重复调用打印机,不能实现调用一次打印机就批量完成打印,最多实现:一次批量打印,只取一次打印机,然后多次调用这个打印机打印单张图片。
考虑将几个图片在打印前合并成一个,然后打印出来。
这种方式打印图片是将图片转化成流的形式打印,有方法实现将几个图片流数据合并成一个流,然后将流数据打印出来。
问题又出现了:这种合并流的方式是将几个不同的图片合并到一个图片里,没有分页;同时没有考虑这种流的合成方式对图片本身内容的影像(未验证)。
二、将多个图片合一,然后打印从目标达成方面考虑,将图片合一有两种格式:word和pdf。
但是考虑到是图片信息在word中显的问题,特别是技术实现和效果方面考虑,最后选择将批量图片制作成pdf格式的文件,交付打印。
目前关于将图片转换成pdf文件应用最广泛的是itext,iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。
通过iText不仅可以生成PDF 或rtf的文档,而且可以将XML、Html文件转化为PDF文件。
这里也可以用纯加java的方式将图片呢合并成pdf文件。
在长春人事档案目录里面,有实现图片转换为pdf的源代码,大家有用到的可以去这个目录(com.yinhai.file.ImageUtils.java)里面找,里面实现了两个静态方法:1)imageToPdf(String 需要转化为pdf的图片所在文件夹,String 新生成pdf文件的路径)要将图片文件转换为pdf文件要分三步走:首先,得到需要转化的图片。
java实现调用打印机

1.import java.io.File;2.import java.io.FileInputStream;3.4.import javax.print.Doc;5.import javax.print.DocFlavor;6.import javax.print.DocPrintJob;7.import javax.print.PrintService;8.import javax.print.PrintServiceLookup;9.import javax.print.ServiceUI;10.import javax.print.SimpleDoc;11.import javax.print.attribute.DocAttributeSet;12.import javax.print.attribute.HashDocAttributeSet;13.import javax.print.attribute.HashPrintRequestAttributeSet;14.import javax.swing.JFileChooser;15.16. /**17. * 建立与打印机的连接18. * @author Administrator19. *20. */21.public class PrintDemo {22.23. public static void main(String[] args) {24. JFileChooser fileChooser = new JFileChooser(); //创建打印作业25. int state = fileChooser.showOpenDialog(null);26. if(state == fileChooser.APPROVE_OPTION){27. File file = new File("D:/zkyzl.txt"); //获取选择的文件28. //构建打印请求属性集29. HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();30. //设置打印格式,因为未确定类型,所以选择autosense31. DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;32. //查找所有的可用的打印服务33. PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);34. //定位默认的打印服务35. PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();36. //显示打印对话框37. PrintService service = ServiceUI.printDialog(null, 200, 200, printService,38. defaultService, flavor, pras);39. if(service != null){40. try {41. DocPrintJob job = service.createPrintJob(); //创建打印作业42. FileInputStream fis = new FileInputStream(file); //构造待打印的文件流43. DocAttributeSet das = new HashDocAttributeSet();44. Doc doc = new SimpleDoc(fis, flavor, das);45. job.print(doc, pras);46. } catch (Exception e) {47. e.printStackTrace();48. }49. }50. }51. }52.}。
java epson 代码
java epson 代码Java Epson 代码Epson 是一家知名的打印机制造商,其提供了丰富的打印机型号和相关技术。
在 Java 开发领域,Epson 也提供了相应的 Java 代码和 API,使开发者能够方便地与 Epson 打印机进行交互和控制。
本文将介绍一些常用的 Java Epson 代码示例,帮助开发者更好地理解和使用 Epson 打印机。
一、连接 Epson 打印机在使用 Epson 打印机之前,首先需要建立与打印机的连接。
以下是一段 Java 代码示例,展示了如何使用 Epson 的 API 进行连接:```javaimport com.epson.epos2.*;import com.epson.eposprint.*;public class EpsonPrinterConnection {public static void main(String[] args) {try {// 创建打印机对象Printer printer = new Printer(Printer.TM_T88, Printer.MODEL_ANK, "USB");// 打开打印机连接printer.openPrinter(Printer.PARAM_DEFAULT);// 进行打印机相关操作// 关闭打印机连接printer.closePrinter();} catch (Exception e) {e.printStackTrace();}}}```在上述代码中,首先导入了 Epson 的相关 API,然后创建了一个打印机对象,并指定了打印机的型号和连接方式。
接着,使用`openPrinter()` 方法打开与打印机的连接,并在连接成功后进行打印机相关的操作。
最后,使用 `closePrinter()` 方法关闭与打印机的连接。
通过这段代码,开发者可以轻松地连接 Epson 打印机,并进行后续的操作。
java实现自动静默打印功能
java实现⾃动静默打印功能最近接到⼀个需求,要求实现⾃动打印功能,⼀般⽹页打印pdf需要借助浏览器的pdf组件,⽽且还要弹出打印窗⼝,再点击打印才能实现。
那么如何实现⾃动打印了,从点击⾃⼰⽹页上3次缩减到点击⼀次了?⼀.实现思路:1.读取本地打印机服务2. 服务端打印肯定不⾏需要⼀个客户端3.应⽤程序需要和本地打印服务通信,发送不同的打印指令本想⾃⼰⽤Python写⼀个打印控件的,但是时间来不及啊。
⼆.采⽤技术知识(以⾃动打印pdf为例)1. java操作⽂件流2. javascripte3. pdfjs4. Lodop打印控件三.技术细节1. 关于lodop预览打印是开源免费,但是静默打印是需要收费的,直接使⽤静默打印会在打印纸张最下⾯出现⽔印,我们只需要将打印机纸张⼤⼩就可以将⽔印打印在⽆效位置,这样在打印的结果上就不会出现⽔印了2. pdfjs使⽤版本不能太⾼,我这⾥使⽤ 1.4.137。
ps:如果找不到对应版本请与评论与我联系,我分享给⼤家四.上代码⼀.后端核⼼⽅法主要是将pdf⽂件转成base64编码public static String encodeBase64File(File file) throws Exception {FileInputStream inputFile = new FileInputStream(file);byte[] buffer = new byte[(int)file.length()];inputFile.read(buffer);inputFile.close();return new BASE64Encoder().encode(buffer);}⼆.前端核⼼代码1.在html⽂件body需要插⼊<canvas id="thepdfcanvas2" style="display:none;"></canvas>2. 需要在html页⾯引⼊lodop打印js,pdfjs<script type="text/javascript" src="../static/js/print/LodopFuncs.js"></script><script type="text/javascript" src="../static/js/pdfjs-dist/build/pdf.js"></script><script type="text/javascript">var LODOP=getLodop(document.getElementById('LODOP_OB'),document.getElementById('LODOP_EM'));</script>3.发送ajax请求向后端返回pdf⽂件流,通过pdfjs绘制pdf成图⽚可以进⾏属性调节,进⽽调⽤print静默打印function printOneURL(pdfUrl,preview){var params = {"path": pdfUrl};$.sendDefaultAjax(jsPPath + "/printModel/print.do", params,function (data) {if (data.code === 0) {var pdfData = atob(data.msg);PDFJS.workerSrc ='../static/js/pdfjs-dist/build/pdf.worker.js';//从pdf⽂件对象逐页获取page,并调⽤toImageAndPrint函数打印内容PDFJS.getDocument({data: pdfData}).then(function getPdfHelloWorld(pdf) {//此适⽤pdf只有⼀个page情况pdf.getPage(1).then(function getPageHelloWorld(page) {//scale调节⽣成的画布图⽚⼤⼩,与清晰度有关,需要调整。
java调用打印机方式二
java调⽤打印机⽅式⼆⼀、主类:package org.jeecg.print;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Stroke;import java.awt.print.Book;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;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;/*** @Author: shafei* @Date: 2020-04-15 10:30* @Version: V1.0* @Description:*/public class PrintTest {protected static final String DIAN_MING = "伊晨Bayby童装⼯⼚";protected static final String SHOU_HUO_REN = "⼤飞家";protected static final String SHOU_HUO_REN_BEI_ZHU = "⽆";protected static final String MAIJIA_DIZHI = "A区三街01114号";protected static final String MAIJIA_DH = "186********";protected static final String MAIJIA_WX = "yichenbaby0";public static void main(String[] args) {OrderVo orderVo = new OrderVo();orderVo.setBuyer("⼤飞家");orderVo.setRemark("⽆");orderVo.setSeller(DIAN_MING);orderVo.setAddress(MAIJIA_DIZHI);orderVo.setIphone(MAIJIA_DH);orderVo.setWxno(MAIJIA_WX);// orderVo.setTotalMoney(0);List<ProductVo> products = new ArrayList<ProductVo>();ProductVo productVo = new ProductVo();productVo.setName("忍者短袖(红)");productVo.setPrice(26.00);productVo.setAmount(8);productVo.setSize("100M");ProductVo productVo2 = new ProductVo();productVo2.setName("忍者短袖(蓝)");productVo2.setPrice(26.00);productVo2.setAmount(4);productVo2.setSize("103M");products.add(productVo);products.add(productVo2);new PrintTest().printDefault(orderVo, products);}private void printDefault(final OrderVo orderVo, final List<ProductVo> products ) {if (PrinterJob.lookupPrintServices().length > 0) {/** 打印格式*/PageFormat pageFormat = new PageFormat();// 设置打印起点从左上⾓开始,从左到右,从上到下打印pageFormat.setOrientation(PageFormat.PORTRAIT);/** 打印页⾯格式设置*/Paper paper = new Paper();// 设置打印宽度(固定,和具体的打印机有关)和⾼度(跟实际打印内容的多少有关)paper.setSize(140, 450);// 设置打印区域打印起点坐标、打印的宽度和⾼度paper.setImageableArea(0, 0, 135, 450);// paper.setImageableArea(0, 0, 135, 450);pageFormat.setPaper(paper);// 创建打印⽂档Book book = new Book();book.append(new Printable() {@Overridepublic int print(Graphics graphics, PageFormat pageFormat,int pageIndex) throws PrinterException {if (pageIndex > 0) {return NO_SUCH_PAGE;}Graphics2D graphics2D = (Graphics2D) graphics;Font font = new Font("宋体", Font.PLAIN, 5);graphics2D.setFont(font);drawString(graphics2D, "//////////////////////////////",10, 17, 119, 8);font = new Font("宋体", Font.PLAIN, 7);graphics2D.setFont(font);int yIndex = 30;int lineHeight = 10;int lineWidth = 120;Color defaultColor = graphics2D.getColor();Color grey = new Color(145, 145, 145);// 收货信息// yIndex = drawString(graphics2D,// "发件⼈:"+DIAN_MING+"(微信:yichenbaby0)", 10, yIndex,// lineWidth, lineHeight);yIndex = drawString(graphics2D, "收货⼈:" + orderVo.getBuyer(), 10, yIndex, lineWidth, lineHeight);yIndex = drawString(graphics2D, "收货⼈备注信息:"+ orderVo.getRemark(), 10, yIndex + lineHeight,lineWidth, lineHeight);// 收货信息边框Stroke stroke = new BasicStroke(0.5f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL, 0, new float[] { 4, 4 }, 0);graphics2D.setStroke(stroke);graphics2D.drawRect(5, 10, 129, yIndex);// 商品名称lineWidth = 129;lineHeight = 8;graphics2D.setFont(new Font("宋体", Font.BOLD, 8));graphics2D.setColor(defaultColor);yIndex = drawString(graphics2D, DIAN_MING, 5, yIndex+ lineHeight + 20, lineWidth, 12);graphics2D.setFont(new Font("宋体", Font.PLAIN, 6));graphics2D.setColor(grey);// yIndex = drawString(graphics2D, "操作员:⾦⾖ "// + getCurrDate(), 5, yIndex + lineHeight + 2,// lineWidth, lineHeight);yIndex = drawString(graphics2D, "时间:"+ getCurrDate(), 5, yIndex + lineHeight + 2,lineWidth, lineHeight);// yIndex = drawString(graphics2D, "⽇期:"+getCurrDate(), 5 +// lineWidth/2, yIndex, lineWidth, lineHeight);// yIndex = drawString(graphics2D, "⽇期:"+getCurrDate(), 5 +// lineWidth/2, yIndex, lineWidth, lineHeight);yIndex = drawString(graphics2D, "商品名", 5, yIndex+ lineHeight * 2 - 5, lineWidth, lineHeight);// yIndex = drawString(graphics2D, "尺码", (lineWidth/10)*4,// yIndex, lineWidth, lineHeight);yIndex = drawString(graphics2D, "单价", (lineWidth / 10) * 8,yIndex, lineWidth, lineHeight);yIndex = drawString(graphics2D, "数量",(lineWidth / 10) * 10, yIndex, lineWidth,lineHeight);//总合计价格int total = 0;double totalPrice = 0;for (ProductVo productVo : products) {graphics2D.setFont(new Font("宋体", Font.PLAIN, 7));//商品名称yIndex = drawString(graphics2D, productVo.getName(), 5,yIndex + 15, (lineWidth / 10) * 7, 10);graphics2D.setFont(new Font("宋体", Font.PLAIN, 6));graphics2D.setColor(grey);// yIndex = drawString(graphics2D, "尺码:" + "100M", 5,// yIndex + 11, lineWidth, lineHeight);// 尺码//单价yIndex = drawString(graphics2D, productVo.getPrice()+"",(lineWidth / 10) * 8, yIndex, lineWidth,lineHeight);//数量yIndex = drawString(graphics2D, productVo.getAmount()+"",(lineWidth / 10) * 10, yIndex, lineWidth,lineHeight);graphics2D.setFont(new Font("宋体", Font.PLAIN, 7));yIndex = yIndex + 2;graphics2D.drawLine(5, yIndex, 5 + lineWidth, yIndex);total=total+productVo.getAmount();totalPrice=totalPrice+(productVo.getPrice()*productVo.getAmount()); }graphics2D.setColor(defaultColor);// yIndex = drawString(graphics2D, "会员名称:⼩清新", 5, yIndex +// lineHeight * 2, lineWidth, lineHeight);yIndex = drawString(graphics2D, "总数:"+total, 5, yIndex+ lineHeight, lineWidth, lineHeight);yIndex = drawString(graphics2D, "总计:"+totalPrice, 5, yIndex+ lineHeight, lineWidth, lineHeight);// yIndex = drawString(graphics2D, "收款:100.00", 5, yIndex// + lineHeight, lineWidth, lineHeight);// yIndex = drawString(graphics2D, "找零:44.70", 5, yIndex// + lineHeight, lineWidth, lineHeight);graphics2D.setFont(new Font("宋体", Font.PLAIN, 6));graphics2D.setColor(grey);// yIndex = drawString(graphics2D, "微信:"+orderVo.getWxno(), 5, yIndex // + lineHeight * 2, lineWidth, lineHeight);yIndex = drawString(graphics2D, "微信:"+MAIJIA_WX, 5, yIndex+ lineHeight * 2, lineWidth, lineHeight);// yIndex = drawString(graphics2D, "地址:" + orderVo.getAddress(), 5,// yIndex + lineHeight, lineWidth, lineHeight);yIndex = drawString(graphics2D, "地址:" + MAIJIA_DIZHI, 5,yIndex + lineHeight, lineWidth, lineHeight);yIndex = yIndex + 20;graphics2D.drawLine(0, yIndex, 140, yIndex);return PAGE_EXISTS;}private String getCurrDate() {Date currDate = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");return sdf.format(currDate);}}, pageFormat);// 获取默认打印机PrinterJob printerJob = PrinterJob.getPrinterJob();printerJob.setPageable(book);try {printerJob.print();} catch (PrinterException e) {e.printStackTrace();System.out.println("打印异常");}} else {System.out.println("没法发现打印机服务");}}/*** 字符串输出** @param graphics2D* 画笔* @param text* 打印⽂本* @param x* 打印起点 x 坐标* @param y* 打印起点 y 坐标* @param lineWidth* ⾏宽* @param lineHeight* ⾏⾼* @return 返回终点 y 坐标*/private static int drawString(Graphics2D graphics2D, String text, int x,int y, int lineWidth, int lineHeight) {FontMetrics fontMetrics = graphics2D.getFontMetrics();if (fontMetrics.stringWidth(text) < lineWidth) {graphics2D.drawString(text, x, y);return y;} else {char[] chars = text.toCharArray();int charsWidth = 0;StringBuffer sb = new StringBuffer();for (int i = 0; i < chars.length; i++) {if ((charsWidth + fontMetrics.charWidth(chars[i])) > lineWidth) { graphics2D.drawString(sb.toString(), x, 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;}}}⼆、需要⽤到的两个对象package org.jeecg.print;import lombok.Data;/*** @Author: shafei* @Date: 2020-04-15 10:33* @Version: V1.0* @Description:*/@Datapublic class ProductVo {private String name;private double price;private int amount;private String size;private String iphone;private String wxno;}package org.jeecg.print;import lombok.Data;/*** @Author: shafei* @Date: 2020-04-15 10:31* @Version: V1.0* @Description:*/@Datapublic class OrderVo {private String buyer;private String remark;private String seller;private String address;private String iphone;private String wxno;}。
Java_实现iReport打印
iReport报表打印功能代码编写环境系统:windows xp开发工具:Myeclipes6.0JDK版本:Java6(jdk6.0,jre6.0)服务器:Tomcat5.5Ireport版本:iReport-2.0.5 windows 安装版(iReport-2.0.5-windows-installer.exe)实现步骤一、iReport-2.0.5安装。
选择安装路径默认安装(一直点击下一步)。
二、将iReprot的jasperreports-2.0.5.jar文件复制到Myeclipes中你工程的WEB-INF/lib目录下。
jasperreports-2.0.5.jar文件所在位置在你iReprot的安装路径下,我的是C:\Program Files\JasperSoft\iReport-2.0.5\lib。
三、要实现打印的Jsp文件编写,Jsp文件中打印按钮或者打印连接应该提交给一个javascript,具体代码如:<a href="javascript: print(${exammanage.oid })"><fontcolor="blue">打印</font></a>javascript代码如下function print(oid){if(!confirm("确定要打印该资格证吗?"))return ;window.showModalDialog('${ctx}/exam/exammanage/examprint_cert.jsp?oid='+oid,'','dialogWidth:50px;dialogHeight:150px;dialogTop:1000px;dialogLef t:1000px');document.forms[0].flg.value = "0";document.forms[0].action="${ctx}/ExamPermitPrint.html";document.forms[0].submit();}代码解释:1、window.showModalDialog('${ctx}/exam/exammanage/examprint_cert.jsp?oid='+oid,'','dialogWidth:50px;dialogHeight:150px;dialogTop:1000px;dialogLef t:1000px');此段的功能是显示打印提示窗口,我的文件是WebRoot路径下/exam/exammanage/路径下的examprint_cert.jsp文件,而且需要传一个你所要打印的记录的唯一字段(数据库中唯一代表一条记录的字段),我这里用OID。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
猪哥解答:
我这里有以前收藏的代码,两个类实现了简易的文本打印机的功能,包括预览。
简单跟你说一下。
1、PrinterDemo.java主体类,也是入口类,里面有main方法可以直接在Eclipse 中调试运行,他实现了从本地磁盘读取文本类文件打印以及打印预览的功能,其中File动作按钮中的PrintPreviw就是打印预览功能,你可以运行看看。
2、PrintPreview.java打印预览类,这是专门为预览打印设计的类,通过他的构造方法可以构造出一个预览类,PrinterDemo中的预览功能就是调用了这个类。
两个类的源码:
【PrinterDemo.java】的源码:
【PrintPreview.java】的源码:
两个类放在同一个包下。