Example usage for java.awt.print PageFormat getPaper

List of usage examples for java.awt.print PageFormat getPaper

Introduction

In this page you can find the example usage for java.awt.print PageFormat getPaper.

Prototype

public Paper getPaper() 

Source Link

Document

Returns a copy of the Paper object associated with this PageFormat .

Usage

From source file:org.pentaho.reporting.designer.core.editor.report.AbstractRenderComponent.java

protected double getTopBorder() {
    if (renderContext == null) {
        return 0;
    }//from w w  w. j a v  a 2s  .co m
    if (showTopBorder == false) {
        return 0;
    }
    final PageDefinition pageDefinition = renderContext.getContextRoot().getPageDefinition();
    final PageFormat pageFormat = pageDefinition.getPageFormat(0);
    final PageFormatFactory pageFormatFactory = PageFormatFactory.getInstance();
    return pageFormatFactory.getTopBorder(pageFormat.getPaper());
}

From source file:org.pentaho.reporting.engine.classic.core.modules.parser.bundle.layout.PageDefinitionReadHandler.java

/**
 * Handles the page format./* w w w.j a v a2  s .c  o  m*/
 *
 * @param atts
 *          the attributes.
 * @throws SAXException
 *           if a parser error occurs or the validation failed.
 * @noinspection SuspiciousNameCombination
 */
private PageFormat configurePageSizeAndMargins(final Attributes atts, PageFormat format) throws SAXException {
    // (1) Grab the existing default ...
    float defTopMargin = (float) format.getImageableY();
    float defBottomMargin = (float) (format.getHeight() - format.getImageableHeight() - format.getImageableY());
    float defLeftMargin = (float) format.getImageableX();
    float defRightMargin = (float) (format.getWidth() - format.getImageableWidth() - format.getImageableX());

    // (2) Now configure the new paper-size
    format = configurePageSize(format, atts);

    // (3) Reconfigure margins as requested
    defTopMargin = ParserUtil.parseFloat(atts.getValue(getUri(), "margin-top"), defTopMargin);
    defBottomMargin = ParserUtil.parseFloat(atts.getValue(getUri(), "margin-bottom"), defBottomMargin);
    defLeftMargin = ParserUtil.parseFloat(atts.getValue(getUri(), "margin-left"), defLeftMargin);
    defRightMargin = ParserUtil.parseFloat(atts.getValue(getUri(), "margin-right"), defRightMargin);

    final Paper p = format.getPaper();
    switch (format.getOrientation()) {
    case PageFormat.PORTRAIT:
        PageFormatFactory.getInstance().setBorders(p, defTopMargin, defLeftMargin, defBottomMargin,
                defRightMargin);
        break;
    case PageFormat.REVERSE_LANDSCAPE:
        PageFormatFactory.getInstance().setBorders(p, defLeftMargin, defBottomMargin, defRightMargin,
                defTopMargin);
        break;
    case PageFormat.LANDSCAPE:
        PageFormatFactory.getInstance().setBorders(p, defRightMargin, defTopMargin, defLeftMargin,
                defBottomMargin);
        break;
    default:
        // will not happen..
        throw new IllegalArgumentException("Unexpected paper orientation.");
    }

    format.setPaper(p);
    return format;
}

From source file:org.pentaho.reporting.engine.classic.core.modules.parser.ext.readhandlers.PageReadHandler.java

/**
 * Handles the page format./*from  w  w  w .j a va 2 s.  co m*/
 *
 * @param atts
 *          the attributes.
 * @throws SAXException
 *           if a parser error occurs or the validation failed.
 * @noinspection SuspiciousNameCombination
 */
