java打印文本文件

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.PrinterName;
import javax.swing.JPanel;

//import com.fujitsu.systemwalker.guiparts.MpGuiptConstants;
//import com.fujitsu.systemwalker.guiparts.MpGuiptTrace;
/**

 *
 */
public class MpGuiptTextPrint {
	private PrintPanelComp printPanel;	//
	/**

	 */
	public static final int PRINT_OK = 0;
	/**

	 */
	public static final int PRINT_CANCEL = 1;
	/**

	 */
	public static final int PRINT_NODATA = -1;
	/**

	 */
	public static final int PRINT_ERROR = -2;
	/**

	 */
	public static final int PRINT_NO_PRINTER = -3;
	/**

	 */
	public MpGuiptTextPrint(){
		printPanel = new PrintPanelComp();
	}
	/**

	 */
	public int printDialog(String[] printData,String jobString,String printerName){
		printPanel.setPrintData(printData);
		if (!printPanel.isGoPrint()){
			return PRINT_NODATA;
		}
		//
		PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
		//
		aset.add(new JobName(jobString, null));
		//
		PrinterJob pj = PrinterJob.getPrinterJob();
		pj.setPrintable(printPanel);
		
		//get printer service
		PrintService printService =null;
		if (printerName == null){
			printService = PrintServiceLookup.lookupDefaultPrintService();
		}
		else{
			//find printer
			//HashAttributeSet hs = new HashAttributeSet();
			//hs.add(new PrinterName(printerName, null));
			//boolean found_print =false;
			PrintService[] services = PrintServiceLookup.lookupPrintServices(null,null);
			if (services.length > 0) {
				for (int i = 0; i < services.length; i++) {
					if (printerName.equals(services[i].getName())){
						printService = services[i];
					}
				}
			}
			else{
				printPanel.setPrintData(null);
				return PRINT_NO_PRINTER;
			}
			

		}
		
		if (printService ==null){
			printPanel.setPrintData(null);
			return PRINT_NO_PRINTER;
		}

		try {
			pj.setPrintService(printService);
			//System.out.println("printer:" + services[0].getName());
			pj.print(aset);
			return PRINT_OK;
		} catch (PrinterException e) {
			// TODO Auto-generated catch block

			// e.printStackTrace();
			return PRINT_ERROR;
		}

	}

