Java Code Examples for java.awt.print.PageFormat#setPaper()

The following examples show how to use java.awt.print.PageFormat#setPaper() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PSPrinterJob.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public EPSPrinter(Printable printable, String title,
                  PrintStream stream,
                  int x, int y, int wid, int hgt) {

    this.printable = printable;
    this.epsTitle = title;
    this.stream = stream;
    llx = x;
    lly = y;
    urx = llx+wid;
    ury = lly+hgt;
    // construct a PageFormat with zero margins representing the
    // exact bounds of the applet. ie construct a theoretical
    // paper which happens to exactly match applet panel size.
    Paper p = new Paper();
    p.setSize((double)wid, (double)hgt);
    p.setImageableArea(0.0,0.0, (double)wid, (double)hgt);
    pf = new PageFormat();
    pf.setPaper(p);
}
 
Example 2
Source File: PSPrinterJob.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public EPSPrinter(Printable printable, String title,
                  PrintStream stream,
                  int x, int y, int wid, int hgt) {

    this.printable = printable;
    this.epsTitle = title;
    this.stream = stream;
    llx = x;
    lly = y;
    urx = llx+wid;
    ury = lly+hgt;
    // construct a PageFormat with zero margins representing the
    // exact bounds of the applet. ie construct a theoretical
    // paper which happens to exactly match applet panel size.
    Paper p = new Paper();
    p.setSize((double)wid, (double)hgt);
    p.setImageableArea(0.0,0.0, (double)wid, (double)hgt);
    pf = new PageFormat();
    pf.setPaper(p);
}
 
Example 3
Source File: WrongPaperPrintingTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void doTest() {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(Chromaticity.MONOCHROME);

    MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
    float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
    Paper paper = new Paper();
    paper.setSize(size[0] * 72.0, size[1] * 72.0);
    paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
    PageFormat pf = new PageFormat();
    pf.setPaper(paper);

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(new WrongPaperPrintingTest(), job.validatePage(pf));
    if (job.printDialog()) {
        try {
            job.print(aset);
        } catch (PrinterException pe) {
            throw new RuntimeException(pe);
        }
    }
}
 
Example 4
Source File: PrintPreferences.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Get an instance of {@link java.awt.print.PageFormat}.
 * @param pj {@link java.awt.print.PrinterJob} which is 
 * associated with the default printer.
 * @return an instance of <code>PageFormat</code> that describes the size and
 * orientation of a page to be printed.
 */
public static PageFormat getPageFormat(PrinterJob pj) {
    PageFormat pageFormat = null;
    pageFormat = pj.defaultPage();
    Paper p = pageFormat.getPaper();
    int pageOrientation = getPreferences().getInt(PROP_PAGE_ORIENTATION, pageFormat.getOrientation());
    double paperWidth = getPreferences().getDouble(PROP_PAGE_WIDTH, p.getWidth());
    double paperHeight = getPreferences().getDouble(PROP_PAGE_HEIGHT, p.getHeight());
    
    double iaWidth = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_WIDTH, p.getImageableWidth());
    double iaHeight = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_HEIGHT, p.getImageableHeight());
    double iaX = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_X, p.getImageableX());
    double iaY = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_Y, p.getImageableY());
    
    pageFormat.setOrientation(pageOrientation);
    p.setSize(paperWidth, paperHeight);
    p.setImageableArea(iaX, iaY, iaWidth, iaHeight);
    pageFormat.setPaper(p);
    return pageFormat;
}
 
Example 5
Source File: RasterPrinterJob.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * The passed in PageFormat is cloned and altered to be usable on
 * the PrinterJob's current printer.
 */
public PageFormat validatePage(PageFormat page) {
    PageFormat newPage = (PageFormat)page.clone();
    Paper newPaper = new Paper();
    validatePaper(newPage.getPaper(), newPaper);
    newPage.setPaper(newPaper);

    return newPage;
}
 
