简体   繁体   English

.txt中的Java收据文件

[英]Java receipt file from .txt

I'm creating a program which will print a .txt receipt of the bought items from the store and it prints successfully, but I need to change the font of some specific part of the text is it possible? 我正在创建一个程序,它将打印从商店购买的商品的.txt收据,并且可以成功打印,但是我需要更改文本某些特定部分的字体吗? for example I wanted to print something like this 例如我想打印这样的东西

THIS SHOULD BE BOLD and BIGGER 这应该大胆和更大

this should be smaller than the first line 这应该小于第一行

This should be italicized 应该斜体显示

would it be possible? 这有没有可能?

UPDATE UPDATE

here is the code of the printing process 这是打印过程的代码

    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) {
    }
  }

Example for Bold (assuming that your printer has the same escape sequences as in the code you linked to). 粗体示例(假设您的打印机具有与链接的代码相同的转义序列)。

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;

Now toPrintInBold is ready to be set to the printer. 现在,可以将toPrintInBold设置为打印机。

If it works, then you have a method and just need more control codes. 如果可行,则您有一种方法,只需要更多控制代码即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM