use of java.awt.print.PrinterJob in project jgnash by ccavanaugh.
the class JasperViewerDialogController method handleFormatAction.
@FXML
private void handleFormatAction() {
final PageFormat oldFormat = report.get().getPageFormat();
final PrinterJob job = PrinterJob.getPrinterJob();
final PageFormat format = job.pageDialog(oldFormat);
if (format != oldFormat) {
report.get().setPageFormat(format);
createJasperPrint(report.get());
Platform.runLater(this::refresh);
}
}
use of java.awt.print.PrinterJob in project tray by qzind.
the class PrintHTML method printLegacy.
private void printLegacy(PrintOutput output, PrintOptions options) throws PrinterException {
PrintOptions.Pixel pxlOpts = options.getPixelOptions();
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(output.getPrintService());
PageFormat page = job.getPageFormat(null);
PrintRequestAttributeSet attributes = applyDefaultSettings(pxlOpts, page);
// setup swing ui
JFrame legacyFrame = new JFrame(pxlOpts.getJobName(Constants.HTML_PRINT));
legacyFrame.setUndecorated(true);
legacyFrame.setLayout(new FlowLayout());
legacyFrame.setExtendedState(Frame.ICONIFIED);
legacyLabel = new JLabel();
legacyLabel.setOpaque(true);
legacyLabel.setBackground(Color.WHITE);
legacyLabel.setBorder(null);
legacyLabel.setDoubleBuffered(false);
legacyFrame.add(legacyLabel);
try {
for (WebAppModel model : models) {
if (model.isPlainText()) {
legacyLabel.setText(model.getSource());
} else {
try (InputStream fis = new URL(model.getSource()).openStream()) {
String webPage = IOUtils.toString(fis, "UTF-8").replaceAll("^[\\s\\S]+<(HTML|html)\\b.*?>", "<html>");
legacyLabel.setText(webPage);
}
}
legacyFrame.pack();
legacyFrame.setVisible(true);
job.setPrintable(this);
printCopies(output, pxlOpts, job, attributes);
}
} catch (Exception e) {
throw new PrinterException(e.getMessage());
} finally {
legacyFrame.dispose();
}
}
use of java.awt.print.PrinterJob in project tray by qzind.
the class PrintImage method print.
@Override
public void print(PrintOutput output, PrintOptions options) throws PrinterException {
if (images.isEmpty()) {
log.warn("Nothing to print");
return;
}
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(output.getPrintService());
PageFormat page = job.getPageFormat(null);
PrintOptions.Pixel pxlOpts = options.getPixelOptions();
PrintRequestAttributeSet attributes = applyDefaultSettings(pxlOpts, page);
scaleImage = pxlOpts.isScaleContent();
interpolation = pxlOpts.getInterpolation();
imageRotation = pxlOpts.getRotation();
// reverse fix for OSX
if (SystemUtilities.isMac() && pxlOpts.getOrientation() != null && pxlOpts.getOrientation().getAsAttribute() == OrientationRequested.REVERSE_LANDSCAPE) {
imageRotation += 180;
manualReverse = true;
}
job.setJobName(pxlOpts.getJobName(Constants.IMAGE_PRINT));
job.setPrintable(this, job.validatePage(page));
printCopies(output, pxlOpts, job, attributes);
}
use of java.awt.print.PrinterJob in project pdfbox by apache.
the class PrintPDF method main.
/**
* Infamous main method.
*
* @param args Command line arguments, should be one and a reference to a file.
* @throws PrinterException if the specified service cannot support the Pageable and Printable interfaces.
* @throws IOException if there is an error parsing the file.
*/
public static void main(String[] args) throws PrinterException, IOException {
try {
// force KCMS (faster than LCMS) if available
Class.forName("sun.java2d.cmm.kcms.KcmsServiceProvider");
System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");
} catch (ClassNotFoundException e) {
LOG.debug("KCMS service not found - using LCMS", e);
}
// suppress the Dock icon on OS X
System.setProperty("apple.awt.UIElement", "true");
String password = "";
String pdfFile = null;
boolean silentPrint = false;
String printerName = null;
Orientation orientation = Orientation.AUTO;
boolean showPageBorder = false;
int dpi = 0;
Map<String, Orientation> orientationMap = new HashMap<>();
orientationMap.put("auto", Orientation.AUTO);
orientationMap.put("landscape", Orientation.LANDSCAPE);
orientationMap.put("portrait", Orientation.PORTRAIT);
for (int i = 0; i < args.length; i++) {
switch(args[i]) {
case PASSWORD:
i++;
if (i >= args.length) {
usage();
}
password = args[i];
break;
case PRINTER_NAME:
i++;
if (i >= args.length) {
usage();
}
printerName = args[i];
break;
case SILENT:
silentPrint = true;
break;
case ORIENTATION:
i++;
if (i >= args.length) {
usage();
}
orientation = orientationMap.get(args[i]);
if (orientation == null) {
usage();
}
break;
case BORDER:
showPageBorder = true;
break;
case DPI:
i++;
if (i >= args.length) {
usage();
}
dpi = Integer.parseInt(args[i]);
break;
default:
pdfFile = args[i];
break;
}
}
if (pdfFile == null) {
usage();
}
try (PDDocument document = PDDocument.load(new File(pdfFile), password)) {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setJobName(new File(pdfFile).getName());
if (printerName != null) {
PrintService[] printService = PrinterJob.lookupPrintServices();
boolean printerFound = false;
for (int i = 0; !printerFound && i < printService.length; i++) {
if (printService[i].getName().contains(printerName)) {
printJob.setPrintService(printService[i]);
printerFound = true;
}
}
}
printJob.setPageable(new PDFPageable(document, orientation, showPageBorder, dpi));
if (silentPrint || printJob.printDialog()) {
printJob.print();
}
}
}
use of java.awt.print.PrinterJob in project pdfbox by apache.
the class Printing method printWithDialogAndAttributes.
/**
* Prints with a print preview dialog and custom PrintRequestAttribute values.
*/
private static void printWithDialogAndAttributes(PDDocument document) throws IOException, PrinterException {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(new PDFPageable(document));
PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
// pages 1 to 1
attr.add(new PageRanges(1, 1));
PDViewerPreferences vp = document.getDocumentCatalog().getViewerPreferences();
if (vp != null && vp.getDuplex() != null) {
String dp = vp.getDuplex();
if (PDViewerPreferences.DUPLEX.DuplexFlipLongEdge.toString().equals(dp)) {
attr.add(Sides.TWO_SIDED_LONG_EDGE);
} else if (PDViewerPreferences.DUPLEX.DuplexFlipShortEdge.toString().equals(dp)) {
attr.add(Sides.TWO_SIDED_SHORT_EDGE);
} else if (PDViewerPreferences.DUPLEX.Simplex.toString().equals(dp)) {
attr.add(Sides.ONE_SIDED);
}
}
if (job.printDialog(attr)) {
job.print(attr);
}
}
Aggregations