Example 6
Source File: RasterPrinterJob.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * The passed in PageFormat is cloned and altered to be usable on
 * the PrinterJob's current printer.
 */
public PageFormat validatePage(PageFormat page) {
    PageFormat newPage = (PageFormat)page.clone();
    Paper newPaper = new Paper();
    validatePaper(newPage.getPaper(), newPaper);
    newPage.setPaper(newPaper);

    return newPage;
}
 
Example 7
Source File: PageDefinitionReadHandler.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Handles the page format.
 *
 * @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;
}
 
Example 8
Source File: RasterPrinterJob.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * The passed in PageFormat is cloned and altered to be usable on
 * the PrinterJob's current printer.
 */
public PageFormat validatePage(PageFormat page) {
    PageFormat newPage = (PageFormat)page.clone();
    Paper newPaper = new Paper();
    validatePaper(newPage.getPaper(), newPaper);
    newPage.setPaper(newPaper);

    return newPage;
}
 
Example 9
Source File: WrongPaperForBookPrintingTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void doTest() {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(Chromaticity.MONOCHROME);

    MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
    float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
    Paper paper = new Paper();
    paper.setSize(size[0] * 72.0, size[1] * 72.0);
    paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
    PageFormat pf = new PageFormat();
    pf.setPaper(paper);

    Book pageable = new Book();
    pageable.append(new WrongPaperForBookPrintingTest(), pf);

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPageable(pageable);
    if (job.printDialog()) {
        try {
            job.print(aset);
        } catch (PrinterException pe) {
            throw new RuntimeException(pe);
        }
    }
}
 
Example 10
Source File: PageFormatSerializer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Restores a page format after it has been serialized.
 *
 * @param data the serialized page format data.
 * @return the restored page format.
 */
private PageFormat createPageFormat( final Object[] data ) {
  final Integer orientation = (Integer) data[ 0 ];
  final float[] dim = (float[]) data[ 1 ];
  final float[] rect = (float[]) data[ 2 ];
  final Paper p = new Paper();
  p.setSize( dim[ 0 ], dim[ 1 ] );
  p.setImageableArea( rect[ 0 ], rect[ 1 ], rect[ 2 ], rect[ 3 ] );
  final PageFormat format = new PageFormat();
  format.setPaper( p );
  format.setOrientation( orientation.intValue() );
  return format;
}
 
Example 11
Source File: PageReadHandler.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Handles the page format.
 *
 * @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;
}
 
Example 12
Source File: RasterPrinterJob.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The passed in PageFormat will be copied and altered to describe
 * the default page size and orientation of the PrinterJob's
 * current printer.
 * Platform subclasses which can access the actual default paper size
 * for a printer may override this method.
 */
public PageFormat defaultPage(PageFormat page) {
    PageFormat newPage = (PageFormat)page.clone();
    newPage.setOrientation(PageFormat.PORTRAIT);
    Paper newPaper = new Paper();
    double ptsPerInch = 72.0;
    double w, h;
    Media media = null;

    PrintService service = getPrintService();
    if (service != null) {
        MediaSize size;
        media =
            (Media)service.getDefaultAttributeValue(Media.class);

        if (media instanceof MediaSizeName &&
           ((size = MediaSize.getMediaSizeForName((MediaSizeName)media)) !=
            null)) {
            w =  size.getX(MediaSize.INCH) * ptsPerInch;
            h =  size.getY(MediaSize.INCH) * ptsPerInch;
            newPaper.setSize(w, h);
            newPaper.setImageableArea(ptsPerInch, ptsPerInch,
                                      w - 2.0*ptsPerInch,
                                      h - 2.0*ptsPerInch);
            newPage.setPaper(newPaper);
            return newPage;

        }
    }

    /* Default to A4 paper outside North America.
     */
    String defaultCountry = Locale.getDefault().getCountry();
    if (!Locale.getDefault().equals(Locale.ENGLISH) && // ie "C"
        defaultCountry != null &&
        !defaultCountry.equals(Locale.US.getCountry()) &&
        !defaultCountry.equals(Locale.CANADA.getCountry())) {

        double mmPerInch = 25.4;
        w = Math.rint((210.0*ptsPerInch)/mmPerInch);
        h = Math.rint((297.0*ptsPerInch)/mmPerInch);
        newPaper.setSize(w, h);
        newPaper.setImageableArea(ptsPerInch, ptsPerInch,
                                  w - 2.0*ptsPerInch,
                                  h - 2.0*ptsPerInch);
    }

    newPage.setPaper(newPaper);

    return newPage;
}
 
