有人可以幫助我編寫一個簡單的java程序,可以從打印機打印字符串。我在javaDocs網站上看到了一些例子,但是它們都很長,我甚至找不出要打印哪個字符串,因爲按鈕名稱,方法名稱等都是相同的。我不介意如果你添加或不添加圖形用戶界面的程序,生病了(可能是,但評論將有助於)我自己。 P.S.我的意思是打印紙張的打印機打印字符串到打印機使用Java
謝謝。
有人可以幫助我編寫一個簡單的java程序,可以從打印機打印字符串。我在javaDocs網站上看到了一些例子,但是它們都很長,我甚至找不出要打印哪個字符串,因爲按鈕名稱,方法名稱等都是相同的。我不介意如果你添加或不添加圖形用戶界面的程序,生病了(可能是,但評論將有助於)我自己。 P.S.我的意思是打印紙張的打印機打印字符串到打印機使用Java
謝謝。
import java.io.FileInputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
public class Main {
static public void main(String args[]) throws Exception {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
if (pss.length == 0)
throw new RuntimeException("No printer services available.");
PrintService ps = pss[0];
System.out.println("Printing to " + ps);
DocPrintJob job = ps.createPrintJob();
FileInputStream fin = new FileInputStream("YOurImageFileName.PNG");
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
job.print(doc, pras);
fin.close();
}
}
System.out.println(「Bill printing」);
FileInputStream imagestream=new FileInputStream("C:\\Users\\DELL\\Desktop\\DummyImage.jpg");
DocFlavor doc=DocFlavor.INPUT_STREAM.JPEG;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1));
//aset.add(MediaSizeName.NA_5X7);
aset.add(Sides.ONE_SIDED);
aset.add(OrientationRequested.PORTRAIT);
Doc document = new SimpleDoc(imagestream, doc, null);
// DocPrintJob job = service.createPrintJob();
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
for (int i = 0; i < services.length; i++) {
System.out.println(services[i].getName());
}
Media[] res1 = (Media[]) services[0].getSupportedAttributeValues(Media.class, null, null);
for (Media media : res1) {
if (media instanceof MediaSizeName) {
MediaSizeName msn = (MediaSizeName) media;
MediaSize ms = MediaSize.getMediaSizeForName(msn);
float width = ms.getX(MediaSize.INCH);
float height = ms.getY(MediaSize.INCH);
System.out.println(media + ": width = " + width + "; height = " + height);
}
}
DocPrintJob job = services[4].createPrintJob();
job.print(document, aset);
我知道這個問題是4歲,但也許有人使用這個簡單的解決方案:
String YourString = "first line \n second line \n etc.etc."; //define and set contents of your string - remember about formating with \n if you want to have it split on lines
JTextArea YourTextArea = new JTextArea(); //define new Swing JtextArea
YourTextArea.setLineWrap(true); //set line wrap for YourTextArea - this will prevent too long lines to be cut - they will be wrapaed to next line
YourTextArea.append(YourString); //append YourString to YourTextArea
YourTextArea.print(); //this will display print dialog that will lead you to print contents of YourTextArea so in efect contents of YourString
你的意思是文字打印機,一些打印紙,或特定類的Java API中(如PringWriter或PrintStream)? – 2012-07-27 03:16:20
類似的問題: http://stackoverflow.com/questions/5325530/java-text-printing-framework – 2012-07-27 03:23:11
@MuhammadGelbana我喜歡'Java報表打印Library'我將需要騰出時間來看看這個。謝謝 ;) – MadProgrammer 2012-07-27 03:32:28