Search in sources :

Example 51 with PageFormat

use of java.awt.print.PageFormat in project tray by qzind.

the class PrintPDF method print.

@Override
public void print(PrintOutput output, PrintOptions options) throws PrinterException {
    if (printables.isEmpty()) {
        log.warn("Nothing to print");
        return;
    }
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintService(output.getPrintService());
    PrintOptions.Pixel pxlOpts = options.getPixelOptions();
    PrintRequestAttributeSet attributes = applyDefaultSettings(pxlOpts, job.getPageFormat(null));
    // Disable attributes per https://github.com/qzind/tray/issues/174
    if (SystemUtilities.isMac() && Constants.JAVA_VERSION.lessThan(Version.valueOf("1.8.0-162"))) {
        log.warn("MacOS and Java < 8u162 cannot use attributes with PDF prints, disabling");
        attributes.clear();
    }
    Scaling scale = (pxlOpts.isScaleContent() ? Scaling.SCALE_TO_FIT : Scaling.ACTUAL_SIZE);
    BookBundle bundle = new BookBundle();
    for (PDDocument doc : printables) {
        PageFormat page = job.getPageFormat(null);
        applyDefaultSettings(pxlOpts, page);
        for (PDPage pd : doc.getPages()) {
            if (pxlOpts.getRotation() % 360 != 0) {
                rotatePage(doc, pd, pxlOpts.getRotation());
            }
            if (pxlOpts.getOrientation() == null) {
                PDRectangle bounds = pd.getBBox();
                if (bounds.getWidth() > bounds.getHeight() || (pd.getRotation() / 90) % 2 == 1) {
                    log.info("Adjusting orientation to print landscape PDF source");
                    page.setOrientation(PrintOptions.Orientation.LANDSCAPE.getAsFormat());
                }
            } else if (pxlOpts.getOrientation() != PrintOptions.Orientation.PORTRAIT) {
                // flip imageable area dimensions when in landscape
                Paper repap = page.getPaper();
                repap.setImageableArea(repap.getImageableX(), repap.getImageableY(), repap.getImageableHeight(), repap.getImageableWidth());
                page.setPaper(repap);
                // reverse fix for OSX
                if (SystemUtilities.isMac() && pxlOpts.getOrientation() == PrintOptions.Orientation.REVERSE_LANDSCAPE) {
                    pd.setRotation(pd.getRotation() + 180);
                }
            }
        }
        bundle.append(new PDFWrapper(doc, scale, false, (float) (pxlOpts.getDensity() * pxlOpts.getUnits().as1Inch()), false, pxlOpts.getOrientation()), page, doc.getNumberOfPages());
    }
    job.setJobName(pxlOpts.getJobName(Constants.PDF_PRINT));
    job.setPageable(bundle.wrapAndPresent());
    printCopies(output, pxlOpts, job, attributes);
}
Also used : PageFormat(java.awt.print.PageFormat) PDPage(org.apache.pdfbox.pdmodel.PDPage) PDFWrapper(qz.printer.PDFWrapper) BookBundle(qz.printer.BookBundle) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PrintOptions(qz.printer.PrintOptions) Scaling(org.apache.pdfbox.printing.Scaling) Paper(java.awt.print.Paper) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) PrinterJob(java.awt.print.PrinterJob) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet)

Example 52 with PageFormat

use of java.awt.print.PageFormat in project pdfbox by apache.

the class PDFPageable method getPageFormat.

/**
 * {@inheritDoc}
 *
 * Returns the actual physical size of the pages in the PDF file. May not fit the local printer.
 */
@Override
public PageFormat getPageFormat(int pageIndex) {
    PDPage page = document.getPage(pageIndex);
    PDRectangle mediaBox = PDFPrintable.getRotatedMediaBox(page);
    PDRectangle cropBox = PDFPrintable.getRotatedCropBox(page);
    // Java does not seem to understand landscape paper sizes, i.e. where width > height, it
    // always crops the imageable area as if the page were in portrait. I suspect that this is
    // a JDK bug but it might be by design, see PDFBOX-2922.
    // 
    // As a workaround, we normalise all Page(s) to be portrait, then flag them as landscape in
    // the PageFormat.
    Paper paper;
    boolean isLandscape;
    if (mediaBox.getWidth() > mediaBox.getHeight()) {
        // rotate
        paper = new Paper();
        paper.setSize(mediaBox.getHeight(), mediaBox.getWidth());
        paper.setImageableArea(cropBox.getLowerLeftY(), cropBox.getLowerLeftX(), cropBox.getHeight(), cropBox.getWidth());
        isLandscape = true;
    } else {
        paper = new Paper();
        paper.setSize(mediaBox.getWidth(), mediaBox.getHeight());
        paper.setImageableArea(cropBox.getLowerLeftX(), cropBox.getLowerLeftY(), cropBox.getWidth(), cropBox.getHeight());
        isLandscape = false;
    }
    PageFormat format = new PageFormat();
    format.setPaper(paper);
    // auto portrait/landscape
    switch(orientation) {
        case AUTO:
            format.setOrientation(isLandscape ? PageFormat.LANDSCAPE : PageFormat.PORTRAIT);
            break;
        case LANDSCAPE:
            format.setOrientation(PageFormat.LANDSCAPE);
            break;
        case PORTRAIT:
            format.setOrientation(PageFormat.PORTRAIT);
            break;
        default:
            break;
    }
    return format;
}
Also used : PageFormat(java.awt.print.PageFormat) PDPage(org.apache.pdfbox.pdmodel.PDPage) Paper(java.awt.print.Paper) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle)