Example 13
Source File: RasterPrinterJob.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The passed in PageFormat will be copied and altered to describe
 * the default page size and orientation of the PrinterJob's
 * current printer.
 * Platform subclasses which can access the actual default paper size
 * for a printer may override this method.
 */
public PageFormat defaultPage(PageFormat page) {
    PageFormat newPage = (PageFormat)page.clone();
    newPage.setOrientation(PageFormat.PORTRAIT);
    Paper newPaper = new Paper();
    double ptsPerInch = 72.0;
    double w, h;
    Media media = null;

    PrintService service = getPrintService();
    if (service != null) {
        MediaSize size;
        media =
            (Media)service.getDefaultAttributeValue(Media.class);

        if (media instanceof MediaSizeName &&
           ((size = MediaSize.getMediaSizeForName((MediaSizeName)media)) !=
            null)) {
            w =  size.getX(MediaSize.INCH) * ptsPerInch;
            h =  size.getY(MediaSize.INCH) * ptsPerInch;
            newPaper.setSize(w, h);
            newPaper.setImageableArea(ptsPerInch, ptsPerInch,
                                      w - 2.0*ptsPerInch,
                                      h - 2.0*ptsPerInch);
            newPage.setPaper(newPaper);
            return newPage;

        }
    }

    /* Default to A4 paper outside North America.
     */
    String defaultCountry = Locale.getDefault().getCountry();
    if (!Locale.getDefault().equals(Locale.ENGLISH) && // ie "C"
        defaultCountry != null &&
        !defaultCountry.equals(Locale.US.getCountry()) &&
        !defaultCountry.equals(Locale.CANADA.getCountry())) {

        double mmPerInch = 25.4;
        w = Math.rint((210.0*ptsPerInch)/mmPerInch);
        h = Math.rint((297.0*ptsPerInch)/mmPerInch);
        newPaper.setSize(w, h);
        newPaper.setImageableArea(ptsPerInch, ptsPerInch,
                                  w - 2.0*ptsPerInch,
                                  h - 2.0*ptsPerInch);
    }

    newPage.setPaper(newPaper);

    return newPage;
}
 