	/**
	 */
	public void setFont(Font font){
		printPanel.setFont(font);
	}
	/**
	 */
	public Font getFont(){
		return printPanel.getFont();
	}
	/**
	 */
	private class PrintPanelComp extends JPanel implements Printable {
		private int page;
		private int[] lineStart;
		private int[] charStart;
		private boolean pageCheck;
		private String[] stringData;
		/**
		 */
		public PrintPanelComp(){
			setBackground(Color.WHITE);
			stringData = null;
			pageCheck = false;
		}
		/**
		 */
		public boolean isGoPrint(){
			if (stringData == null ||
					stringData.length <= 0){
				return false;
			}
			return true;
		}
		/**
		 */
		public void setPrintData(String[] stringData){
			this.stringData = stringData;
			pageCheck = false;
		}
		/**
		 */		
		public int print(Graphics g, PageFormat fmt, int index) throws PrinterException {
			if (!isGoPrint()){
				return NO_SUCH_PAGE;
			}

			FontMetrics metrics=g.getFontMetrics();
			//
			if(!pageCheck){
				chaeckPageAttribute(metrics,fmt);
			}
			//
			if(index>=page){						//
				pageCheck=false;	//
				return NO_SUCH_PAGE;
			}else{									//
				//
				int x=(int)fmt.getImageableX();
				int y=(int)fmt.getImageableY();
				g.translate(x,y);
				//
				int px=0;
				int py=metrics.getHeight();
				//
				for(int l=lineStart[index];l<stringData.length;l++){
					//
					int s;
					if(l!=lineStart[index]){
						s=0;
					}else{
						s=charStart[index];
					}
					//
					for(int c=s;c<stringData[l].length();c++){
						//
						if (stringData[l].charAt(c) == '\f'){
							return PAGE_EXISTS;
						}
						//
						int w=px+metrics.charWidth(stringData[l].charAt(c));
						//
						if(w>fmt.getImageableWidth()){
							px=0;
							py+=metrics.getHeight();
							//
							if(py>fmt.getImageableHeight()){
								return PAGE_EXISTS;
							}
						}
						//
						g.drawString(String.valueOf(stringData[l].charAt(c)),px,py);
						//
						px+=metrics.charWidth(stringData[l].charAt(c));
					}
					//
					px=0;
					py+=metrics.getHeight();
					//
					if(py>fmt.getImageableHeight()){
						return PAGE_EXISTS;
					}
				}
				//
				return PAGE_EXISTS;
			}
		}
		/**
		 */
		private void chaeckPageAttribute(FontMetrics metrics,PageFormat fmt){
			ArrayList<Integer> lineArray=new ArrayList<Integer>();
			ArrayList<Integer> charArray=new ArrayList<Integer>();
			//
			int px=0;
			int py=metrics.getHeight();
			//
			lineArray.add(Integer.valueOf(0));
			charArray.add(Integer.valueOf(0));
			//
			for(int l=0;l<stringData.length;l++){
				for(int c=0;c<stringData[l].length();c++){
					//
					if (stringData[l].charAt(c) == '\f'){
						px=0;
						py=metrics.getHeight();
						//
						if (c == stringData[l].length() - 1){
							//
							lineArray.add(Integer.valueOf(l + 1));
							charArray.add(Integer.valueOf(0));
						}
						else{
							//
							lineArray.add(Integer.valueOf(l));
							charArray.add(Integer.valueOf(c + 1));
						}
					}
					else{
						//
						int w=px+metrics.charWidth(stringData[l].charAt(c));
						//
						if(w>fmt.getImageableWidth()){
							px=0;
							py+=metrics.getHeight();
							//
							if(py>fmt.getImageableHeight()){
								px=0;
								py=metrics.getHeight();
								//
								lineArray.add(Integer.valueOf(l));
								charArray.add(Integer.valueOf(c));
							}
						}
						//
						//
						px+=metrics.charWidth(stringData[l].charAt(c));
					}
				}
				//
				px=0;
				py+=metrics.getHeight();
				//
				if(py>fmt.getImageableHeight()){
					px=0;
					py=metrics.getHeight();
					//
					if(l!=stringData.length-1){
						lineArray.add(Integer.valueOf(l+1));
						charArray.add(Integer.valueOf(0));
					}
				}
			}
			//
			page=lineArray.size();
			lineStart=new int[page];
			charStart=new int[page];
			for(int p=0;p<page;p++){
				lineStart[p]=((Integer)lineArray.get(p)).intValue();
				charStart[p]=((Integer)charArray.get(p)).intValue();
			}
			//
			pageCheck=true;
		}
		
	}
	
	public static void main(String[] args) throws Exception{
		
		
		MpGuiptTextPrint mptp = new MpGuiptTextPrint();
		
		//
		

		String fileName = "/tmp/3.txt";
		String encode = "shift_jis";

		InputStreamReader read = null;
		BufferedReader reader = null;

		String lineIn = null;
		StringBuffer buffer = new StringBuffer();
		try {
			read = new InputStreamReader(new FileInputStream(fileName), encode);
			reader = new BufferedReader(read);
			while ((lineIn = reader.readLine()) != null) {
				if (buffer.length() != 0) {
					buffer.append("\r\n");
				}
				buffer.append(lineIn.trim());
			}
		} catch (Exception e) {
			throw e;
		} finally {
			read.close();
			reader.close();
		}
		
		int i = mptp.printDialog(buffer.toString().split("\r\n"), "javaprint","ppp2");
		System.out.println("i:"+i);
	}
}





AttributeSet aset = new HashAttributeSet();
    aset.add(new PrinterName("HP LaserJet", null));
    services = PrintServiceLookup.lookupPrintServices(null, aset);

你可能感兴趣的:(java)