protected MediaPrintableArea getDefaultPrintableArea(PageFormat page, double w, double h) { double ix, iw, iy, ih; if (w >= 72.0 * 6.0) { ix = 72.0; iw = w - 2 * 72.0; } else { ix = w / 6.0; iw = w * 0.75; } if (h >= 72.0 * 6.0) { iy = 72.0; ih = h - 2 * 72.0; } else { iy = h / 6.0; ih = h * 0.75; } return new MediaPrintableArea((float) (ix / DPI), (float) (iy / DPI), (float) (iw / DPI), (float) (ih / DPI), MediaPrintableArea.INCH); }
private MediaPrintableArea[] getAllPrintableAreas() { if (mpas == null) { Media[] media = (Media[])getSupportedAttributeValues(Media.class, null, null); mpas = new MediaPrintableArea[media.length]; for (int i=0; i< mpas.length; i++) { if (media[i] instanceof MediaSizeName) { MediaSizeName msn = (MediaSizeName)media[i]; MediaSize mediaSize = MediaSize.getMediaSizeForName(msn); if (mediaSize == null) { mpas[i] = (MediaPrintableArea) getDefaultAttributeValue(MediaPrintableArea.class); } else { mpas[i] = new MediaPrintableArea(0.25f, 0.25f, mediaSize.getX(MediaSize.INCH)-0.5f, mediaSize.getY(MediaSize.INCH)-0.5f, MediaSize.INCH); } } } } MediaPrintableArea[] mpasCopy = new MediaPrintableArea[mpas.length]; System.arraycopy(mpas, 0, mpasCopy, 0, mpas.length); return mpasCopy; }
private boolean isSupportedMediaPrintableArea(MediaPrintableArea mpa) { getMediaPrintables(null); int units = MediaPrintableArea.INCH; if (mediaPrintables != null) { for (int i=0; i<mediaPrintables.length; i++) { if ((mpa.getX(units) >= mediaPrintables[i].getX(units)) && (mpa.getY(units) >= mediaPrintables[i].getY(units)) && (mpa.getX(units) + mpa.getWidth(units) <= mediaPrintables[i].getX(units) + mediaPrintables[i].getWidth(units)) && (mpa.getY(units) + mpa.getHeight(units) <= mediaPrintables[i].getY(units) + mediaPrintables[i].getHeight(units))) { return true; } } } return false; }
/** * Called to update for new selected * print service. Tests if currently * selected attributes are supported. */ void updateForSelectedService() { if (categorySupported(MediaPrintableArea.class)) { left.setEnabled(true); right.setEnabled(true); top.setEnabled(true); bottom.setEnabled(true); left_tf.setEnabled(true); right_tf.setEnabled(true); top_tf.setEnabled(true); bottom_tf.setEnabled(true); } else { left.setEnabled(false); right.setEnabled(false); top.setEnabled(false); bottom.setEnabled(false); left_tf.setEnabled(false); right_tf.setEnabled(false); top_tf.setEnabled(false); bottom_tf.setEnabled(false); } }
/** * Get "nice" String Representation * @param ctx context * @return info */ public String toString (Properties ctx) { StringBuffer sb = new StringBuffer(); // Print Media size //AA Goodwill : Custom Paper Support if (m_mediaSize != null){ sb.append(m_mediaSize.getMediaSizeName()); // Print dimension String name = m_mediaSize.getMediaSizeName().toString(); if (!name.startsWith("iso")) sb.append(" - ").append(m_mediaSize.toString(MediaSize.INCH,"\"")) .append(" (").append(getMediaPrintableArea().toString(MediaPrintableArea.INCH,"\"")); if (!name.startsWith("na")) sb.append(" - ").append(m_mediaSize.toString(MediaSize.MM,"mm")) .append(" (").append(getMediaPrintableArea().toString(MediaPrintableArea.MM,"mm")); // Print Orientation sb.append(") - ") .append(Msg.getMsg(ctx, m_landscape ? "Landscape" : "Portrait")); } else sb.append("Custom - ").append(toString()); //End Of AA Goodwill return sb.toString(); }
protected PrintRequestAttributeSet updateMediaAndMarginsIfNeeded( PrintRequestAttributeSet newAttrs) { /* create copy of this.attrs*/ PrintRequestAttributeSet sum = new HashPrintRequestAttributeSet(this.attrs); /* remove MediaMargins and MediaPrintableArea attributes from the copy of job attributes if newAttrs contains MediaMargins or PrintableArea */ if (newAttrs.containsKey(MediaPrintableArea.class) || attrs.containsKey(MediaMargins.class)) { sum.remove(MediaPrintableArea.class); sum.remove(MediaMargins.class); } sum.addAll(newAttrs); return sum; }
private boolean isSupportedMediaPrintableArea(MediaPrintableArea mpa) { getMediaPrintables(null); if (mediaPrintables != null) { for (int i=0; i<mediaPrintables.length; i++) { if (mpa.equals(mediaPrintables[i])) { return true; } } } return false; }
@Override protected MediaPrintableArea getDefaultPrintableArea(PageFormat page, double w, double h) { final float dpi = 72.0f; Paper paper = page.getPaper(); return new MediaPrintableArea( (float) (paper.getImageableX() / dpi), (float) (paper.getImageableY() / dpi), (float) (paper.getImageableWidth() / dpi), (float) (paper.getImageableHeight() / dpi), MediaPrintableArea.INCH); }
private static void printWithCustomImageareaSize() { final JTable table = createAuthorTable(18); PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet(); printAttributes.add(DialogTypeSelection.NATIVE); printAttributes.add(new Copies(1)); printAttributes.add(new MediaPrintableArea( 0.25f, 0.25f, 8.0f, 5.0f, MediaPrintableArea.INCH)); Printable printable = table.getPrintable( JTable.PrintMode.NORMAL, new MessageFormat("Author Table"), new MessageFormat("Page - {0}") ); PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(printable); boolean printAccepted = job.printDialog(printAttributes); if (printAccepted) { try { job.print(printAttributes); closeFrame(); } catch (Exception e) { throw new RuntimeException(e); } } else { throw new RuntimeException("User cancels the printer job!"); } }
static double getPrintableXFromASet(PrintRequestAttributeSet aset) { try { return ((MediaPrintableArea) aset.get( MediaPrintableArea.class)).getX(MediaPrintableArea.INCH); } catch (Exception e) { return -1.0; } }
static double getPrintableYFromASet(PrintRequestAttributeSet aset) { try { return ((MediaPrintableArea) aset.get( MediaPrintableArea.class)).getY(MediaPrintableArea.INCH); } catch (Exception e) { return -1.0; } }
public static void main(String args[]) { PrinterJob job; job = PrinterJob.getPrinterJob(); PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); // Here you could see the PageFormat with an empty PrintRequestAttributeSet PageFormat pf2 = job.getPageFormat(pras); System.out.println((pf2.getImageableX() / 72f) + " " + (pf2.getImageableY() / 72f) + " " + (pf2.getImageableWidth() / 72f) + " " + (pf2.getImageableHeight() / 72f) ); // Set left margin to 2.0 pras.add(new MediaPrintableArea(2.0f, (float)(pf2.getImageableY() / 72f), (float) ((pf2.getImageableWidth() / 72f) - 1.0f), (float) (pf2.getImageableHeight() / 72f), MediaPrintableArea.INCH)); pf2 = job.getPageFormat(pras); System.out.println((pf2.getImageableX() / 72f) + " " + (pf2.getImageableY() / 72f) + " " + (pf2.getImageableWidth() / 72f) + " " + (pf2.getImageableHeight() / 72f) ); // check if returned left margin of imageable area is 2.0 as set earlier if (pf2.getImageableX() / 72f != 2.0f) { throw new RuntimeException("getPageFormat doesn't apply specified " + "MediaPrintableArea attribute"); } }