Example usage for javax.print.attribute PrintRequestAttributeSet add

List of usage examples for javax.print.attribute PrintRequestAttributeSet add

Introduction

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

Prototype

public boolean add(Attribute attribute);

Source Link

Document

Adds the specified attribute value to this attribute set if it is not already present, first removing any existing value in the same attribute category as the specified attribute value (optional operation).

Usage

From source file:org.pentaho.reporting.engine.classic.extensions.modules.java14print.Java14PrintUtil.java

/**
 * This method replaces the media definition from the given attribute set with the one found in the report itself.
 * <p/>//from ww  w. jav  a2s .  co m
 * If no JobName is set, a default jobname will be assigned.
 *
 * @param attributes
 * @param report
 * @return
 */
public static PrintRequestAttributeSet copyConfiguration(PrintRequestAttributeSet attributes,
        final MasterReport report) {
    if (attributes == null) {
        attributes = new HashPrintRequestAttributeSet();
    }

    // for now, be lazy, assume that the first page is the reference
    final PageDefinition pdef = report.getPageDefinition();
    final PageFormat format = pdef.getPageFormat(0);
    final Paper paper = format.getPaper();

    final Media media = MediaSize.findMedia((float) (paper.getWidth() / POINTS_PER_INCH),
            (float) (paper.getHeight() / POINTS_PER_INCH), Size2DSyntax.INCH);
    attributes.add(media);

    final MediaPrintableArea printableArea = new MediaPrintableArea(
            (float) (paper.getImageableX() / POINTS_PER_INCH),
            (float) (paper.getImageableY() / POINTS_PER_INCH),
            (float) (paper.getImageableWidth() / POINTS_PER_INCH),
            (float) (paper.getImageableHeight() / POINTS_PER_INCH), Size2DSyntax.INCH);

    attributes.add(printableArea);
    attributes.add(mapOrientation(format.getOrientation()));

    return attributes;
}

From source file:org.pentaho.reporting.engine.classic.extensions.modules.java14print.Java14PrintUtil.java

public static PrintRequestAttributeSet copyAuxillaryAttributes(PrintRequestAttributeSet attributes,
        final MasterReport report) {
    if (attributes == null) {
        attributes = new HashPrintRequestAttributeSet();
    }/* w  ww . j  a v  a  2 s.  com*/

    if (attributes.containsKey(JobName.class) == false) {
        final String jobName = report.getReportConfiguration().getConfigProperty(PrintUtil.PRINTER_JOB_NAME_KEY,
                report.getTitle());
        if (jobName != null) {
            attributes.add(new JobName(jobName, null));
        }
    }
    if (attributes.containsKey(Copies.class) == false) {
        final int numberOfCopies = PrintUtil.getNumberOfCopies(report.getReportConfiguration());
        attributes.add(new Copies(numberOfCopies));
    }

    return attributes;
}

From source file:us.mn.state.health.lims.common.provider.reports.SampleLabelPrintProvider.java

private void runPrintJobs() throws PrintException {
    returnedData += ";;";

    if (masterCount > 0) {
        DocPrintJob masterJob = ps.createPrintJob();

        PrintRequestAttributeSet masterAset = new HashPrintRequestAttributeSet();
        masterAset.add(new Copies(masterCount));

        String masterLabel = "";
        String masterZPL = StringUtil.getMessageForKey("master.label.zpl.prefix"); //"^XA";
        for (Property prop : masterPrintOrder) {
            if (ConfigurationProperties.getInstance().isPropertyValueEqual(prop, "true")) {
                masterLabel += (!GenericValidator.isBlankOrNull(labelData.get(prop.name().toString()))
                        ? labelData.get(prop.name().toString())
                        : "") + "\n";
                if (prop.equals(Property.MASTER_LABELS_ACCESSION)) {
                    masterZPL += StringUtil.getMessageForKey("master.label.zpl.accession.barcode.prefix") //"^FO20,30^BCN,60,N,N,N^FD"
                            + labelData.get(prop.name().toString())
                            + StringUtil.getMessageForKey("master.label.zpl.accession.text.prefix") //"^FS^FO30,90^AEN,12,18^FD"
                            + labelData.get(prop.name().toString())
                            + StringUtil.getMessageForKey("master.label.zpl.accession.suffix"); //"^FS";
                } else {
                    masterZPL += StringUtil.getMessageForKey("master.label.zpl.other.prefix") //"^FO25,195^AEN,12,18^FD"
                            + labelData.get(prop.name().toString())
                            + StringUtil.getMessageForKey("master.label.zpl.other.suffix"); //"^FS";
                }/*  w  w  w. jav a2 s. c o  m*/
            }
        }
        masterZPL += StringUtil.getMessageForKey("master.label.zpl.suffix"); //"^XZ";

        // Printing raw ZPL and returning the ZPL string that can be displayed via JavaScript as a simulated label in the browser
        //         System.out.println("Master Label (" + masterCount + " copies):\n" + masterLabel);
        //         System.out.println("Master Label ZPL (" + masterCount + " copies):\n" + masterZPL + "\n");
        returnedData += masterZPL;

        byte[] byt = masterZPL.getBytes();
        DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
        Doc doc = new SimpleDoc(byt, flavor, null);
        masterJob.addPrintJobListener(new MyPrintJobListener());
        masterJob.print(doc, masterAset);
    }

    returnedData += ";;";

    for (int j = 1; j <= itemCount; j++) {
        DocPrintJob itemJob = ps.createPrintJob();
        PrintRequestAttributeSet itemAset = new HashPrintRequestAttributeSet();
        itemAset.add(new Copies(1));

        String itemLabel = "";
        String itemZPL = StringUtil.getMessageForKey("item.label.zpl.prefix"); //"^XA";
        for (Property prop : itemPrintOrder) {
            if (ConfigurationProperties.getInstance().isPropertyValueEqual(prop, "true")) {
                itemLabel += !GenericValidator.isBlankOrNull(labelData.get(prop.name().toString()))
                        ? labelData.get(prop.name().toString())
                        : "";
                if (prop.equals(Property.SPECIMEN_LABELS_ACCESSION)) {
                    itemLabel += "-" + j;
                    itemZPL += StringUtil.getMessageForKey("item.label.zpl.accession.barcode.prefix") //"^FO20,30^BCN,60,N,N,N^FD"
                            + labelData.get(prop.name().toString()) + "-" + j
                            + StringUtil.getMessageForKey("item.label.zpl.accession.text.prefix") //"^FS^FO30,90^AEN,12,18^FD"
                            + labelData.get(prop.name().toString()) + "-" + j
                            + StringUtil.getMessageForKey("item.label.zpl.accession.suffix"); //"^FS";
                } else {
                    itemZPL += StringUtil.getMessageForKey("item.label.zpl.other.prefix") //"^FO25,195^AEN,12,18^FD"
                            + labelData.get(prop.name().toString())
                            + StringUtil.getMessageForKey("item.label.zpl.other.suffix"); //"^FS";
                }
                itemLabel += "\n";
            }
        }
        itemZPL += StringUtil.getMessageForKey("item.label.zpl.suffix"); //"^XZ";

        // Printing raw ZPL and returning a ZPL string that can be displayed via JavaScript as a simulated label in the browser
        //         System.out.println("Item Label:\n" + itemLabel);            
        //         System.out.println("Item Label ZPL:\n" + itemZPL + "\n");
        returnedData += "::" + itemZPL;

        byte[] byt = itemZPL.getBytes();
        DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
        Doc doc = new SimpleDoc(byt, flavor, null);
        itemJob.addPrintJobListener(new MyPrintJobListener());
        itemJob.print(doc, itemAset);
    }

    //String label = SystemConfiguration.getInstance()
    //      .getDefaultSampleLabel(accessionNumber);

    //byte[] byt = label.getBytes();
    //DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
    //Doc doc = new SimpleDoc(byt, flavor, null);
    //job.addPrintJobListener(new MyPrintJobListener());
    //job.print(doc, aset);
}

