diff --git a/README.md b/README.md index 8490924..9d9f4ed 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,9 @@ import com.github.anastaciocintra.escpos.EscPos; import com.github.anastaciocintra.output.PrinterOutputStream; import javax.print.PrintService; +import javax.print.attribute.HashPrintRequestAttributeSet; +import javax.print.attribute.PrintRequestAttributeSet; +import javax.print.attribute.standard.JobName; import java.io.IOException; public class HelloWorld { @@ -53,6 +56,10 @@ public class HelloWorld { PrintService printService = PrinterOutputStream.getPrintServiceByName(args[0]); PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService); + PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet(); + attributes.add(new JobName("your job name", null)); + printerOutputStream.setPrintRequestAttributeSet(attributes); + EscPos escpos = new EscPos(printerOutputStream); escpos.writeLF("Hello world"); escpos.feed(5).cut(EscPos.CutMode.FULL); diff --git a/src/main/java/com/github/anastaciocintra/output/PrinterOutputStream.java b/src/main/java/com/github/anastaciocintra/output/PrinterOutputStream.java index 031564e..9da833c 100644 --- a/src/main/java/com/github/anastaciocintra/output/PrinterOutputStream.java +++ b/src/main/java/com/github/anastaciocintra/output/PrinterOutputStream.java @@ -18,6 +18,7 @@ import javax.print.PrintService; import javax.print.PrintServiceLookup; import javax.print.SimpleDoc; +import javax.print.attribute.PrintRequestAttributeSet; /** * Supply OutputStream to the printer. @@ -30,6 +31,7 @@ public class PrinterOutputStream extends PipedOutputStream { protected final PipedInputStream pipedInputStream; protected final Thread threadPrint; + private PrintRequestAttributeSet printRequestAttributeSet=null; /** * creates one instance of PrinterOutputStream. @@ -57,7 +59,7 @@ public PrinterOutputStream(PrintService printService) throws IOException { Doc d = new SimpleDoc(pipedInputStream, df, null); DocPrintJob job = printService.createPrintJob(); - job.print(d, null); + job.print(d, this.printRequestAttributeSet); } catch (PrintException ex) { throw new RuntimeException(ex); } @@ -164,4 +166,18 @@ public static PrintService getPrintServiceByName(String printServiceName) { return foundService; } + /** + * Set printer request attributes. + *
+ * For example Job name: + * attributes.add(new JobName("your job name", null)); + * Null by default + * Could be set before each printer job + * Save previous state attributes for next jobs. + *
+ * @param attributes printer request attributes. + */ + public void setPrintRequestAttributeSet(PrintRequestAttributeSet attributes){ + this.printRequestAttributeSet = attributes; + } }