来自.txt的Java收据文件

时间:2014-07-17 01:53:53

标签: java printing

我正在创建一个程序,该程序将从商店打印购买商品的 .txt 收据 它打印成功,但我需要更改文本的某些特定部分的字体是否可能? 例如,我想打印像这样的东西

  

这应该是大胆的和更大的

     

这应该小于第一行

     

这应该是斜体

会有可能吗?

更新

这是打印过程的代码

    public static void main(String[] args) throws PrintException, IOException {
    String defaultPrinter =PrintServiceLookup.lookupDefaultPrintService().getName();
    System.out.println("Default printer: " + defaultPrinter);

    PrintService service = PrintServiceLookup.lookupDefaultPrintService();

    FileInputStream in = new FileInputStream(new File("sample.txt"));

    PrintRequestAttributeSet  pras = new HashPrintRequestAttributeSet();
    pras.add(new Copies(1));


    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    Doc doc = new SimpleDoc(in, flavor, null);

    DocPrintJob job = service.createPrintJob();
    PrintJobWatcher pjw = new PrintJobWatcher(job);
    job.print(doc, pras);
    pjw.waitForDone();
    in.close();

    // send FF to eject the page
    InputStream ff = new ByteArrayInputStream("\f".getBytes());
    Doc docff = new SimpleDoc(ff, flavor, null);
    DocPrintJob jobff = service.createPrintJob();
    pjw = new PrintJobWatcher(jobff);
    jobff.print(docff, null);
    pjw.waitForDone();
  }
}

class PrintJobWatcher {
  boolean done = false;

  PrintJobWatcher(DocPrintJob job) {
    job.addPrintJobListener(new PrintJobAdapter() {
      public void printJobCanceled(PrintJobEvent pje) {
        allDone();
      }
      public void printJobCompleted(PrintJobEvent pje) {
        allDone();
      }
      public void printJobFailed(PrintJobEvent pje) {
        allDone();
      }
      public void printJobNoMoreEvents(PrintJobEvent pje) {
        allDone();
      }
      void allDone() {
        synchronized (PrintJobWatcher.this) {
          done = true;
          System.out.println("Printing done ...");
          PrintJobWatcher.this.notify();
        }
      }
    });
  }
  public synchronized void waitForDone() {
    try {
      while (!done) {
        wait();
      }
    } catch (InterruptedException e) {
    }
  }

1 个答案:

答案 0 :(得分:0)

粗体的示例(假设您的打印机具有与您链接的代码中相同的转义序列)。

public static final char ESC =  27;
public static final String BOLD_ON = ESC + "E";
public static final String BOLD_OFF = ESC + "F";

String textString = "some text";
String toPrintInBold = BOLD_ON + textString + BOLD_OFF;

现在toPrintInBold已准备好设置为打印机。

如果它有效,那么你有一个方法,只需要更多的控制代码。