java打印机代码_ java打印命令

用java实现打印功能 怎么写 求大神求源码PrintService[] services = PrinterJob.lookupPrintServices();//查找系统中所有的可用打印服务
PrintService service = ServiceUI.printDialog(null, 500, 500, services, services[0], null, new HashPrintRequestAttributeSet());//创建一个默认的打印机选择对话框
DocPrintJob job = service.createPrintJob();//创建一个打印任务
Doc doc = new SimpleDoc(new FileInputStream(new File("D:\\image\\MM.jpg")), DocFlavor.INPUT_STREAM.JPEG, null);//创建一个打印的文档
job.print(doc, null);//打印
这是jdk提供的打印接口 , 你也可以找找其它的第三方类库,jdk提供打印接口功能一般般
如何通过JAVA代码调用网络打印机使其打印接收到的文件用java实现本地打?。簀ava.awt中提供了一些打印的API,要实现打?。紫纫竦么蛴《韵?,然后继承Printable实现接口方法print,以便打印机进行打印,最后用用Graphics2D直接输出直接输出 。下面代码实现了简单的打印功能:
怎么用java代码 获取打印机列表import javax.print.DocFlavor;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.PrintRequestAttributeSet;
public class PrintTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.PNG;
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
}
[Win32 Printer : Zan 黑白图像打印机,
Win32 Printer : Zan 彩色图像打印机,
Win32 Printer : Microsoft XPS Document Writer,
Win32 Printer : Fax,
【java打印机代码_ java打印命令】Win32 Printer : \\192.168.30.127\HP LaserJet 1020,
Win32 Printer : \\192.168.31.68\Brother MFC-8220 USB Printer]
java 调用打印机API无法打印 , 但是直接打印可以,请问有人遇到过这样的问题吗?代码逻辑有问题:
if (printService.length0) {
// 这个地方应该判断activePrintService != null,
因为你是用的打印机是activePrintService来打印,
而不是判断系统有有打印机就调用activePrintService来打印 。
JAVA中怎么编写打印功能代码Print.java--打印内容定义[code]import java.awt.*;import java.awt.event.*;import java.awt.print.*;import java.awt.image.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.border.*;import javax.swing.event.*;import java.util.*;import java.io.*;public class Print implements Printable{int m_wPage;int m_hPage;int m_orientation;Printable m_target;int maxNumPage=1;String title="数据表格打印";Font titleFont=new Font("黑体",Font.BOLD,14);boolean hasTail=true;int tailAlign=0;int headAlign=0;int topSpace=0;int leftSpace=0;int yStart=0;int yEnd=0;int xStart=topSpace;int xEnd=0;int x=0,y=0;String strTemp="打印内容";public void doPrint(){try{m_orientation=PageFormat.PORTRAIT;//设置打印对象,默认纸张PrinterJob prnJob=PrinterJob.getPrinterJob();PageFormat pageFormat=prnJob.defaultPage();pageFormat.setOrientation(m_orientation);m_wPage=(int)(pageFormat.getWidth());m_hPage=(int)(pageFormat.getHeight());//将待打印的窗体根据默认纸张设置传入打印对象prnJob.setPrintable(this,pageFormat);if(!prnJob.printDialog()) return;prnJob.print();}catch(PrinterException ex){ex.printStackTrace();System.err.println("打印错误:"+ex.toString());}}/*** 初始化打印参数*/public void initPrintParameter(){}/***构造打印内容,以送打印机打印*/public int print(Graphics pg,PageFormat pageFormat,int pageIndex) throws PrinterException{//初始化打印参数initPrintParameter();//将画布设置为页面大小pg.translate((int)pageFormat.getImageableX(),(int)pageFormat.getImageableY());int wPage=0;int hPage=0;//根据打印机页面设置调整画布大小if(pageFormat.getOrientation()==pageFormat.PORTRAIT){wPage=(int)pageFormat.getImageableWidth();hPage=(int)pageFormat.getImageableHeight();}else{wPage=(int)pageFormat.getImageableWidth();wPage+=wPage/2;hPage=(int)pageFormat.getImageableHeight();pg.setClip(0,0,wPage,hPage);}wPage=wPage-2*leftSpace;hPage=hPage-2*topSpace;xStart=leftSpace;xEnd=wPage-2;//为画布设置颜色和字体int y=topSpace;pg.setFont(titleFont);pg.setColor(Color.black);//画标题,并使其居中Font fn=pg.getFont();FontMetrics fm=pg.getFontMetrics();y+=fm.getAscent();alignText(title,pg,y,xStart,xEnd,headAlign);y+=30;x=leftSpace+2;Font headerFont=new Font("宋体",Font.BOLD,14);pg.setFont(headerFont);fm=pg.getFontMetrics();int h=fm.getAscent();yStart=y-1;y+=h;pg.setFont(headerFont);fm=pg.getFontMetrics();int header=y;h=fm.getHeight();//计算行高,每页行数,总行数和指定页码的起始行、结束行int rowH=Math.max(h,10);int tailH=rowH+30;int rowPerPage=0;int leftPix=0;if(hasTail){rowPerPage=(hPage-header-tailH)/rowH;leftPix=(hPage-header-tailH)%rowH;yEnd=hPage-leftPix-tailH+2;}else{rowPerPage=(hPage-header)/rowH;leftPix=(hPage-header)%rowH;yEnd=hPage-leftPix+2;}pg.drawString(strTemp,x,y);//画表格边框pg.drawLine(xStart,yStart,xStart,yEnd);pg.drawLine(xStart,yStart,xEnd,yStart);pg.drawLine(xEnd,yStart,xEnd,yEnd);pg.drawLine(xStart,yEnd,xEnd,yEnd);//打印页码if(hasTail){int pageNumber=pageIndex+1;String s="第"+pageNumber+"页";alignText(s,pg,yEnd+30,xStart,xEnd,tailAlign);}System.gc();return PAGE_EXISTS;}/*** 文字排列,坐标在y处,显示范围(start-end)* 0表示居中显示 , 1表示左对齐 , 2表示右对齐*/private void alignText(String s,Graphics pg,int y,int start,int end,int mode){Font fn=pg.getFont();FontMetrics fm=pg.getFontMetrics();int wString=fm.stringWidth(s);int x=start;switch(mode){case 0:if((end-start-wString)0) x=start+(end-start-wString)/2;break;case 1:break;case 2:if((end-start-wString)0) x=start+(end-start-wString);break;}pg.drawString(s,x,y);}public static void main(String[] args){Print p=new Print();p.doPrint();}}[code]运行方法:javac -d . Print.javajava Print自己运行一下from:

推荐阅读