private void handlePageFormat(final Attributes atts) throws SAXException {
    final MasterReport report = (MasterReport) getRootHandler()
            .getHelperObject(ReportParserUtil.HELPER_OBJ_REPORT_NAME);

    // grab the default page definition ...
    PageFormat format = report.getPageDefinition().getPageFormat(0);
    float defTopMargin = (float) format.getImageableY();
    float defBottomMargin = (float) (format.getHeight() - format.getImageableHeight() - format.getImageableY());
    float defLeftMargin = (float) format.getImageableX();
    float defRightMargin = (float) (format.getWidth() - format.getImageableWidth() - format.getImageableX());

    format = createPageFormat(format, atts);

    defTopMargin = ParserUtil.parseFloat(atts.getValue(getUri(), PageReadHandler.TOPMARGIN_ATT), defTopMargin);
    defBottomMargin = ParserUtil.parseFloat(atts.getValue(getUri(), PageReadHandler.BOTTOMMARGIN_ATT),
            defBottomMargin);
    defLeftMargin = ParserUtil.parseFloat(atts.getValue(getUri(), PageReadHandler.LEFTMARGIN_ATT),
            defLeftMargin);
    defRightMargin = ParserUtil.parseFloat(atts.getValue(getUri(), PageReadHandler.RIGHTMARGIN_ATT),
            defRightMargin);

    final Paper p = format.getPaper();
    switch (format.getOrientation()) {
    case PageFormat.PORTRAIT:
        PageFormatFactory.getInstance().setBorders(p, defTopMargin, defLeftMargin, defBottomMargin,
                defRightMargin);
        break;
    case PageFormat.LANDSCAPE:
        // right, top, left, bottom
        PageFormatFactory.getInstance().setBorders(p, defRightMargin, defTopMargin, defLeftMargin,
                defBottomMargin);
        break;
    case PageFormat.REVERSE_LANDSCAPE:
        PageFormatFactory.getInstance().setBorders(p, defLeftMargin, defBottomMargin, defRightMargin,
                defTopMargin);
        break;
    default:
        // will not happen..
        throw new IllegalArgumentException("Unexpected paper orientation.");
    }

    format.setPaper(p);
    pageFormat = format;
}

From source file:org.pentaho.reporting.engine.classic.core.modules.parser.simple.readhandlers.JFreeReportReadHandler.java

/**
 * Starts parsing./*ww w .  j  a  va2  s  . co  m*/
 *
 * @param attrs
 *          the attributes.
 * @throws org.xml.sax.SAXException
 *           if there is a parsing error.
 * @noinspection SuspiciousNameCombination
 */
protected void startParsing(final PropertyAttributes attrs) throws SAXException {
    RootXmlReadHandler rootHandler = getRootHandler();
    final Object maybeReport = rootHandler.getHelperObject(ReportParserUtil.HELPER_OBJ_REPORT_NAME);
    final MasterReport report;
    if (maybeReport instanceof MasterReport == false) {
        // replace it ..
        report = new MasterReport();
        report.setAttribute(AttributeNames.Core.NAMESPACE, AttributeNames.Core.SOURCE, rootHandler.getSource());
    } else {
        report = (MasterReport) maybeReport;
    }

    final int groupCount = report.getGroupCount();
    for (int i = 0; i < groupCount; i++) {
        final Group g = report.getGroup(i);
        if (g instanceof RelationalGroup) {
            groupList.add((RelationalGroup) g);
        } else {
            throw new ParseException("The existing report contains non-default groups. "
                    + "This parser cannot handle such a construct.");
        }
    }

    final RootXmlReadHandler parser = rootHandler;
    if (ReportParserUtil.isIncluded(parser) == false) {
        final String query = attrs.getValue(getUri(), "query");
        if (query != null) {
            report.setQuery(query);
        }

        final String name = attrs.getValue(getUri(), JFreeReportReadHandler.NAME_ATT);
        if (name != null) {
            report.setName(name);
        }

        PageFormat format = report.getPageDefinition().getPageFormat(0);
        float defTopMargin = (float) format.getImageableY();
        float defBottomMargin = (float) (format.getHeight() - format.getImageableHeight()
                - format.getImageableY());
        float defLeftMargin = (float) format.getImageableX();
        float defRightMargin = (float) (format.getWidth() - format.getImageableWidth()
                - format.getImageableX());

        format = createPageFormat(format, attrs);

        defTopMargin = ParserUtil.parseFloat(attrs.getValue(getUri(), JFreeReportReadHandler.TOPMARGIN_ATT),
                defTopMargin);
        defBottomMargin = ParserUtil
                .parseFloat(attrs.getValue(getUri(), JFreeReportReadHandler.BOTTOMMARGIN_ATT), defBottomMargin);
        defLeftMargin = ParserUtil.parseFloat(attrs.getValue(getUri(), JFreeReportReadHandler.LEFTMARGIN_ATT),
                defLeftMargin);
        defRightMargin = ParserUtil.parseFloat(attrs.getValue(getUri(), JFreeReportReadHandler.RIGHTMARGIN_ATT),
                defRightMargin);

        final Paper p = format.getPaper();
        switch (format.getOrientation()) {
        case PageFormat.PORTRAIT:
            PageFormatFactory.getInstance().setBorders(p, defTopMargin, defLeftMargin, defBottomMargin,
                    defRightMargin);
            break;
        case PageFormat.LANDSCAPE:
            // right, top, left, bottom
            PageFormatFactory.getInstance().setBorders(p, defRightMargin, defTopMargin, defLeftMargin,
                    defBottomMargin);
            break;
        case PageFormat.REVERSE_LANDSCAPE:
            PageFormatFactory.getInstance().setBorders(p, defLeftMargin, defBottomMargin, defRightMargin,
                    defTopMargin);
            break;
        default:
            throw new IllegalStateException("Unexpected paper orientation.");
        }

        final int pageSpan = ParserUtil.parseInt(attrs.getValue(getUri(), JFreeReportReadHandler.PAGESPAN_ATT),
                1);

        format.setPaper(p);
        report.setPageDefinition(new SimplePageDefinition(format, pageSpan, 1));
    }
    if (rootHandler.getHelperObject(ReportParserUtil.HELPER_OBJ_LEGACY_STYLES) instanceof HashMap == false) {
        rootHandler.setHelperObject(ReportParserUtil.HELPER_OBJ_LEGACY_STYLES,
                new HashMap<String, ElementStyleSheet>());
    }
    rootHandler.setHelperObject(ReportParserUtil.HELPER_OBJ_REPORT_NAME, report);

    final String useMinChunkWidth = attrs.getValue(getUri(), "use-min-chunkwidth");
    if (useMinChunkWidth != null) {
        report.getStyle().setStyleProperty(ElementStyleKeys.USE_MIN_CHUNKWIDTH,
                ReportParserUtil.parseBoolean(useMinChunkWidth, getLocator()));
    }

    report.setCompatibilityLevel(ClassicEngineBoot.computeVersionId(3, 8, 0));
    this.report = report;
}