Example 14
Source File: PrintJob2D.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public boolean printDialog() {

        boolean proceedWithPrint = false;

        printerJob = PrinterJob.getPrinterJob();
        if (printerJob == null) {
            return false;
        }
        DialogType d = this.jobAttributes.getDialog();
        PrintService pServ = printerJob.getPrintService();
        if ((pServ == null) &&  (d == DialogType.NONE)){
            return false;
        }
        copyAttributes(pServ);

        DefaultSelectionType select =
            this.jobAttributes.getDefaultSelection();
        if (select == DefaultSelectionType.RANGE) {
            attributes.add(SunPageSelection.RANGE);
        } else if (select == DefaultSelectionType.SELECTION) {
            attributes.add(SunPageSelection.SELECTION);
        } else {
            attributes.add(SunPageSelection.ALL);
        }

        if (frame != null) {
             attributes.add(new DialogOwner(frame));
         }

        if ( d == DialogType.NONE) {
            proceedWithPrint = true;
        } else {
            if (d == DialogType.NATIVE) {
                attributes.add(DialogTypeSelection.NATIVE);
            }  else { //  (d == DialogType.COMMON)
                attributes.add(DialogTypeSelection.COMMON);
            }
            if (proceedWithPrint = printerJob.printDialog(attributes)) {
                if (pServ == null) {
                    // Windows gives an option to install a service
                    // when it detects there are no printers so
                    // we make sure we get the updated print service.
                    pServ = printerJob.getPrintService();
                    if (pServ == null) {
                        return false;
                    }
                }
                updateAttributes();
                translateOutputProps();
            }
        }

        if (proceedWithPrint) {

            JobName jname = (JobName)attributes.get(JobName.class);
            if (jname != null) {
                printerJob.setJobName(jname.toString());
            }

            pageFormat = new PageFormat();

            Media media = (Media)attributes.get(Media.class);
            MediaSize mediaSize =  null;
            if (media != null  && media instanceof MediaSizeName) {
                mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
            }

            Paper p = pageFormat.getPaper();
            if (mediaSize != null) {
                p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
                          mediaSize.getY(MediaSize.INCH)*72.0);
            }

            if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
                // AWT uses 1/4" borders by default
                p.setImageableArea(18.0, 18.0,
                                   p.getWidth()-36.0,
                                   p.getHeight()-36.0);
            } else {
                p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
            }

            pageFormat.setPaper(p);

            OrientationRequested orient =
               (OrientationRequested)attributes.get(OrientationRequested.class);
            if (orient!= null &&
                orient == OrientationRequested.REVERSE_LANDSCAPE) {
                pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
            } else if (orient == OrientationRequested.LANDSCAPE) {
                pageFormat.setOrientation(PageFormat.LANDSCAPE);
            } else {
                pageFormat.setOrientation(PageFormat.PORTRAIT);
                }

            printerJob.setPrintable(this, pageFormat);

        }

        return proceedWithPrint;
    }
 
Example 15
Source File: RasterPrinterJob.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The passed in PageFormat will be copied and altered to describe
 * the default page size and orientation of the PrinterJob's
 * current printer.
 * Platform subclasses which can access the actual default paper size
 * for a printer may override this method.
 */
public PageFormat defaultPage(PageFormat page) {
    PageFormat newPage = (PageFormat)page.clone();
    newPage.setOrientation(PageFormat.PORTRAIT);
    Paper newPaper = new Paper();
    double ptsPerInch = 72.0;
    double w, h;
    Media media = null;

    PrintService service = getPrintService();
    if (service != null) {
        MediaSize size;
        media =
            (Media)service.getDefaultAttributeValue(Media.class);

        if (media instanceof MediaSizeName &&
           ((size = MediaSize.getMediaSizeForName((MediaSizeName)media)) !=
            null)) {
            w =  size.getX(MediaSize.INCH) * ptsPerInch;
            h =  size.getY(MediaSize.INCH) * ptsPerInch;
            newPaper.setSize(w, h);
            newPaper.setImageableArea(ptsPerInch, ptsPerInch,
                                      w - 2.0*ptsPerInch,
                                      h - 2.0*ptsPerInch);
            newPage.setPaper(newPaper);
            return newPage;

        }
    }

    /* Default to A4 paper outside North America.
     */
    String defaultCountry = Locale.getDefault().getCountry();
    if (!Locale.getDefault().equals(Locale.ENGLISH) && // ie "C"
        defaultCountry != null &&
        !defaultCountry.equals(Locale.US.getCountry()) &&
        !defaultCountry.equals(Locale.CANADA.getCountry())) {

        double mmPerInch = 25.4;
        w = Math.rint((210.0*ptsPerInch)/mmPerInch);
        h = Math.rint((297.0*ptsPerInch)/mmPerInch);
        newPaper.setSize(w, h);
        newPaper.setImageableArea(ptsPerInch, ptsPerInch,
                                  w - 2.0*ptsPerInch,
                                  h - 2.0*ptsPerInch);
    }

    newPage.setPaper(newPaper);

    return newPage;
}
 