From source file:us.mn.state.health.lims.common.provider.reports.SampleLabelPrintProvider.java

public void printBarcodeLabel(String[] accList) {
    try {/*from  w ww.  j  av a2  s.c  o m*/

        // Support with 3 labels per row for now
        int itemsperrow = 3;

        String numberOfLabelCopiesString = null;
        int numberOfLabelCopies = 1;
        try {
            numberOfLabelCopiesString = SystemConfiguration.getInstance()
                    .getLabelNumberOfCopies("print.label.numberofcopies");
            numberOfLabelCopies = Integer.parseInt(numberOfLabelCopiesString);
        } catch (NumberFormatException nfe) {
            // System.out.println("numberOfLabelCopies not defined as
            // numeric");
        }

        String accessionNumber = "fail";// (String)

        int labelPerPatient = 1;
        try {
            labelPerPatient = masterCount;
        } catch (NumberFormatException nfe) {
            labelPerPatient = 1;
        }

        if (ps == null) {
            // System.out.println("Zebra printer is not found.");
            return;
        }

        ArrayList listAccessNumber = new ArrayList<>();

        for (int i = 0; i < accList.length; i++) {
            listAccessNumber.add(accList[i]);
        }

        ArrayList<String> newListAccessNumber = new ArrayList<String>();
        Iterator iter = listAccessNumber.iterator();
        while (iter.hasNext()) {
            String element = (String) iter.next();
            for (int i = 0; i < labelPerPatient; i++) {
                newListAccessNumber.add(element);
            }
        }
        int sizeListAcc = newListAccessNumber.size();

        if (sizeListAcc % itemsperrow != 0) {
            for (int i = 0; i < (itemsperrow - (sizeListAcc % itemsperrow)); i++) {
                newListAccessNumber.add("");
            }
        }
        String accessionNumber1 = "";
        String accessionNumber2 = "";
        String accessionNumber3 = "";

        for (int i = 0; i < newListAccessNumber.size(); i = i + itemsperrow) {
            accessionNumber1 = newListAccessNumber.get(i).toString();
            accessionNumber2 = newListAccessNumber.get(i + 1).toString();
            accessionNumber3 = newListAccessNumber.get(i + 2).toString();
            DocPrintJob job = ps.createPrintJob();
            PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
            //aset.add(new Copies(numberOfLabelCopies * labelPerPatient));
            aset.add(new Copies(numberOfLabelCopies * 1));
            String label = "";
            if (itemsperrow == 3) {
                label = SystemConfiguration.getInstance().getAllDefaultSampleLabel(accessionNumber1,
                        accessionNumber2, accessionNumber3);
                returnedData += label;
            } else {
                return;
            }
            System.out.println(returnedData);

            byte[] byt = label.getBytes();
            DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
            Doc doc = new SimpleDoc(byt, flavor, null);
            job.addPrintJobListener(new MyPrintJobListener());
            job.print(doc, aset);
            //System.out.println("success...");
        }

    } catch (Exception e) {
        System.out.println("Error in SampleLabelPrintProvider");
        e.printStackTrace();
    } finally {

    }
}