来自 .txt 的 Java 收据文件

标签 java printing

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

THIS SHOULD BE BOLD and BIGGER

this should be smaller than the first line

This should be italicized

有可能吗?

更新

这是打印过程的代码

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

最佳答案

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

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 已准备好设置到打印机。

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

关于来自 .txt 的 Java 收据文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24793655/

相关文章:

java - 如何在 jsf 中使用接缝 validator ?

java - 从内部类访问外部类 : Why is it done this way?

java - Spring Data错误由: org. hibernate.QueryException引起:无法解析属性

javascript - 如何使用 MVC 和 Javascript 隐藏打印内容?

html - 内部水平分页?

java - 在一维数组/直方图中查找局部最小值/最大值

java - @OneToMany 关系使用单个@JoinColumn?

android - 如何打印整个 WebView(可滚动)?

css - 是否可以将打印机设置为仅打印 CSS 中的一页?

r - 如何从函数内打印空行?