Example 16
Source File: PrintJob2D.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public boolean printDialog() {

        boolean proceedWithPrint = false;

        printerJob = PrinterJob.getPrinterJob();
        if (printerJob == null) {
            return false;
        }
        DialogType d = this.jobAttributes.getDialog();
        PrintService pServ = printerJob.getPrintService();
        if ((pServ == null) &&  (d == DialogType.NONE)){
            return false;
        }
        copyAttributes(pServ);

        DefaultSelectionType select =
            this.jobAttributes.getDefaultSelection();
        if (select == DefaultSelectionType.RANGE) {
            attributes.add(SunPageSelection.RANGE);
        } else if (select == DefaultSelectionType.SELECTION) {
            attributes.add(SunPageSelection.SELECTION);
        } else {
            attributes.add(SunPageSelection.ALL);
        }

        if (frame != null) {
             attributes.add(new DialogOwner(frame));
         }

        if ( d == DialogType.NONE) {
            proceedWithPrint = true;
        } else {
            if (d == DialogType.NATIVE) {
                attributes.add(DialogTypeSelection.NATIVE);
            }  else { //  (d == DialogType.COMMON)
                attributes.add(DialogTypeSelection.COMMON);
            }
            if (proceedWithPrint = printerJob.printDialog(attributes)) {
                if (pServ == null) {
                    // Windows gives an option to install a service
                    // when it detects there are no printers so
                    // we make sure we get the updated print service.
                    pServ = printerJob.getPrintService();
                    if (pServ == null) {
                        return false;
                    }
                }
                updateAttributes();
                translateOutputProps();
            }
        }

        if (proceedWithPrint) {

            JobName jname = (JobName)attributes.get(JobName.class);
            if (jname != null) {
                printerJob.setJobName(jname.toString());
            }

            pageFormat = new PageFormat();

            Media media = (Media)attributes.get(Media.class);
            MediaSize mediaSize =  null;
            if (media != null  && media instanceof MediaSizeName) {
                mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
            }

            Paper p = pageFormat.getPaper();
            if (mediaSize != null) {
                p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
                          mediaSize.getY(MediaSize.INCH)*72.0);
            }

            if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
                // AWT uses 1/4" borders by default
                p.setImageableArea(18.0, 18.0,
                                   p.getWidth()-36.0,
                                   p.getHeight()-36.0);
            } else {
                p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
            }

            pageFormat.setPaper(p);

            OrientationRequested orient =
               (OrientationRequested)attributes.get(OrientationRequested.class);
            if (orient!= null &&
                orient == OrientationRequested.REVERSE_LANDSCAPE) {
                pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
            } else if (orient == OrientationRequested.LANDSCAPE) {
                pageFormat.setOrientation(PageFormat.LANDSCAPE);
            } else {
                pageFormat.setOrientation(PageFormat.PORTRAIT);
                }

            printerJob.setPrintable(this, pageFormat);

        }

        return proceedWithPrint;
    }
 
Example 17
Source File: Options.java    From ramus with GNU General Public License v3.0 4 votes vote down vote up
public static PageFormat getPageFormat(String name, PageFormat pageFormat) {
    Properties properties = getProperties(name);
    if (properties == null)
        return pageFormat;
    final String orientation = properties.getProperty(ORIENTATION);
    if (LANDSCAPE.equals(orientation))
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
    else if (PORTRAIT.equals(orientation))
        pageFormat.setOrientation(PageFormat.PORTRAIT);
    else if (REVERSE_LANDSCAPE.equals(orientation))
        pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);

    String s = properties.getProperty(PAPER_IMAGEABLE_HEIGHT);
    if (s == null)
        return pageFormat;
    double iHeight = Double.parseDouble(s);
    s = properties.getProperty(PAPER_IMAGEABLE_WIDTH);
    if (s == null)
        return pageFormat;
    double iWidth = Double.parseDouble(s);

    s = properties.getProperty(PAPER_HEIGHT);
    if (s == null)
        return pageFormat;
    double height = Double.parseDouble(s);
    s = properties.getProperty(PAPER_WIDTH);
    if (s == null)
        return pageFormat;
    double width = Double.parseDouble(s);

    s = properties.getProperty(PAPER_IMAGEABLE_X);
    if (s == null)
        return pageFormat;
    final double x = Double.parseDouble(s);
    s = properties.getProperty(PAPER_IMAGEABLE_Y);
    if (s == null)
        return pageFormat;
    final double y = Double.parseDouble(s);
    final Paper paper = pageFormat.getPaper();
    paper.setImageableArea(x, y, iWidth, iHeight);
    paper.setSize(width, height);
    pageFormat.setPaper(paper);
    return pageFormat;
}
 
