Search in sources :

Example 11 with Paper

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

the class BaseDynamicJasperReport method assignPageFormat.

private void assignPageFormat(final DynamicReportBuilder builder) {
    final PageFormat format = getPageFormat();
    final Paper paper = format.getPaper();
    final int orientation = format.getOrientation();
    int topMargin;
    int rightMargin;
    int leftMargin;
    int bottomMargin;
    Page page;
    if (orientation == PageFormat.PORTRAIT) {
        page = new Page((int) paper.getHeight(), (int) paper.getWidth(), true);
        leftMargin = (int) paper.getImageableX();
        rightMargin = (int) paper.getWidth() - (int) paper.getImageableWidth() - leftMargin;
        topMargin = (int) paper.getImageableY();
        bottomMargin = (int) paper.getHeight() - (int) paper.getImageableHeight() - topMargin;
    } else {
        page = new Page((int) paper.getWidth(), (int) paper.getHeight(), false);
        rightMargin = (int) paper.getImageableY();
        leftMargin = (int) paper.getHeight() - (int) paper.getImageableHeight() - rightMargin;
        topMargin = (int) paper.getImageableX();
        bottomMargin = (int) paper.getWidth() - (int) paper.getImageableWidth() - topMargin;
    }
    builder.setPageSizeAndOrientation(page);
    builder.setMargins(topMargin, bottomMargin, leftMargin, rightMargin);
}
Also used : PageFormat(java.awt.print.PageFormat) Paper(java.awt.print.Paper) Page(ar.com.fdvs.dj.domain.constants.Page) JasperPrint(net.sf.jasperreports.engine.JasperPrint)

Example 12 with Paper

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

the class PrintableCheckLayout method getPageFormat.

public PageFormat getPageFormat() {
    PrintRequestAttributeSet printAttributes = checkLayout.getPrintAttributes();
    if (pageFormat == null) {
        // create a default pageFormat
        PageFormat format = getPrinterJob().defaultPage();
        Paper paper = format.getPaper();
        MediaSizeName media = (MediaSizeName) printAttributes.get(Media.class);
        MediaSize ms = MediaSize.getMediaSizeForName(media);
        MediaPrintableArea ma = (MediaPrintableArea) printAttributes.get(MediaPrintableArea.class);
        if (ma != null) {
            int INCH = MediaPrintableArea.INCH;
            paper.setImageableArea((ma.getX(INCH) * 72), (ma.getY(INCH) * 72), (ma.getWidth(INCH) * 72), (ma.getHeight(INCH) * 72));
        }
        if (ms != null) {
            paper.setSize((ms.getX(Size2DSyntax.INCH) * 72), (ms.getY(Size2DSyntax.INCH) * 72));
        }
        format.setPaper(paper);
        OrientationRequested or = (OrientationRequested) printAttributes.get(OrientationRequested.class);
        if (or != null) {
            if (or == OrientationRequested.LANDSCAPE) {
                format.setOrientation(PageFormat.LANDSCAPE);
            } else if (or == OrientationRequested.REVERSE_LANDSCAPE) {
                format.setOrientation(PageFormat.REVERSE_LANDSCAPE);
            } else if (or == OrientationRequested.PORTRAIT) {
                format.setOrientation(PageFormat.PORTRAIT);
            } else if (or == OrientationRequested.REVERSE_PORTRAIT) {
                format.setOrientation(PageFormat.PORTRAIT);
            }
        }
        pageFormat = format;
    }
    return pageFormat;
}
Also used : PageFormat(java.awt.print.PageFormat) MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Media(javax.print.attribute.standard.Media) Paper(java.awt.print.Paper) OrientationRequested(javax.print.attribute.standard.OrientationRequested) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet)

Example 13 with Paper

use of java.awt.print.Paper in project groovy-core by groovy.

the class TextEditor method print.

public int print(Graphics graphics, PageFormat pageFormat, int page) throws PrinterException {
    if (page < numPages) {
        Paper paper = pageFormat.getPaper();
        // initialize the PRINT_PANE (need this so that wrapping
        // can take place)
        PRINT_PANE.setDocument(getDocument());
        PRINT_PANE.setFont(getFont());
        PRINT_SIZE.setSize(paper.getImageableWidth(), getSize().getHeight());
        PRINT_PANE.setSize(PRINT_SIZE);
        // translate the graphics origin upwards so the area of the page we
        // want to print is in the origin; the clipping region auto set
        // will take care of the rest
        double y = -(page * paper.getImageableHeight()) + paper.getImageableY();
        ((Graphics2D) graphics).translate(paper.getImageableX(), y);
        // print the text with its own routines
        PRINT_PANE.print(graphics);
        // translate the graphics object back to reality in the y dimension
        // so we can print a page number
        ((Graphics2D) graphics).translate(0, -y);
        Rectangle rect = graphics.getClipBounds();
        graphics.setClip(rect.x, 0, rect.width, (int) paper.getHeight() + 100);
        // get the name of the pane (or user name) and the time for the header
        Calendar cal = Calendar.getInstance();
        String header = cal.getTime().toString().trim();
        String name = getName() == null ? System.getProperty("user.name").trim() : getName().trim();
        String pageStr = String.valueOf(page + 1);
        Font font = Font.decode("Monospaced 8");
        graphics.setFont(font);
        FontMetrics fm = graphics.getFontMetrics(font);
        int width = SwingUtilities.computeStringWidth(fm, header);
        ((Graphics2D) graphics).drawString(header, (float) (paper.getImageableWidth() / 2 - width / 2), (float) paper.getImageableY() / 2 + fm.getHeight());
        ((Graphics2D) graphics).translate(0, paper.getImageableY() - fm.getHeight());
        double height = paper.getImageableHeight() + paper.getImageableY() / 2;
        width = SwingUtilities.computeStringWidth(fm, name);
        ((Graphics2D) graphics).drawString(name, (float) (paper.getImageableWidth() / 2 - width / 2), (float) height - fm.getHeight() / 2);
        ((Graphics2D) graphics).translate(0, fm.getHeight());
        width = SwingUtilities.computeStringWidth(fm, pageStr);
        ((Graphics2D) graphics).drawString(pageStr, (float) (paper.getImageableWidth() / 2 - width / 2), (float) height - fm.getHeight() / 2);
        return Printable.PAGE_EXISTS;
    }
    return Printable.NO_SUCH_PAGE;
}
Also used : FontMetrics(java.awt.FontMetrics) Calendar(java.util.Calendar) Rectangle(java.awt.Rectangle) Paper(java.awt.print.Paper) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Example 14 with Paper

