/** * Sets the media size. * @param name the MediaSizeName */ public static void setMediaSizeName(MediaSizeName name) { mediaSizeName = name; mediaSize = MediaSize.getMediaSizeForName(mediaSizeName); area = new MediaPrintableArea(0.5f, 0.5f, mediaSize.getX(MediaSize.INCH) - 1.0f, mediaSize.getY(MediaSize.INCH) - 1.0f, MediaPrintableArea.INCH); }
/** * Prints using custom PrintRequestAttribute values. */ private static void printWithAttributes(PDDocument document) throws IOException, PrinterException { PrinterJob job = PrinterJob.getPrinterJob(); job.setPageable(new PDFPageable(document)); PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet(); attr.add(new PageRanges(1, 1)); // pages 1 to 1 job.print(attr); }
/** * @param context * @return */ protected AttributesImpl createPageMasterAttributes( TableRenderContext context) { AttributesImpl attributes = new AttributesImpl(); attributes.addAttribute("", "master-name", "master-name", "CDATA", "content"); String width = mediaSize.getX(Size2DSyntax.MM) + "mm"; String height = mediaSize.getY(Size2DSyntax.MM) + "mm"; if (orientation.equals(OrientationRequested.LANDSCAPE) || orientation.equals(OrientationRequested.REVERSE_LANDSCAPE)) { attributes.addAttribute("", "page-width", "page-width", "CDATA", height); attributes.addAttribute("", "page-height", "page-height", "CDATA", width); } else if (orientation.equals(OrientationRequested.PORTRAIT) || orientation.equals(OrientationRequested.REVERSE_PORTRAIT)) { attributes.addAttribute("", "page-width", "page-width", "CDATA", width); attributes.addAttribute("", "page-height", "page-height", "CDATA", height); } else { assert false; } attributes.addAttribute("", "margin", "margin", "CDATA", "1cm"); return attributes; }
MediaSize ms=MediaSize.getMediaSizeForName(media); pap.setImageableArea((mediaarea.getX(MediaPrintableArea.INCH)*72), (mediaarea.getY(MediaPrintableArea.INCH)*72), (mediaarea.getWidth(MediaPrintableArea.INCH)*72), (mediaarea.getHeight(MediaPrintableArea.INCH)*72)); if(ms!=null) pap.setSize((ms.getX(MediaSize.INCH)*72), (ms.getY(MediaSize.INCH)*72)); pf.setPaper(pap); if(orientation!=null) if(orientation.getValue()==OrientationRequested.LANDSCAPE.getValue()) else if(orientation.getValue()==OrientationRequested.REVERSE_LANDSCAPE.getValue()) else if(orientation.getValue()==OrientationRequested.PORTRAIT.getValue()) else if(orientation.getValue()==OrientationRequested.REVERSE_PORTRAIT.getValue())
public Dimension getGraphicsSize() { switch(page) { case Paper.USER: return new Dimension( (int)(getPaperWidth()/getXScale()*72), (int)(getPaperHeight()/getYScale()*72) ); default: MediaSize ms = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A4); if (getLandscape()>0) { return new Dimension( (int)(ms.getY(MediaSize.INCH)*72), (int)(ms.getX(MediaSize.INCH)*72) ); } else { return new Dimension( (int)(ms.getX(MediaSize.INCH)*72), (int)(ms.getY(MediaSize.INCH)*72) ); } } } }
MediaSize.getMediaSizeForName(paperSizeName); float[] size = mediaSize.getSize(MediaSize.INCH); printRequestAttributeSet .add(new MediaPrintableArea(0, 0, size[0], size[1], MediaPrintableArea.INCH)); docAttributeSet.add(new MediaPrintableArea(0, 0, size[0], size[1], MediaPrintableArea.INCH)); logger.fine("Paper Size: " + paperSizeName.getName() + " " + size[0] + " x " + size[1]);
printRequestAttributeSet.add(new PageRanges(startPage + 1, endPage + 1)); printRequestAttributeSet.add(new Copies(copies));
public static MediaPrintableArea getPrintableArea(Paper paper, Margin margin) { return new MediaPrintableArea(((Double)margin.getLeft()).floatValue(), ((Double)margin.getTop()).floatValue(), ((Double)Justification.getJustifedWidth(paper, margin)).floatValue(), ((Double)Justification.getJustifiedHeight(paper, margin)).floatValue(), MediaPrintableArea.MM); }
/** * Asks the user for a printer and settings and return the pageformat.<br> * Does not print! Use {@link #printJob(java.awt.print.PrinterJob, javax.print.attribute.PrintRequestAttributeSet, boolean)} to print. * * @param job is the new printerjob * @param jobName is the jobname, null = none * @param aset is an initialized (and possibly empty) attribute set, must not be null, because its returned! * @return the pageformat and possibly updated aset, null if printing aborted */ public static PageFormat print(PrinterJob job, String jobName, PrintRequestAttributeSet aset) { if (aset.get(Media.class) == null) { aset.add(mediaSizeName); } if (aset.get(MediaPrintableArea.class) == null) { aset.add(area); } if (jobName != null) { aset.add(new JobName(jobName, Locale.getDefault())); job.setJobName(jobName); } if (job.printDialog(aset)) { return getPageFormat(aset); } return null; }
/** * Loads/set the media size name derived from the properties manager. * Otherwise a default paper size of NA Letter is returned * * @return a MediaSizeName given the conditions above. */ private MediaSizeName loadDefaultPrinterProperties() { int printMediaUnit = PropertiesManager.checkAndStoreIntegerProperty( propertiesManager, PropertiesManager.PROPERTY_PRINT_MEDIA_SIZE_UNIT, 1000); double printMediaWidth = PropertiesManager.checkAndStoreDoubleProperty( propertiesManager, PropertiesManager.PROPERTY_PRINT_MEDIA_SIZE_WIDTH, 215.9); double printMediaHeight = PropertiesManager.checkAndStoreDoubleProperty( propertiesManager, PropertiesManager.PROPERTY_PRINT_MEDIA_SIZE_HEIGHT, 279.4); // get the closed matching media name. return MediaSize.findMedia((float) printMediaWidth, (float) printMediaHeight, printMediaUnit); }
@Override protected Collection getBasicInstance(Class<?> type) { Helper.assertIsAssignableTo(type, getTypes()); return new JobStateReasons(); }
public TableModel produce( final DataRow parameters, final DataFactoryContext dataFactoryContext ) throws ReportDataFactoryException { final PrintService[] services = PrintServiceLookup.lookupPrintServices( DocFlavor.SERVICE_FORMATTED.PAGEABLE, null ); final TypedTableModel tt = new TypedTableModel(); tt.addColumn( "Printer ID", String.class ); tt.addColumn( "Printer Name", String.class ); for ( int i = 0; i < services.length; i++ ) { final PrintService service = services[i]; final PrinterName displayName = service.getAttribute( PrinterName.class ); if ( displayName != null ) { tt.addRow( new Object[] { service.getName(), displayName.getValue() } ); } else { tt.addRow( new Object[] { service.getName(), service.getName() } ); } } return tt; } }
private void calculateTotalPagesToPrint() { // iterate over page ranges to find out how many pages are to // be printed PageRanges pageRanges = (PageRanges) printRequestAttributeSet.get(PageRanges.class); totalPagesToPrint = 0; // we need to loop over the multiple ranges as commas can be used // to specify more then one range. Make sure the specified pages // fall with in the range allowed by the document. int start, end; for (int[] ranges : pageRanges.getMembers()) { start = ranges[0]; end = ranges[1]; if (start < 1) { start = 1; } if (end > pageTree.getNumberOfPages()) { end = pageTree.getNumberOfPages(); } totalPagesToPrint += end - start + 1; } }
if (attrib != null) { MediaPrintableArea mpa = (MediaPrintableArea)attrib; paper.setImageableArea(mpa.getX(MediaPrintableArea.INCH) * DOTS_PER_INCH, mpa.getY(MediaPrintableArea.INCH) * DOTS_PER_INCH, mpa.getWidth(MediaPrintableArea.INCH) * DOTS_PER_INCH, mpa.getHeight(MediaPrintableArea.INCH) * DOTS_PER_INCH); MediaSize size = MediaSize.getMediaSizeForName((MediaSizeName)attrib); paper.setSize(size.getX(MediaSize.INCH) * DOTS_PER_INCH, size.getY(MediaSize.INCH) * DOTS_PER_INCH);
/** * Utility that tries to save the state of the currently set MediaSize. * The width height and unit values are written to the the propertiesManager. * When the Viewer RI is exited the properites file is wrtten to disk. * * @param printHelper instance of the open documents print helper. */ private void savePrinterProperties(PrintHelper printHelper) { PrintRequestAttributeSet printRequestAttributeSet = printHelper.getPrintRequestAttributeSet(); Object printAttributeSet = printRequestAttributeSet.get(Media.class); if (propertiesManager != null && printAttributeSet instanceof MediaSizeName) { MediaSizeName paper = (MediaSizeName) printAttributeSet; MediaSize mediaSize = MediaSize.getMediaSizeForName(paper); // write out the new page size property values. int printMediaUnit = MediaSize.MM; propertiesManager.set( PropertiesManager.PROPERTY_PRINT_MEDIA_SIZE_UNIT, String.valueOf(printMediaUnit)); double printMediaWidth = mediaSize.getX(printMediaUnit); propertiesManager.set( PropertiesManager.PROPERTY_PRINT_MEDIA_SIZE_WIDTH, String.valueOf(printMediaWidth)); double printMediaHeight = mediaSize.getY(printMediaUnit); propertiesManager.set( PropertiesManager.PROPERTY_PRINT_MEDIA_SIZE_HEIGHT, String.valueOf(printMediaHeight)); } }
attr.add(new PageRanges(1, 1)); // pages 1 to 1
@Override protected Collection getBasicInstance(Class<?> type) { Helper.assertIsAssignableTo(type, getTypes()); return new JobStateReasons(); }
DocPrintJob printJob = printService.createPrintJob(); HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet(); attributes.add(new PageRanges(1)); printJob.print(pdfDoc, attributes);
/** * Prints using custom PrintRequestAttribute values. */ private static void printWithAttributes(PDDocument document) throws IOException, PrinterException { PrinterJob job = PrinterJob.getPrinterJob(); job.setPageable(new PDFPageable(document)); PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet(); attr.add(new PageRanges(1, 1)); // pages 1 to 1 job.print(attr); }