Example 18
Source File: PageFormatFactory.java    From pentaho-reporting with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Creates a new pageformat using the given paper and the given orientation.
 *
 * @param paper
 *          the paper to use in the new pageformat
 * @param orientation
 *          one of PageFormat.PORTRAIT, PageFormat.LANDSCAPE or PageFormat.REVERSE_LANDSCAPE
 * @return the created Pageformat
 * @throws NullPointerException
 *           if the paper given was null
 */
public PageFormat createPageFormat( final Paper paper, final int orientation ) {
  if ( paper == null ) {
    throw new NullPointerException( "Paper given must not be null" );
  }
  final PageFormat pf = new PageFormat();
  pf.setPaper( paper );
  pf.setOrientation( orientation );
  return pf;
}
 
Example 19
Source File: PrinterAttributesPanel.java    From tn5250j with GNU General Public License v2.0 3 votes vote down vote up
private void getLandscapeAttributes() {

    PrinterJob printJob = PrinterJob.getPrinterJob();

    PageFormat documentPageFormat = new PageFormat();
    documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
    documentPageFormat.setPaper(pappyLand);

    documentPageFormat = printJob.pageDialog(documentPageFormat);

    pappyLand = documentPageFormat.getPaper();


  }
 
Example 20
Source File: PrintPageDirectNew.java    From haxademic with MIT License 2 votes vote down vote up
public void sendToDefaultPrinter() {
	// configure the printing job & simple single-page document
	PrinterJob printJob = PrinterJob.getPrinterJob();
	
	
	
	PageFormat pageFormat = printJob.defaultPage();
	Paper paper = pageFormat.getPaper();
	
	float inchWidth = 16;
	float inchHeight = 20;
	
	float marginWidth = 100;
	float marginHeight = 100;
	
	int pWidth = (int) ((inchWidth * 25.4f) * 2.835f);// quick: convert inch to mm, mm to pt
	int pHeight = (int) ((inchHeight * 25.4f) * 2.835f);
	
	paper.setSize(pWidth, pHeight);
	paper.setImageableArea(marginWidth, marginHeight, pWidth, pHeight);
	pageFormat.setPaper(paper);
	
	//not sure this exactly works, we should probably just scale the square to fit and we send it the correct rotation orientation
	//pageFormat.setOrientation(PageFormat.LANDSCAPE);
	

	// we might also be able to set attributes here
	Printable page = new ImagePage();
	Book book = new Book();
	
	book.append(page, pageFormat);
	
	
	/*
	Early testing at "LikeMinded Productions":
	
	PageFormat documentPageFormat = new PageFormat();
	
	// new settings to play with
	Paper pape = new Paper();
	pape.setSize(10000, 6000);
	pape.setImageableArea(0, 0, 10000, 6000);
	documentPageFormat.setPaper(pape);
	
	documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
	*/
	
	
	printJob.setPageable(book);

	// use the print dialog UI or just print it!
	if(useDialog) {
		if (printJob.printDialog()) printPage(printJob);
	} else {
		printPage(printJob);
	}
}