From source file:org.pentaho.reporting.engine.classic.core.util.PageFormatFactory.java

public static String printPageFormat(final PageFormat pf) {
    StringBuffer b = new StringBuffer();
    b.append("PageFormat={width=");
    b.append(pf.getWidth());/* w  ww . ja  v  a  2  s  .  c o m*/
    b.append(", height=");
    b.append(pf.getHeight());
    b.append(", imageableX=");
    b.append(pf.getImageableX());
    b.append(", imageableY=");
    b.append(pf.getImageableY());
    b.append(", imageableWidth=");
    b.append(pf.getImageableWidth());
    b.append(", imageableHeight=");
    b.append(pf.getImageableHeight());
    b.append(", orientation=").append(pf.getOrientation());
    b.append(", paper=");
    b.append(printPaper(pf.getPaper()));
    b.append("}");
    return b.toString();
}

From source file:org.pentaho.reporting.engine.classic.core.util.PageFormatFactory.java

public void setPageMargins(final PageFormat pageFormat, final Insets pageMargins) {
    final Paper paper = pageFormat.getPaper();
    setBorders(paper, pageMargins.top, pageMargins.left, pageMargins.bottom, pageMargins.right);
    pageFormat.setPaper(paper);//  w  ww .  j  a v a  2  s. c o m
}

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   w w  w.  ja v a 2s  . 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.sanjose.util.JRPrinterAWT.java

/**
 *
 *//*from   w  ww.ja va  2s .  co m*/
private boolean printPages(int firstPageIndex, int lastPageIndex, boolean withPrintDialog,
        PrintService pService) throws JRException {
    boolean isOK = true;

    if (firstPageIndex < 0 || firstPageIndex > lastPageIndex
            || lastPageIndex >= jasperPrint.getPages().size()) {
        throw new JRException("Invalid page index range : " + firstPageIndex + " - " + lastPageIndex + " of "
                + jasperPrint.getPages().size());
    }

    pageOffset = firstPageIndex;

    PrinterJob printJob = PrinterJob.getPrinterJob();

    // fix for bug ID 6255588 from Sun bug database
    initPrinterJobFields(printJob);

    try {
        printJob.setPrintService(pService);
    } catch (PrinterException e) {
        e.printStackTrace();
        throw new JRException(e.getMessage());
    }

    PageFormat pageFormat = printJob.defaultPage();
    Paper paper = pageFormat.getPaper();

    printJob.setJobName("JasperReports - " + jasperPrint.getName());

    switch (jasperPrint.getOrientationValue()) {
    case LANDSCAPE: {
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
        paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        paper.setImageableArea(0, 0, jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        break;
    }
    case PORTRAIT:
    default: {
        pageFormat.setOrientation(PageFormat.PORTRAIT);
        paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
        paper.setImageableArea(0, 0, jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
    }
    }

    pageFormat.setPaper(paper);

    Book book = new Book();
    book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1);
    printJob.setPageable(book);
    try {
        if (withPrintDialog) {
            if (printJob.printDialog()) {
                printJob.print();
            } else {
                isOK = false;
            }
        } else {
            printJob.print();
        }
    } catch (Exception ex) {
        throw new JRException("Error printing report.", ex);
    }

    return isOK;
}