Search in sources :

Example 56 with PageFormat

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

the class ReportPrintFactory method getPageFormat.

/**
 * Generates a {@code PageFormat} given a {@code Preferences} node
 * @param p {@code Preferences} node
 * @return restored {@code PageFormat} or the default if it has not been saved before
 */
public static PageFormat getPageFormat(final Preferences p) {
    double height = p.getDouble(HEIGHT, 0);
    double width = p.getDouble(WIDTH, 0);
    int orientation = p.getInt(ORIENTATION, 0);
    double imageableHeight = p.getDouble(IMAGEABLE_HEIGHT, 0);
    double imageableWidth = p.getDouble(IMAGEABLE_WIDTH, 0);
    double imageableX = p.getDouble(IMAGEABLE_X, 0);
    double imageableY = p.getDouble(IMAGEABLE_Y, 0);
    if (height == 0 || width == 0 || imageableHeight == 0 || imageableWidth == 0) {
        return getDefaultPage();
    }
    final PageFormat pf = new PageFormat();
    pf.setOrientation(orientation);
    Paper paper = pf.getPaper();
    paper.setSize(width, height);
    paper.setImageableArea(imageableX, imageableY, imageableWidth, imageableHeight);
    pf.setPaper(paper);
    return pf;
}
Also used : PageFormat(java.awt.print.PageFormat) Paper(java.awt.print.Paper)

Example 57 with PageFormat

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

the class ReportPrintFactory method getDefaultPage.

/**
 * Returns the default paper size for a report
 *
 * @return page format
 */
public static PageFormat getDefaultPage() {
    /* A4 is the assumed default */
    PageSize defaultPageSize = PageSize.A4;
    /* US and Canada use letter size as the default */
    final Locale[] letterLocales = new Locale[] { Locale.US, Locale.CANADA, Locale.CANADA_FRENCH };
    if (Arrays.asList(letterLocales).contains(Locale.getDefault())) {
        defaultPageSize = PageSize.LETTER;
    }
    /* Create the default paper size with a default margin */
    final Paper paper = new Paper();
    double width = defaultPageSize.width;
    double height = defaultPageSize.height;
    paper.setSize(width, height);
    paper.setImageableArea(DEFAULT_MARGIN, DEFAULT_MARGIN, width - (2 * DEFAULT_MARGIN), height - (2 * DEFAULT_MARGIN));
    final PageFormat format = new PageFormat();
    format.setPaper(paper);
    return format;
}
Also used : Locale(java.util.Locale) PageFormat(java.awt.print.PageFormat) PageSize(jgnash.report.pdf.PageSize) Paper(java.awt.print.Paper)

Example 58 with PageFormat

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

the class PDFBoxTableTest method crossTabReportTest.

@Test
void crossTabReportTest() throws IOException {
    Path tempPath = null;
    Path tempRasterPath = null;
    try (final Report report = new SimpleReport()) {
        tempPath = Files.createTempFile("pdfTest", ".pdf");
        tempRasterPath = Files.createTempFile("pdfTest", ".png");
        final float padding = 2.5f;
        report.setTableFont(PDType1Font.COURIER);
        report.setHeaderFont(PDType1Font.HELVETICA_BOLD);
        report.setCellPadding(padding);
        report.setBaseFontSize(9);
        report.setForceGroupPagination(true);
        final PageFormat pageFormat = (PageFormat) report.getPageFormat().clone();
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
        report.setPageFormat(pageFormat);
        report.setFooterFont(PDType1Font.TIMES_ITALIC);
        report.setEllipsis("…");
        assertEquals(2, GroupInfo.getGroups(new CrossTabTestReport()).size());
        report.addTable(new CrossTabTestReport());
        report.addFooter();
        report.saveToFile(tempPath);
        assertTrue(report.isLandscape());
        assertEquals(padding, report.getCellPadding());
        assertTrue(Files.exists(tempPath));
        // Create a PNG file
        final BufferedImage bim = report.renderImage(0, 300);
        ImageIOUtil.writeImage(bim, tempRasterPath.toString(), 300);
        assertTrue(Files.exists(tempRasterPath));
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        if (tempPath != null) {
            Files.deleteIfExists(tempPath);
        }
        if (tempRasterPath != null) {
            Files.deleteIfExists(tempRasterPath);
        }
    }
}
Also used : Path(java.nio.file.Path) PageFormat(java.awt.print.PageFormat) Report(jgnash.report.pdf.Report) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.jupiter.api.Test)

Example 59 with PageFormat

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

the class PDFBoxTableTest method basicReportTest.

@Test
void basicReportTest() throws IOException {
    Path tempPath = null;
    Path tempRasterPath = null;
    try (final Report report = new SimpleReport()) {
        tempPath = Files.createTempFile("pdfTest", ".pdf");
        tempRasterPath = Files.createTempFile("pdfTest", ".png");
        final float padding = 2.5f;
        report.setTableFont(PDType1Font.COURIER);
        report.setHeaderFont(PDType1Font.HELVETICA_BOLD);
        report.setCellPadding(padding);
        report.setBaseFontSize(9);
        final PageFormat pageFormat = ReportPrintFactory.getDefaultPage();
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
        report.setPageFormat(pageFormat);
        report.setFooterFont(PDType1Font.TIMES_ITALIC);
        report.setEllipsis("…");
        assertEquals(1, GroupInfo.getGroups(new BasicTestReport()).size());
        // assertEquals(80, ((GroupInfo) GroupInfo.getGroups(new BasicTestReport()).toArray()[0]).rows);
        report.addTable(new BasicTestReport());
        report.addFooter();
        report.saveToFile(tempPath);
        assertTrue(report.isLandscape());
        assertEquals(padding, report.getCellPadding());
        assertTrue(Files.exists(tempPath));
        // Create a PNG file
        final BufferedImage bim = report.renderImage(0, 300);
        ImageIOUtil.writeImage(bim, tempRasterPath.toString(), 300);
        assertTrue(Files.exists(tempRasterPath));
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        if (tempPath != null) {
            Files.deleteIfExists(tempPath);
        }
        if (tempRasterPath != null) {
            Files.deleteIfExists(tempRasterPath);
        }
    }
}
Also used : Path(java.nio.file.Path) PageFormat(java.awt.print.PageFormat) Report(jgnash.report.pdf.Report) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.jupiter.api.Test)

Example 60 with PageFormat

use of java.awt.print.PageFormat in project netbeans-mmd-plugin by raydac.

the class Pages method getPreferredSize.

@Override
@Nonnull
public Dimension getPreferredSize() {
    final PrintPage[][] pages = this.parent.getPages();
    final PageFormat thePageFormat = this.parent.getPageFormat();
    final double scale = this.parent.getScale();
    int pagesAtHorizontal = 0;
    int pagesAtVertical = pages.length;
    final double paperWidth = thePageFormat.getWidth();
    final double paperHeight = thePageFormat.getHeight();
    for (final PrintPage[] row : pages) {
        pagesAtHorizontal = Math.max(pagesAtHorizontal, row.length);
    }
    final int width = (int) Math.round(INTERVAL_X + ((paperWidth + INTERVAL_X) * pagesAtHorizontal));
    final int height = (int) Math.round(INTERVAL_Y + ((paperHeight + INTERVAL_Y) * pagesAtVertical));
    return new Dimension((int) Math.round(width * scale), (int) Math.round(height * scale));
}
Also used : PageFormat(java.awt.print.PageFormat) Dimension(java.awt.Dimension) Nonnull(javax.annotation.Nonnull)

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