PrinterJob - 选择打印机|| DocPrintJob格式化页面

时间:2016-03-14 16:25:12

标签: java printing

我无动于衷,但需要尽快开展工作。

PrinterJob的使用效果很好,但打印机必须是Windows中的默认打印机才能工作。如果不是,则不会打印。在这种情况下,任何人都知道如何选择特定的打印机?

DocPrintJob允许我使用特定的打印机,但格式化完全关闭。它在左边有大约1英寸的边距,然后切断长44字符串。这是打印收据打印机。

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
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.util.ArrayList;

import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;

public class PrinterExample {
String receiptPrinter = "Star TSP100 Cutter (TSP143)";

public PrinterExample(ArrayList<String> text) throws PrintException, PrinterException {

    // this works perfectly, but the printer
    // has to be the default printer
    PrinterJob job2 = PrinterJob.getPrinterJob();
    PageFormat pf = job2.defaultPage();
    double margin = 1.0;
    Paper paper = new Paper();
    paper.setSize(216.0, (double) (paper.getHeight() + text.size() * 10.0));
    paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2);
    pf.setPaper(paper);
    pf.setOrientation(PageFormat.PORTRAIT);
    job2.setPrintable(new ThePrintable(text), pf);
    job2.print();

    // this doesn't take into account the
    // size of the sheet of paper and chops off
    // the text

    DocPrintJob job = getPrinterJob(receiptPrinter);
    DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
    SimpleDoc doc = new SimpleDoc(new ThePrintable(text), flavor, null);
    job.print(doc, null);

}

public static void printIdentificationPages() {
    DocPrintJob job = null;
    PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
    System.out.println("Number of print services: " + printServices.length);

    for (PrintService printer : printServices) {
        System.out.println("Printer: " + printer.getName());
        job = printer.createPrintJob();
        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
        ArrayList<String> printArray = new ArrayList<>();
        printArray.add("This is a test page to");
        printArray.add("help you identify the");
        printArray.add("printer you want to use.");
        printArray.add("");
        printArray.add(printer.getName());

        SimpleDoc doc = new SimpleDoc(new ThePrintable(printArray), flavor, null);
        try {
            job.print(doc, null);
        } catch (PrintException e) {
            e.printStackTrace();
        }
    }
}

private static DocPrintJob getPrinterJob(String printerToUse) {
    DocPrintJob job = null;
    PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
    System.out.println("Number of print services: " + printServices.length);

    for (PrintService printer : printServices) {
        System.out.println("Printer: " + printer.getName());
        if (printer.getName().equals(printerToUse)) {
            job = printer.createPrintJob();
        }
    }
    return job;
}

public static void main(String... args) {
    ArrayList<String> printArray = new ArrayList<>();
    printArray.add("Hello");
    printArray.add("World!");
    printArray.add("");
    printArray.add("12345678901234567890123456789012345678901234");
    printArray.add("");

    try {
        new PrinterExample(printArray);
    } catch (PrintException | PrinterException e) {
        e.printStackTrace();
    }

    // Printer.printIdentificationPages();
}

}

class ThePrintable implements Printable {

ArrayList<String> text = new ArrayList<>();

public ThePrintable(ArrayList<String> text) {
    this.text = text;
}

public int print(Graphics g, PageFormat pf, int page) {
    if (page > 0) {
        return NO_SUCH_PAGE;
    }
    int i;
    Graphics2D g2d = (Graphics2D) g;
    Line2D.Double line = new Line2D.Double();
    g2d.setColor(Color.black);
    g2d.translate(pf.getImageableX(), pf.getImageableY());
    int y = 15;

    g.setFont(new Font("Consolas", Font.BOLD, 8));
    for (String s : text) {
        g2d.drawString(s, 1, y);
        y += 15;
    }

    return PAGE_EXISTS;
}
}

1 个答案:

答案 0 :(得分:0)

这是解决方案......第二个只需要一些属性

DocAttributeSet das = new HashDocAttributeSet();
das.add(PrintQuality.HIGH);
das.add(MediaSizeName.ISO_A4);

SimpleDoc doc = new SimpleDoc(new MyPrintable(printArray, false), flavor, das);