use of javax.print.attribute.PrintRequestAttributeSet in project tray by qzind.
the class PrintPixel method applyDefaultSettings.
protected PrintRequestAttributeSet applyDefaultSettings(PrintOptions.Pixel pxlOpts, PageFormat page, Media[] supported) {
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
// apply general attributes
if (pxlOpts.getColorType() != null) {
attributes.add(pxlOpts.getColorType().getAsChromaticity());
}
attributes.add(pxlOpts.getDuplex());
if (pxlOpts.getOrientation() != null) {
attributes.add(pxlOpts.getOrientation().getAsOrientRequested());
}
if (pxlOpts.getPrinterTray() != null && !pxlOpts.getPrinterTray().isEmpty()) {
Media tray = findMediaTray(supported, pxlOpts.getPrinterTray());
if (tray != null) {
attributes.add(tray);
}
}
// TODO - set paper thickness
// Java prints using inches at 72dpi
final float CONVERT = pxlOpts.getUnits().toInches() * 72f;
log.trace("DPI: [{}x{}]\tCNV: {}", pxlOpts.getDensity(), pxlOpts.getCrossDensity(), CONVERT);
if (pxlOpts.getDensity() > 0) {
double cross = pxlOpts.getCrossDensity();
if (cross == 0) {
cross = pxlOpts.getDensity();
}
attributes.add(new PrinterResolution((int) cross, (int) pxlOpts.getDensity(), pxlOpts.getUnits().getDPIUnits()));
}
// apply sizing and margins
Paper paper = page.getPaper();
float pageX = 0f;
float pageY = 0f;
float pageW = (float) page.getWidth() / CONVERT;
float pageH = (float) page.getHeight() / CONVERT;
// page size
if (pxlOpts.getSize() != null && pxlOpts.getSize().getWidth() > 0 && pxlOpts.getSize().getHeight() > 0) {
pageW = (float) pxlOpts.getSize().getWidth();
pageH = (float) pxlOpts.getSize().getHeight();
paper.setSize(pageW * CONVERT, pageH * CONVERT);
}
// margins
if (pxlOpts.getMargins() != null) {
pageX += pxlOpts.getMargins().left();
pageY += pxlOpts.getMargins().top();
pageW -= (pxlOpts.getMargins().right() + pxlOpts.getMargins().left());
pageH -= (pxlOpts.getMargins().bottom() + pxlOpts.getMargins().top());
}
log.trace("Drawable area: {},{}:{},{}", pageX, pageY, pageW, pageH);
if (pageW > 0 && pageH > 0) {
attributes.add(new MediaPrintableArea(pageX, pageY, pageW, pageH, pxlOpts.getUnits().getMediaSizeUnits()));
paper.setImageableArea(pageX * CONVERT, pageY * CONVERT, pageW * CONVERT, pageH * CONVERT);
page.setPaper(paper);
} else {
log.warn("Could not apply custom size, using printer default");
attributes.add(new MediaPrintableArea(0, 0, (float) page.getWidth() / 72f, (float) page.getHeight() / 72f, PrintOptions.Unit.INCH.getMediaSizeUnits()));
}
log.trace("{}", Arrays.toString(attributes.toArray()));
return attributes;
}
use of javax.print.attribute.PrintRequestAttributeSet in project tray by qzind.
the class PrintRaw method printToPrinter.
/**
* Constructs a {@code SimpleDoc} with the {@code commands} byte array.
*/
private void printToPrinter(PrintService service, byte[] cmds, PrintOptions.Raw rawOpts) throws PrintException {
if (service == null) {
throw new NullPrintServiceException("Service cannot be null");
}
if (cmds == null || cmds.length == 0) {
throw new NullCommandException("No commands found to send to the printer");
}
SimpleDoc doc = new SimpleDoc(cmds, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(new JobName(rawOpts.getJobName(Constants.RAW_PRINT), Locale.getDefault()));
DocPrintJob printJob = service.createPrintJob();
waitForPrint(printJob, doc, attributes);
}
use of javax.print.attribute.PrintRequestAttributeSet in project pdfbox by apache.
the class PDFDebugger method printMenuItemActionPerformed.
private void printMenuItemActionPerformed(ActionEvent evt) {
if (document == null) {
return;
}
AccessPermission ap = document.getCurrentAccessPermission();
if (!ap.canPrint()) {
JOptionPane.showMessageDialog(this, "You do not have permission to print");
return;
}
try {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(new PDFPageable(document, Orientation.AUTO, false, PrintDpiMenu.getDpiSelection()));
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
PDViewerPreferences vp = document.getDocumentCatalog().getViewerPreferences();
if (vp != null && vp.getDuplex() != null) {
String dp = vp.getDuplex();
if (PDViewerPreferences.DUPLEX.DuplexFlipLongEdge.toString().equals(dp)) {
pras.add(Sides.TWO_SIDED_LONG_EDGE);
} else if (PDViewerPreferences.DUPLEX.DuplexFlipShortEdge.toString().equals(dp)) {
pras.add(Sides.TWO_SIDED_SHORT_EDGE);
} else if (PDViewerPreferences.DUPLEX.Simplex.toString().equals(dp)) {
pras.add(Sides.ONE_SIDED);
}
}
if (job.printDialog(pras)) {
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try {
long t0 = System.nanoTime();
job.print(pras);
long t1 = System.nanoTime();
long ms = TimeUnit.MILLISECONDS.convert(t1 - t0, TimeUnit.NANOSECONDS);
LOG.info("Printed in " + ms + " ms");
} finally {
setCursor(Cursor.getDefaultCursor());
}
}
} catch (PrinterException e) {
new ErrorDialog(e).setVisible(true);
}
}
Aggregations