Example usage for javax.print.attribute HashPrintRequestAttributeSet HashPrintRequestAttributeSet

List of usage examples for javax.print.attribute HashPrintRequestAttributeSet HashPrintRequestAttributeSet

Introduction

In this page you can find the example usage for javax.print.attribute HashPrintRequestAttributeSet HashPrintRequestAttributeSet.

Prototype

public HashPrintRequestAttributeSet() 

Source Link

Document

Construct a new, empty print request attribute set.

Usage

From source file:Print.java

public static void main(String[] args) throws IOException {
    // These are values we'll set from the command-line arguments
    boolean query = false;
    String printerName = null;//from   w  ww. j  a v a2  s .  com
    String inputFileName = null;
    String outputFileName = null;
    String outputFileType = null;
    PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();

    // Loop through the arguments
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("-q"))
            query = true; // Is this is query?
        else if (args[i].equals("-p")) // Specific printer name
            printerName = args[++i];
        else if (args[i].equals("-i")) // The file to print
            inputFileName = args[++i];
        else if (args[i].equals("-ps")) { // Print it to this file
            // Sun's Java 1.4 implementation only supports PostScript
            // output. Other implementations might offer PDF, for example.
            outputFileName = args[++i];
            outputFileType = "application/postscript";
        }
        // The rest of the arguments represent common printing attributes
        else if (args[i].equals("-color")) // Request a color printer
            attributes.add(Chromaticity.COLOR);
        else if (args[i].equals("-landscape")) // Request landscape mode
            attributes.add(OrientationRequested.LANDSCAPE);
        else if (args[i].equals("-letter")) // US Letter-size paper
            attributes.add(MediaSizeName.NA_LETTER);
        else if (args[i].equals("-a4")) // European A4 paper
            attributes.add(MediaSizeName.ISO_A4);
        else if (args[i].equals("-staple")) // Request stapling
            attributes.add(Finishings.STAPLE);
        else if (args[i].equals("-collate")) // Collate multiple copies
            attributes.add(SheetCollate.COLLATED);
        else if (args[i].equals("-duplex")) // Request 2-sided
            attributes.add(Sides.DUPLEX);
        else if (args[i].equals("-2")) // 2 pages to a sheet
            attributes.add(new NumberUp(2));
        else if (args[i].equals("-copies")) // how many copies
            attributes.add(new Copies(Integer.parseInt(args[++i])));
        else {
            System.out.println("Unknown argument: " + args[i]);
            System.exit(1);
        }
    }

    if (query) {
        // If the -q argument was specified, but no printer was named,
        // then list all available printers that can support the attributes
        if (printerName == null)
            queryServices(attributes);
        // Otherwise, look for a named printer that can support the
        // attributes and print its status
        else
            queryPrinter(printerName, attributes);
    } else if (outputFileName != null)
        // If this is not a query and we have a filename, print to a file
        printToFile(outputFileName, outputFileType, inputFileName, attributes);
    else
        // Otherwise, print to the named printer, or to the default
        // printer otherwise.
        print(printerName, inputFileName, attributes);

    // The main() method ends here, but there may be a printing thread
    // operating in the background. So the program may not terminate
    // until printing completes.
}

From source file:Main.java

public static void main(String args[]) {

    try {/*from  w w  w.ja  v a  2 s. c o  m*/
        String cn = UIManager.getSystemLookAndFeelClassName();
        UIManager.setLookAndFeel(cn); // Use the native L&F
    } catch (Exception cnf) {
    }
    PrinterJob job = PrinterJob.getPrinterJob();
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    PageFormat pf = job.pageDialog(aset);
    job.setPrintable(new Main(), pf);
    boolean ok = job.printDialog(aset);
    if (ok) {
        try {
            job.print(aset);
        } catch (PrinterException ex) {
            /* The job did not successfully complete */
        }
    }
    System.exit(0);
}

