Printing

Aus AmIHereWiki
Zur Navigation springen Zur Suche springen
  • Simple PCL print commands as java application
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

/**
 * @author amihere
 * @company wiki
 * @project formular
 * @version 1.0
 * @date 19.05.2011
 */
public class SimplePrintPCL {

  /**
   * @param args
   * @throws FileNotFoundException
   */
  public static void main(final String[] args) throws FileNotFoundException {

    final String esc = Character.toString((char) 27); // escape

    final String formfeed = Character.toString((char) 12); // form feed, next page

    //devices = "prn:", "lpt1:", "com1:", "ttyS0:" .
    final String local = "\\\\localhost\\druckerfreigabe";

    final String device = ((args.length > 0) && (!args[0].isEmpty())) ? args[0] : local; // printer share on windows

    System.out.println("Ready... \n");
    System.out.println("    device by cmd line argument: java SimplePrintPCL \"\\\\pc\\printshare\")");
    System.out.println(" or device by cmd line argument: java SimplePrintPCL \"prn:");
    System.out.println(" or device by cmd line argument: java SimplePrintPCL \"/dev/lpt0" + "\n");
    System.out.print("Device: ");

    System.out.println("[ " + device + " ] is printing... \n");

    final OutputStream fos = new FileOutputStream(device); // placed here to get error message after first lines  

    final PrintWriter pw = new PrintWriter(fos);

    pw.println(esc + "%-12345X"); // UEL Command (exit language)
    pw.println(esc + "E"); // Printer Reset Command.
    pw.println(esc + "&l1S"); // Duplex

    pw.println("Seite 1/6 duplex formfeed ->");
    pw.println("Seite 1 und 2 auf einem Blatt, Vorder/Rückseite bedruckt");
    pw.println(formfeed); // form feed, soft page break
    pw.println("Seite 2/6");
    pw.println(formfeed); // form feed, soft page break

    pw.println(esc + "&l0S"); // Simplex
    pw.println("Seite 3/6 simplex formfeed ->");
    pw.println("Seite 3 auf einem Blatt, Vorderseite bedruckt");
    pw.println(formfeed); // form feed, soft page break
    pw.println("Seite 4/6");
    pw.println("Seite 4 auf einem Blatt, Rückseite bedruckt");
    pw.println(formfeed); // form feed, soft page break

    pw.println(esc + "&l1S"); // Duplex
    pw.println("Seite 5/6 duplex &l26A ->");
    pw.println("Seite 5 auf einem Blatt, Vorderseite bedruckt");

    pw.println(esc + "&l26A"); // form feed, hard page break (new paper/letter)
    pw.println("Seite 6 auf einem Blatt, Vorderseite bedruckt, wenn Rückseite bedruckt: Problem mit &l26A PCL Command");
    pw.println("Seite 6/6");

    pw.println(esc + "E"); // Printer Reset Command.
    pw.println(esc + "%-12345X"); // UEL Command (exit language)
    pw.close();

    System.out.println("Done.");
  }
}