Example 53 with PageFormat

use of java.awt.print.PageFormat in project SIMVA-SoS by SESoS.

the class ChartPanel method createChartPrintJob.

/**
 * Creates a print job for the chart.
 */
public void createChartPrintJob() {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    PageFormat pf2 = job.pageDialog(pf);
    if (pf2 != pf) {
        job.setPrintable(this, pf2);
        if (job.printDialog()) {
            try {
                job.print();
            } catch (PrinterException e) {
                JOptionPane.showMessageDialog(this, e);
            }
        }
    }
}
Also used : PageFormat(java.awt.print.PageFormat) PrinterException(java.awt.print.PrinterException) PrinterJob(java.awt.print.PrinterJob)

Example 54 with PageFormat

use of java.awt.print.PageFormat in project jgnash by ccavanaugh.

the class ReportViewerDialogController method handleFitHeightAction.

@FXML
private void handleFitHeightAction() {
    final PageFormat pageFormat = report.get().getPageFormat();
    final double heightRatio = (scrollPane.getViewportBounds().getHeight() - (2 * PAGE_BORDER)) / pageFormat.getHeight();
    final double widthRatio = (scrollPane.getViewportBounds().getWidth() - (2 * PAGE_BORDER)) / pageFormat.getWidth();
    zoomComboBox.getSelectionModel().clearSelection();
    setActualZoomRatio(Math.min(heightRatio, widthRatio));
}
Also used : PageFormat(java.awt.print.PageFormat) FXML(javafx.fxml.FXML) InjectFXML(jgnash.uifx.util.InjectFXML)

Example 55 with PageFormat

use of java.awt.print.PageFormat in project jgnash by ccavanaugh.

the class PageFormatDialogController method generatePageFormat.

private PageFormat generatePageFormat() {
    final PageFormat pageFormat = new PageFormat();
    final Unit unit = unitsComboBox.getValue();
    if (portraitRadioButton.isSelected()) {
        pageFormat.setOrientation(PageFormat.PORTRAIT);
    } else {
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
    }
    double width = widthField.getDecimal().doubleValue() * unit.scale;
    double height = heightField.getDecimal().doubleValue() * unit.scale;
    double rightMargin = rightMarginField.getDecimal().doubleValue() * unit.scale;
    double bottomMargin = bottomMarginField.getDecimal().doubleValue() * unit.scale;
    double imageableX = leftMarginField.getDecimal().doubleValue() * unit.scale;
    double imageableY = topMarginField.getDecimal().doubleValue() * unit.scale;
    double imageableWidth = width - imageableX - rightMargin;
    double imageableHeight = height - imageableY - bottomMargin;
    final Paper paper = pageFormat.getPaper();
    paper.setSize(width, height);
    paper.setImageableArea(imageableX, imageableY, imageableWidth, imageableHeight);
    pageFormat.setPaper(paper);
    return pageFormat;
}
Also used : PageFormat(java.awt.print.PageFormat) Paper(java.awt.print.Paper)

Aggregations

PageFormat (java.awt.print.PageFormat)61 Paper (java.awt.print.Paper)23 PrinterJob (java.awt.print.PrinterJob)22 PrinterException (java.awt.print.PrinterException)19 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)12 PrintService (javax.print.PrintService)11 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)10 Printable (java.awt.print.Printable)9 BufferedImage (java.awt.image.BufferedImage)8 Graphics2D (java.awt.Graphics2D)7 File (java.io.File)6 IOException (java.io.IOException)6 MediaSize (javax.print.attribute.standard.MediaSize)6 HeadlessException (java.awt.HeadlessException)5 StreamPrintService (javax.print.StreamPrintService)5 Media (javax.print.attribute.standard.Media)5 MediaSizeName (javax.print.attribute.standard.MediaSizeName)5 Dimension (java.awt.Dimension)4 Rectangle (java.awt.Rectangle)4 Rectangle2D (java.awt.geom.Rectangle2D)4