From source file:PrintDialogExample.java

public static void main(String args[]) {

    try {/*from  w w w  .j  a v  a2 s.com*/
        String cn = UIManager.getSystemLookAndFeelClassName();
        UIManager.setLookAndFeel(cn); // Use the native L&F
    } catch (Exception cnf) {
    }
    PrinterJob job = PrinterJob.getPrinterJob();
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    PageFormat pf = job.pageDialog(aset);
    job.setPrintable(new PrintDialogExample(), pf);
    boolean ok = job.printDialog(aset);
    if (ok) {
        try {
            job.print(aset);
        } catch (PrinterException ex) {
            /* The job did not successfully complete */
        }
    }
    System.exit(0);
}

From source file:Test.java

public Test() {
    this.setSize(200, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());

    JColorChooser.showDialog(this, null, Color.blue);
    JButton printDialogButton = new JButton("Print Dialog");
    printDialogButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            final PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
            attributes.add(DialogTypeSelection.COMMON);
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.printDialog(attributes);

        }//from   ww  w.  ja v a2  s.c o  m
    });
    this.add(printDialogButton);
}

From source file:com.akman.enjoyfood.SelectPrinters.java

/**
 * This method get all the printer installed in the system. Add then into
 * JTable.//from  ww  w .  j av a 2s .  c o m
 */
public void getPrinters() {

    model.setRowCount(0);

    DocFlavor doc_flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
    PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();

    PrintService[] service = PrintServiceLookup.lookupPrintServices(doc_flavor, attr_set);
    for (PrintService printService : service) {

        PrintServiceAttributeSet printServiceAttributes = printService.getAttributes();
        PrinterState printerState = (PrinterState) printServiceAttributes.get(PrinterState.class);

        if (printService != null) {
            Object row[] = { new Printer(printService.getName(), true), "Online" };
            model.addRow(row);
        } else {
            Object row[] = { new Printer(printService.getName(), true), "Offline" };
            model.addRow(row);
        }
    }

}

From source file:ru.trett.cis.services.PrinterServiceImpl.java

@Override
public String print(String file) throws ApplicationException, FileNotFoundException, PrintException {
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(MediaSizeName.ISO_A4);
    PrintService printServices = PrintServiceLookup.lookupDefaultPrintService();
    if (printServices == null)
        throw new ApplicationException("Default printer not found");
    LOGGER.info("Default printer is " + printServices.getName());
    DocPrintJob printJob = printServices.createPrintJob();
    FileInputStream fis = new FileInputStream(file);
    Doc doc = new SimpleDoc(fis, flavor, null);
    printJob.print(doc, aset);/* w w  w.  j ava 2 s.c  om*/
    return printServices.getName();
}

From source file:PrintTest.java

public PrintTestFrame() {
    setTitle("PrintTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    canvas = new PrintComponent();
    add(canvas, BorderLayout.CENTER);

    attributes = new HashPrintRequestAttributeSet();

    JPanel buttonPanel = new JPanel();
    JButton printButton = new JButton("Print");
    buttonPanel.add(printButton);/* w w w . j  a  v  a 2  s .c  o  m*/
    printButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                PrinterJob job = PrinterJob.getPrinterJob();
                job.setPrintable(canvas);
                if (job.printDialog(attributes))
                    job.print(attributes);
            } catch (PrinterException e) {
                JOptionPane.showMessageDialog(PrintTestFrame.this, e);
            }
        }
    });

    JButton pageSetupButton = new JButton("Page setup");
    buttonPanel.add(pageSetupButton);
    pageSetupButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            PrinterJob job = PrinterJob.getPrinterJob();
            job.pageDialog(attributes);
        }
    });

    add(buttonPanel, BorderLayout.NORTH);
}

From source file:JuliaSet3.java