use of java.awt.print.Paper in project groovy by apache.

the class TextEditor method print.

public int print(Graphics graphics, PageFormat pageFormat, int page) throws PrinterException {
    if (page < numPages) {
        Paper paper = pageFormat.getPaper();
        // initialize the PRINT_PANE (need this so that wrapping
        // can take place)
        PRINT_PANE.setDocument(getDocument());
        PRINT_PANE.setFont(getFont());
        PRINT_SIZE.setSize(paper.getImageableWidth(), getSize().getHeight());
        PRINT_PANE.setSize(PRINT_SIZE);
        // translate the graphics origin upwards so the area of the page we
        // want to print is in the origin; the clipping region auto set
        // will take care of the rest
        double y = -(page * paper.getImageableHeight()) + paper.getImageableY();
        ((Graphics2D) graphics).translate(paper.getImageableX(), y);
        // print the text with its own routines
        PRINT_PANE.print(graphics);
        // translate the graphics object back to reality in the y dimension
        // so we can print a page number
        ((Graphics2D) graphics).translate(0, -y);
        Rectangle rect = graphics.getClipBounds();
        graphics.setClip(rect.x, 0, rect.width, (int) paper.getHeight() + 100);
        // get the name of the pane (or user name) and the time for the header
        Calendar cal = Calendar.getInstance();
        String header = cal.getTime().toString().trim();
        String name = getName() == null ? System.getProperty("user.name").trim() : getName().trim();
        String pageStr = String.valueOf(page + 1);
        Font font = Font.decode("Monospaced 8");
        graphics.setFont(font);
        FontMetrics fm = graphics.getFontMetrics(font);
        int width = SwingUtilities.computeStringWidth(fm, header);
        ((Graphics2D) graphics).drawString(header, (float) (paper.getImageableWidth() / 2 - width / 2), (float) paper.getImageableY() / 2 + fm.getHeight());
        ((Graphics2D) graphics).translate(0, paper.getImageableY() - fm.getHeight());
        double height = paper.getImageableHeight() + paper.getImageableY() / 2;
        width = SwingUtilities.computeStringWidth(fm, name);
        ((Graphics2D) graphics).drawString(name, (float) (paper.getImageableWidth() / 2 - width / 2), (float) height - fm.getHeight() / 2);
        ((Graphics2D) graphics).translate(0, fm.getHeight());
        width = SwingUtilities.computeStringWidth(fm, pageStr);
        ((Graphics2D) graphics).drawString(pageStr, (float) (paper.getImageableWidth() / 2 - width / 2), (float) height - fm.getHeight() / 2);
        return Printable.PAGE_EXISTS;
    }
    return Printable.NO_SUCH_PAGE;
}
Also used : FontMetrics(java.awt.FontMetrics) Calendar(java.util.Calendar) Rectangle(java.awt.Rectangle) Paper(java.awt.print.Paper) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Example 15 with Paper

use of java.awt.print.Paper in project groovy by apache.

the class TextEditor method getNumberOfPages.

public int getNumberOfPages() {
    Paper paper = PAGE_FORMAT.getPaper();
    numPages = (int) Math.ceil(getSize().getHeight() / paper.getImageableHeight());
    return numPages;
}
Also used : Paper(java.awt.print.Paper)

Aggregations

Paper (java.awt.print.Paper)18 PageFormat (java.awt.print.PageFormat)12 MediaSize (javax.print.attribute.standard.MediaSize)6 Media (javax.print.attribute.standard.Media)5 PrintService (javax.print.PrintService)4 MediaSizeName (javax.print.attribute.standard.MediaSizeName)4 OrientationRequested (javax.print.attribute.standard.OrientationRequested)4 Graphics2D (java.awt.Graphics2D)3 Rectangle (java.awt.Rectangle)3 PrinterException (java.awt.print.PrinterException)3 MediaPrintableArea (javax.print.attribute.standard.MediaPrintableArea)3 Font (java.awt.Font)2 FontMetrics (java.awt.FontMetrics)2 HeadlessException (java.awt.HeadlessException)2 Printable (java.awt.print.Printable)2 PrinterAbortException (java.awt.print.PrinterAbortException)2 PrinterJob (java.awt.print.PrinterJob)2 IOException (java.io.IOException)2 Calendar (java.util.Calendar)2 Preferences (java.util.prefs.Preferences)2