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); } } }
/** * Set Media Size * @param x the value to which to set this <code>Paper</code> object's width * @param y the value to which to set this <code>Paper</code> object's height * @param units number of microns (see Size2DSyntax.INCH, Size2DSyntax.MM) * @param landscape true if it's landscape format * @see Paper#setSize(double, double) */ public void setMediaSize (double x, double y, int units, boolean landscape) { if (x == 0 || y == 0) throw new IllegalArgumentException("MediaSize is null"); m_landscape = landscape; // Get Sise in Inch * 72 final double mult = (double)units / (double)Size2DSyntax.INCH * (double)72; final double width = x * mult; final double height = y * mult; // Set Size setSize (width, height); log.debug("Width & Height" + ": " + x + "/" + y + " - Landscape=" + m_landscape); }
public Paper getPaper() { final short size = getDmPaperSize(structPtr); Paper p = StdPaper.getPaper(size); if (p == null) { final long fields = getDmFields(); if (((fields & DM_PAPERLENGTH) != 0) && ((fields & DM_PAPERWIDTH) != 0)) { p = new CustomPaper(size, new MediaSize( getDmPaperWidth(structPtr) / 10, getDmPaperLength(structPtr) / 10, Size2DSyntax.MM)); } } return p; }
public static MediaSizeName[] getSupportedMediaSizeNames(final long handle) throws PrintException { final MediaSizeName[] names; final int[] sizes = getSupportedPaperSizes(handle); final Vector<MediaSizeName> v = new Vector<MediaSizeName>( sizes.length / 2); for (int i = 0; i < sizes.length; i += 2) { if ((sizes[i] > 0) && (sizes[i + 1] > 0)) { final MediaSizeName name = MediaSize.findMedia(sizes[i] / 10, sizes[i + 1] / 10, Size2DSyntax.MM); if ((name != null) && !v.contains(name)) { v.add(name); } } } names = new MediaSizeName[v.size()]; return v.toArray(names); }
public static MediaSizeName mapMedia(MediaType mType) { MediaSizeName media = null; // JAVAXSIZES.length and SIZES.length must be equal! // Attempt to recover by getting the smaller size. int length = Math.min(SIZES.length, JAVAXSIZES.length); for (int i=0; i < length; i++) { if (SIZES[i] == mType) { if ((JAVAXSIZES[i] != null) && MediaSize.getMediaSizeForName(JAVAXSIZES[i]) != null) { media = JAVAXSIZES[i]; break; } else { /* create Custom Media */ media = new CustomMediaSizeName(SIZES[i].toString()); float w = (float)Math.rint(WIDTHS[i] / 72.0); float h = (float)Math.rint(LENGTHS[i] / 72.0); if (w > 0.0 && h > 0.0) { // add new created MediaSize to our static map // so it will be found when we call findMedia new MediaSize(w, h, Size2DSyntax.INCH, media); } break; } } } return media; }
private void print(final PrintPackageRequest request) throws PrinterException { logger.log(Level.FINE, "Printing request {}", request); // Create Print Job final PrinterJob pjob = PrinterJob.getPrinterJob(); pjob.setJobName(request.getPrintJobName()); final PageFormat pf = pjob.defaultPage(); final Paper paper = pjob.defaultPage().getPaper(); final MediaSize size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A4); paper.setSize(size.getSize(Size2DSyntax.INCH)[0] * 72, size.getSize(Size2DSyntax.INCH)[1] * 72); paper.setImageableArea(0, 0, size.getSize(Size2DSyntax.INCH)[0] * 72, size.getSize(Size2DSyntax.INCH)[1] * 72); pf.setPaper(paper); final Book book = new Book();// java.awt.print.Book book.append(request.getPrintable(), pf, request.getNumPages()); pjob.setPageable(book); pjob.setPrintService(request.getPrintService()); pjob.print(request.getAttributes()); // task 09618: allow us to configure the client to return an error even if everything went OK, so we can test final String alwaysReturnError = Context.getContext().getProperty(Context.CTX_Testing_AlwaysReturnError, Context.DEFAULT_AlwaysReturnError); if (Boolean.parseBoolean(alwaysReturnError)) { logger.log(Level.INFO, "{} is true, so we report an error, despite the print was OK", Context.CTX_Testing_AlwaysReturnError); final String errorMsg = Context.getContext().getProperty(Context.CTX_Testing_ErrorMessage, Context.DEFAULT_ErrorMessage); throw new PrinterException(errorMsg); } }
public void setPaper(final Paper paper) { if (paper != null) { if (paper.getDmPaperSize() > 0) { setDmPaperSize(structPtr, paper.getDmPaperSize()); } else { setDmPaperWidth(structPtr, (short) (paper.getSize().getX( Size2DSyntax.MM) * 10)); setDmPaperLength(structPtr, (short) (paper.getSize().getY( Size2DSyntax.MM) * 10)); } } }
protected PPDMediaSizeName(int value, float x, float y) { super(value); if (x > y) { float z = x; y = x; x = z; } new MediaSize(x / 72, y / 72, Size2DSyntax.INCH, this); }
protected HashPrintRequestAttributeSet getAttrsForPageFormat(PageFormat page) { HashPrintRequestAttributeSet lattrs=new HashPrintRequestAttributeSet(); /* Add Orientation attribute */ switch (page.getOrientation()) { case PageFormat.LANDSCAPE: lattrs.add(OrientationRequested.LANDSCAPE); break; case PageFormat.PORTRAIT: lattrs.add(OrientationRequested.PORTRAIT); break; case PageFormat.REVERSE_LANDSCAPE: lattrs.add(OrientationRequested.REVERSE_LANDSCAPE); break; } /* Add Media attribute */ MediaSizeName media = MediaSize.findMedia( (float) (page.getWidth() / 72.0), (float) (page.getHeight() / 72.0), Size2DSyntax.INCH); if (media != null) { lattrs.add(media); } /* Add MediaMargins attribute */ lattrs.add(new MediaMargins((float) (page.getImageableX() / 72.0), (float) (page.getImageableY() / 72.0), (float) ((page.getWidth() - page.getImageableX() - page.getImageableWidth()) / 72.0), (float) ((page.getHeight() - page.getImageableHeight() - page.getImageableY()) / 72.0), MediaMargins.INCH)); return lattrs; }
private static PrintRequestAttributeSet formatToAttrs( final PageFormat format) { final PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet(); if (format != null) { attributes.add(new MediaPrintableArea((float) (format .getImageableX() / 72.0), (float) (format .getImageableY() / 72.0), (float) (format .getWidth() / 72.0), (float) (format.getHeight() / 72.0), Size2DSyntax.INCH)); switch (format.getOrientation()) { case PageFormat.PORTRAIT: attributes.add(OrientationRequested.PORTRAIT); break; case PageFormat.LANDSCAPE: attributes.add(OrientationRequested.LANDSCAPE); break; case PageFormat.REVERSE_LANDSCAPE: attributes.add(OrientationRequested.REVERSE_LANDSCAPE); break; } } return attributes; }
/** * @param media The {@link Media} to extract dimensions for. * @param units The units to return. * @return The dimensions of the specified {@link Media}. */ public static double[] getMediaDimensions(Media media, LengthUnits units) { MediaSize size = media instanceof MediaSizeName ? MediaSize.getMediaSizeForName((MediaSizeName) media) : null; if (size == null) { size = MediaSize.NA.LETTER; } return new double[] { units.convert(LengthUnits.IN, size.getX(Size2DSyntax.INCH)), units.convert(LengthUnits.IN, size.getY(Size2DSyntax.INCH)) }; }
/** * Sets the paper size. * * @param service The {@link PrintService} to use. * @param set The {@link PrintRequestAttributeSet} to use. * @param size The size of the paper. * @param units The type of units being used. */ public static void setPaperSize(PrintService service, PrintRequestAttributeSet set, double[] size, LengthUnits units) { double[] margins = getPaperMargins(service, set, units); MediaSizeName mediaSizeName = MediaSize.findMedia((float) LengthUnits.IN.convert(units, size[0]), (float) LengthUnits.IN.convert(units, size[1]), Size2DSyntax.INCH); if (mediaSizeName == null) { mediaSizeName = MediaSizeName.NA_LETTER; } set.add(mediaSizeName); setPaperMargins(service, set, margins, units); }
public static MediaSizeName getMediaSizeName(PDDocument document) throws IOException { PDFRenderer renderer = new PDFRenderer(document); BufferedImage image = renderer.renderImageWithDPI(0, 72f); float w, h, swp; w = image.getWidth() / 72f; h = image.getHeight() / 72f; if (w > h) { swp = w; w = h; h = swp; } return MediaSize.findMedia(w, h, Size2DSyntax.INCH); }
private PageFormat getPageFormat(final AttributeSet... attrSets) { final Paper paper = new Paper(); final PageFormat format = new PageFormat(); final DevmodeStructWrapper dm = service.getPrinterProps(); final OrientationRequested o = dm.getOrientation(); final MediaPrintableArea area = getAttribute( MediaPrintableArea.class, attrSets); DevmodeStructWrapper.Paper p = dm.getPaper(); if (p == null) { p = (DevmodeStructWrapper.Paper) service .getDefaultAttributeValue(DevmodeStructWrapper.Paper.class); dm.setPaper(p); } paper.setSize(p.getSize().getX(Size2DSyntax.INCH) * 72.0, p .getSize().getY(Size2DSyntax.INCH) * 72.0); format.setPaper(paper); if (OrientationRequested.LANDSCAPE.equals(o) || OrientationRequested.REVERSE_LANDSCAPE.equals(o)) { format.setOrientation(PageFormat.LANDSCAPE); } else { format.setOrientation(PageFormat.PORTRAIT); } if (area != null) { paper.setImageableArea(area.getX(MediaPrintableArea.INCH) * 72, area.getY(MediaPrintableArea.INCH) * 72, area.getWidth(MediaPrintableArea.INCH) * 72, area.getHeight(MediaPrintableArea.INCH) * 72); } else { final double x = paper.getWidth() / 10; final double y = paper.getHeight() / 10; paper.setImageableArea(x, y, (paper.getWidth() - 2 * x), (paper .getHeight() - 2 * y)); } return format; }
private boolean updateMargins() { float x1; float y1; float x2; float y2; NumberFormatter format = getFloatFormatter(); if (!leftTxt.isEnabled()) { removeAttribute(MediaPrintableArea.class); removeAttribute(MediaMargins.class); return true; } try { x1 = ((Float) format.stringToValue(leftTxt.getText())).floatValue(); x2 = ((Float) format.stringToValue(rightTxt.getText())).floatValue(); y1 = ((Float) format.stringToValue(topTxt.getText())).floatValue(); y2 = ((Float) format.stringToValue(bottomTxt.getText())).floatValue(); } catch(ParseException e) { return false; } if (sizeBox.isEnabled() && (sizeBox.getSelectedItem() instanceof MediaSizeName) && myService.isAttributeCategorySupported(MediaPrintableArea.class)) { MediaSize mediaSize = MediaSize.getMediaSizeForName( (MediaSizeName) sizeBox.getSelectedItem()); float paperWidth = mediaSize.getX(Size2DSyntax.MM); float paperHeight = mediaSize.getY(Size2DSyntax.MM); if ((x1 + x2 >= paperWidth) || (y1 + y2 >= paperHeight)) { return false; } newAttrs.add(new MediaPrintableArea(x1, y1, paperWidth - x1 - x2, paperHeight - y1 - y2, MediaPrintableArea.MM)); } else { removeAttribute(MediaPrintableArea.class); } if (myService.isAttributeCategorySupported(MediaMargins.class)) { newAttrs.add(new MediaMargins(x1, y1, x2, y2, MediaMargins.MM)); } else { removeAttribute(MediaMargins.class); } return true; }
public static Object getIppValue(Attribute attr, byte ippvtag) { Object o = null; switch (ippvtag) { // integer values for the "value-tag" field. case IppAttribute.TAG_BOOLEAN: case IppAttribute.TAG_INTEGER: case IppAttribute.TAG_ENUM: if (attr instanceof IntegerSyntax) { o = new Integer(((IntegerSyntax) attr).getValue()); } else if (attr instanceof EnumSyntax) { o = new Integer(((EnumSyntax) attr).getValue()); } else if (attr instanceof DateTimeSyntax || attr instanceof ResolutionSyntax || attr instanceof SetOfIntegerSyntax || attr instanceof Size2DSyntax || attr instanceof TextSyntax || attr instanceof URISyntax) { // TODO - process other attr's types } break; // octetString values for the "value-tag" field. case IppAttribute.TAG_DATETIME: case IppAttribute.TAG_RESOLUTION: case IppAttribute.TAG_RANGEOFINTEGER: case IppAttribute.TAG_OCTETSTRINGUNSPECIFIEDFORMAT: case IppAttribute.TAG_TEXTWITHLANGUAGE: case IppAttribute.TAG_NAMEWITHLANGUAGE: if (attr instanceof IntegerSyntax) { // TODO - it seems that this needs to be fixed o = new Integer(((IntegerSyntax) attr).toString()); } else if (attr instanceof EnumSyntax) { // TODO - it seems that this needs to be fixed o = new Integer(((EnumSyntax) attr).toString()); } else if (attr instanceof DateTimeSyntax || attr instanceof ResolutionSyntax || attr instanceof SetOfIntegerSyntax || attr instanceof Size2DSyntax) { // TODO - process other attr's types } else if (attr instanceof TextSyntax) { // TODO - it seems that this needs to be fixed o = new Integer(((TextSyntax) attr).toString()); } else if (attr instanceof URISyntax) { // TODO - it seems that this needs to be fixed o = new Integer(((URISyntax) attr).toString()); } break; // character-string values for the "value-tag" field case IppAttribute.TAG_TEXTWITHOUTLANGUAGE: case IppAttribute.TAG_NAMEWITHOUTLANGUAGE: case IppAttribute.TAG_KEYWORD: case IppAttribute.TAG_URI: case IppAttribute.TAG_URISCHEME: case IppAttribute.TAG_CHARSET: case IppAttribute.TAG_NATURAL_LANGUAGE: case IppAttribute.TAG_MIMEMEDIATYPE: if (attr instanceof IntegerSyntax) { o = ((IntegerSyntax) attr).toString(); } else if (attr instanceof EnumSyntax) { o = ((EnumSyntax) attr).toString(); } else if (attr instanceof DateTimeSyntax || attr instanceof ResolutionSyntax || attr instanceof SetOfIntegerSyntax || attr instanceof Size2DSyntax) { // TODO - process other attr's types } else if (attr instanceof TextSyntax) { o = ((TextSyntax) attr).toString(); } else if (attr instanceof URISyntax) { o = ((URISyntax) attr).toString(); } break; default: break; } return o; }
protected PageFormat getPageFormatForAttrs( PrintRequestAttributeSet newattrs) { PageFormat pf = new PageFormat(); if (newattrs.containsKey(OrientationRequested.class)) { OrientationRequested or = (OrientationRequested) newattrs.get(OrientationRequested.class); pf.setOrientation(or.equals(OrientationRequested.LANDSCAPE) ? PageFormat.LANDSCAPE : (or.equals(OrientationRequested.REVERSE_LANDSCAPE) ? PageFormat.REVERSE_LANDSCAPE : PageFormat.PORTRAIT)); } Paper paper = new Paper(); MediaSize size = MediaSize.getMediaSizeForName( newattrs.containsKey(Media.class) && (newattrs.get(Media.class).getClass(). isAssignableFrom(MediaSizeName.class)) ? (MediaSizeName)newattrs.get(Media.class) : MediaSizeName.ISO_A4); paper.setSize(size.getX(Size2DSyntax.INCH) * 72.0, size.getY(Size2DSyntax.INCH) * 72.0); MediaMargins mm; if (newattrs.containsKey(MediaMargins.class)) { mm = (MediaMargins) newattrs.get(MediaMargins.class); } else if(newattrs.containsKey(MediaPrintableArea.class)) { mm = new MediaMargins(size, (MediaPrintableArea) attrs.get(MediaPrintableArea.class)); } else { mm = new MediaMargins(25.4F, 25.4F, 25.4F, 25.4F, MediaMargins.MM); } paper.setImageableArea(mm.getX1(MediaMargins.INCH) * 72.0, mm.getY1(MediaMargins.INCH) * 72.0, (size.getX(Size2DSyntax.INCH) - mm.getX1(MediaMargins.INCH) - mm.getX2(MediaMargins.INCH)) * 72.0, (size.getY(Size2DSyntax.INCH) - mm.getY1(MediaMargins.INCH) - mm.getY2(MediaMargins.INCH)) * 72.0 ); pf.setPaper(paper); return pf; }
private static PageFormat attrsToFormat( final PrintRequestAttributeSet attributes) { if (attributes == null) { return new PageFormat(); } final PageFormat format = new PageFormat(); final Paper paper = new Paper(); final OrientationRequested orient = (OrientationRequested) attributes .get(OrientationRequested.class); final MediaSize size = attributes.containsKey(Media.class) ? MediaSize .getMediaSizeForName((MediaSizeName) attributes .get(Media.class)) : (MediaSize) attributes.get(MediaSize.class); final MediaPrintableArea area = (MediaPrintableArea) attributes .get(MediaPrintableArea.class); if (orient != null) { if (orient.equals(OrientationRequested.LANDSCAPE)) { format.setOrientation(PageFormat.LANDSCAPE); } else if (orient.equals(OrientationRequested.REVERSE_LANDSCAPE)) { format.setOrientation(PageFormat.REVERSE_LANDSCAPE); } } if (size != null) { paper.setSize(size.getX(Size2DSyntax.INCH) * 72.0, size .getY(Size2DSyntax.INCH) * 72.0); } if (area != null) { paper.setImageableArea(area.getX(Size2DSyntax.INCH) * 72.0, area .getY(Size2DSyntax.INCH) * 72.0, area .getWidth(Size2DSyntax.INCH) * 72.0, area .getHeight(Size2DSyntax.INCH) * 72.0); } format.setPaper(paper); return format; }