public void print() {
    // Get a list of all printers that can handle Printable objects.
    DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
    PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, null);

    // Set some define printing attributes
    PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet();
    printAttributes.add(OrientationRequested.LANDSCAPE); // landscape mode
    printAttributes.add(Chromaticity.MONOCHROME); // print in mono

    // Display a dialog that allows the user to select one of the
    // available printers and to edit the default attributes
    PrintService service = ServiceUI.printDialog(null, 100, 100, services, null, null, printAttributes);

    // If the user canceled, don't do anything
    if (service == null)
        return;/*from   w w w.j  a v a  2  s . c o m*/

    // Now call a method defined below to finish the printing
    printToService(service, printAttributes);
}

From source file:BookTest.java

public BookTestFrame() {
    setTitle("BookTest");

    text = new JTextField();
    add(text, BorderLayout.NORTH);

    attributes = new HashPrintRequestAttributeSet();

    JPanel buttonPanel = new JPanel();

    JButton printButton = new JButton("Print");
    buttonPanel.add(printButton);/*from w  ww. j a  v  a2s.c om*/
    printButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                PrinterJob job = PrinterJob.getPrinterJob();
                job.setPageable(makeBook());
                if (job.printDialog(attributes)) {
                    job.print(attributes);
                }
            } catch (PrinterException e) {
                JOptionPane.showMessageDialog(BookTestFrame.this, e);
            }
        }
    });

    JButton pageSetupButton = new JButton("Page setup");
    buttonPanel.add(pageSetupButton);
    pageSetupButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            PrinterJob job = PrinterJob.getPrinterJob();
            pageFormat = job.pageDialog(attributes);
        }
    });

    JButton printPreviewButton = new JButton("Print preview");
    buttonPanel.add(printPreviewButton);
    printPreviewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            PrintPreviewDialog dialog = new PrintPreviewDialog(makeBook());
            dialog.setVisible(true);
        }
    });

    add(buttonPanel, BorderLayout.SOUTH);
    pack();
}

From source file:ru.codemine.pos.service.kkm.template.JasperChequeTemplate.java

public void print(Cheque cheque, String printerName) throws JRException {
    List<JasperChequeRecord> records = new ArrayList<>();

    for (ChequeLine line : cheque.getContent()) {
        records.add(new JasperChequeRecord(line.getProduct().getName(), line.getQuantity(),
                "=" + line.getLineTotal()));
    }//from   www  .ja  va 2s . c om

    Map<String, Object> parameters = new HashMap<>();
    parameters.put("titleString", cheque.getShop().getOrgName());
    parameters.put("reqs",
            "?? " + cheque.getShop().getOrgInn() + "    " + cheque.getShop().getOrgKpp());
    parameters.put("currentTime", DateTime.now().toString("dd.MM.YY HH:mm"));
    parameters.put("currentUser", cheque.getCreator().getUsername());
    parameters.put("chequeNumber", "#0011");
    parameters.put("chequeTotal", "=" + cheque.getChequeTotal());
    parameters.put("paymentType", Cheque.getAvaiblePaymentTypes().get(cheque.getPaymentType()));
    parameters.put("fromClient", cheque.getChequeTotal().toString());
    parameters.put("toClient", "0.0");

    JasperReport report = (JasperReport) JRLoader.loadObjectFromFile("reports/ChequeTemplate.jasper");
    JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(records);
    JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, dataSource);

    PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();

    PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
    printServiceAttributeSet.add(new PrinterName(printerName, null));

    JRPrintServiceExporter exporter = new JRPrintServiceExporter();
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));

    SimplePrintServiceExporterConfiguration configuration = new SimplePrintServiceExporterConfiguration();
    configuration.setPrintRequestAttributeSet(printRequestAttributeSet);
    configuration.setPrintServiceAttributeSet(printServiceAttributeSet);
    configuration.setDisplayPageDialog(false);
    configuration.setDisplayPrintDialog(false);

    exporter.setConfiguration(configuration);
    exporter.exportReport();

}