@Override public void run() { PrintDialog dialog = new PrintDialog(getViewSite().getShell(), SWT.NONE); PrinterData data = dialog.open(); if (data == null) { return; } if (data.printToFile) { data.fileName = "print.out"; // TODO you probably want to ask the user for a filename //$NON-NLS-1$ } Printer printer = new Printer(data); try { Rectangle printerBounds = printer.getClientArea(); Rectangle trimBounds = printer.computeTrim( printerBounds.x, printerBounds.y, printerBounds.width, printerBounds.height); System.out.println(printerBounds + ", " + trimBounds); // $NON-NLS-1$ if (printer.startJob(getPartName())) { viewer.print(printer); printer.endJob(); } } catch (Throwable e) { e.printStackTrace(); } finally { printer.dispose(); } }
public void goPrinting(Print grid, boolean isStatement) { PagePrint page = new PagePrint(grid); if (isStatement) { FontData fontData = getSystemFontData(); PageNumberPageDecoration footer = new PageNumberPageDecoration(SWT.CENTER); footer.setFontData(fontData); footer.setFormat( new PageNumberFormat() { @Override public String format(PageNumber pageNumber) { return "" + (pageNumber.getPageNumber() + 1); } }); page.setFooter(footer); } PrintJob job = new PrintJob("Printing...", page); job.setOrientation(PaperClips.ORIENTATION_LANDSCAPE); if (!isStatement) { Margins margins = job.getMargins(); margins.top = margins.top - 30; margins.bottom = margins.bottom - 20; job.setMargins(margins); } PrintDialog dialog = new PrintDialog(Display.getDefault().getActiveShell(), SWT.NONE); PrinterData printerData = dialog.open(); if (printerData != null) PaperClips.print(job, printerData); }
public boolean performPrint(Shell shell, Image image) { GC gc = null; boolean success = true; PrintDialog dialog = new PrintDialog(shell); PrinterData pd = dialog.open(); if (pd != null) { Printer printer = new Printer(pd); Rectangle bounds = image.getBounds(); Rectangle area = printer.getClientArea(); Point dpi = printer.getDPI(); int xScale = dpi.x / 96; int yScale = dpi.y / 96; int width = bounds.width * xScale; int height = bounds.height * yScale; int pWidth = area.width - 5 * dpi.x / 4; int pHeight = area.height - 5 * dpi.x / 4; float factor = Math.min( 1.0F, Math.min((float) pWidth / (float) width, (float) pHeight / (float) height)); int aWidth = (int) (factor * width); int aHeight = (int) (factor * height); int xoff = (area.width - aWidth) / 2; int yoff = (area.height - aHeight) / 2; String jobName = ScreenshotMessages.getString("ScreenshotPlugin.Screenshot"); if (printer.startJob(jobName)) { System.out.println(" Job gestartet\n"); if (printer.startPage()) { System.out.println(" Seite gestartet\n"); System.out.println( image.toString() + ", " + bounds.x + ", " + bounds.y + ", " + bounds.width + ", " + bounds.height + ", " + xoff + ", " + yoff + ", " + aWidth + ", " + aHeight); gc = new GC(printer); System.out.println(gc.toString()); gc.drawImage( image, bounds.x, bounds.y, bounds.width, bounds.height, xoff, yoff, aWidth, aHeight); printer.endPage(); } printer.endJob(); } else { success = false; } if (gc != null) { gc.dispose(); } printer.dispose(); } return success; }
/** * Handle the create button selection event. * * @param event org.eclipse.swt.events.SelectionEvent */ void createButtonSelected(SelectionEvent event) { /* Compute the appropriate dialog style */ int style = getDefaultStyle(); if (okButton.getEnabled() && okButton.getSelection()) style |= SWT.OK; if (cancelButton.getEnabled() && cancelButton.getSelection()) style |= SWT.CANCEL; if (yesButton.getEnabled() && yesButton.getSelection()) style |= SWT.YES; if (noButton.getEnabled() && noButton.getSelection()) style |= SWT.NO; if (retryButton.getEnabled() && retryButton.getSelection()) style |= SWT.RETRY; if (abortButton.getEnabled() && abortButton.getSelection()) style |= SWT.ABORT; if (ignoreButton.getEnabled() && ignoreButton.getSelection()) style |= SWT.IGNORE; if (iconErrorButton.getEnabled() && iconErrorButton.getSelection()) style |= SWT.ICON_ERROR; if (iconInformationButton.getEnabled() && iconInformationButton.getSelection()) style |= SWT.ICON_INFORMATION; if (iconQuestionButton.getEnabled() && iconQuestionButton.getSelection()) style |= SWT.ICON_QUESTION; if (iconWarningButton.getEnabled() && iconWarningButton.getSelection()) style |= SWT.ICON_WARNING; if (iconWorkingButton.getEnabled() && iconWorkingButton.getSelection()) style |= SWT.ICON_WORKING; if (primaryModalButton.getEnabled() && primaryModalButton.getSelection()) style |= SWT.PRIMARY_MODAL; if (applicationModalButton.getEnabled() && applicationModalButton.getSelection()) style |= SWT.APPLICATION_MODAL; if (systemModalButton.getEnabled() && systemModalButton.getSelection()) style |= SWT.SYSTEM_MODAL; if (saveButton.getEnabled() && saveButton.getSelection()) style |= SWT.SAVE; if (openButton.getEnabled() && openButton.getSelection()) style |= SWT.OPEN; if (multiButton.getEnabled() && multiButton.getSelection()) style |= SWT.MULTI; /* Open the appropriate dialog type */ String name = dialogCombo.getText(); if (name.equals(ControlExample.getResourceString("ColorDialog"))) { ColorDialog dialog = new ColorDialog(shell, style); dialog.setRGB(new RGB(100, 100, 100)); dialog.setText(ControlExample.getResourceString("Title")); RGB result = dialog.open(); textWidget.append(ControlExample.getResourceString("ColorDialog") + Text.DELIMITER); textWidget.append( ControlExample.getResourceString("Result", new String[] {"" + result}) + Text.DELIMITER); textWidget.append("getRGB() = " + dialog.getRGB() + Text.DELIMITER + Text.DELIMITER); return; } if (name.equals(ControlExample.getResourceString("DirectoryDialog"))) { DirectoryDialog dialog = new DirectoryDialog(shell, style); dialog.setMessage(ControlExample.getResourceString("Example_string")); dialog.setText(ControlExample.getResourceString("Title")); String result = dialog.open(); textWidget.append(ControlExample.getResourceString("DirectoryDialog") + Text.DELIMITER); textWidget.append( ControlExample.getResourceString("Result", new String[] {"" + result}) + Text.DELIMITER + Text.DELIMITER); return; } if (name.equals(ControlExample.getResourceString("FileDialog"))) { FileDialog dialog = new FileDialog(shell, style); dialog.setFileName(ControlExample.getResourceString("readme_txt")); dialog.setFilterNames(FilterNames); dialog.setFilterExtensions(FilterExtensions); dialog.setText(ControlExample.getResourceString("Title")); String result = dialog.open(); textWidget.append(ControlExample.getResourceString("FileDialog") + Text.DELIMITER); textWidget.append( ControlExample.getResourceString("Result", new String[] {"" + result}) + Text.DELIMITER); textWidget.append("getFileNames() =" + Text.DELIMITER); if ((dialog.getStyle() & SWT.MULTI) != 0) { String[] files = dialog.getFileNames(); for (int i = 0; i < files.length; i++) { textWidget.append("\t" + files[i] + Text.DELIMITER); } } textWidget.append( "getFilterIndex() = " + dialog.getFilterIndex() + Text.DELIMITER + Text.DELIMITER); return; } if (name.equals(ControlExample.getResourceString("FontDialog"))) { FontDialog dialog = new FontDialog(shell, style); dialog.setText(ControlExample.getResourceString("Title")); FontData result = dialog.open(); textWidget.append(ControlExample.getResourceString("FontDialog") + Text.DELIMITER); textWidget.append( ControlExample.getResourceString("Result", new String[] {"" + result}) + Text.DELIMITER); textWidget.append("getFontList() =" + Text.DELIMITER); FontData[] fonts = dialog.getFontList(); if (fonts != null) { for (int i = 0; i < fonts.length; i++) { textWidget.append("\t" + fonts[i] + Text.DELIMITER); } } textWidget.append("getRGB() = " + dialog.getRGB() + Text.DELIMITER + Text.DELIMITER); return; } if (name.equals(ControlExample.getResourceString("PrintDialog"))) { PrintDialog dialog = new PrintDialog(shell, style); dialog.setText(ControlExample.getResourceString("Title")); PrinterData result = dialog.open(); textWidget.append(ControlExample.getResourceString("PrintDialog") + Text.DELIMITER); textWidget.append( ControlExample.getResourceString("Result", new String[] {"" + result}) + Text.DELIMITER); textWidget.append("getScope() = " + dialog.getScope() + Text.DELIMITER); textWidget.append("getStartPage() = " + dialog.getStartPage() + Text.DELIMITER); textWidget.append("getEndPage() = " + dialog.getEndPage() + Text.DELIMITER); textWidget.append( "getPrintToFile() = " + dialog.getPrintToFile() + Text.DELIMITER + Text.DELIMITER); return; } if (name.equals(ControlExample.getResourceString("MessageBox"))) { MessageBox dialog = new MessageBox(shell, style); dialog.setMessage(ControlExample.getResourceString("Example_string")); dialog.setText(ControlExample.getResourceString("Title")); int result = dialog.open(); textWidget.append(ControlExample.getResourceString("MessageBox") + Text.DELIMITER); /* * The resulting integer depends on the original * dialog style. Decode the result and display it. */ switch (result) { case SWT.OK: textWidget.append(ControlExample.getResourceString("Result", new String[] {"SWT.OK"})); break; case SWT.YES: textWidget.append(ControlExample.getResourceString("Result", new String[] {"SWT.YES"})); break; case SWT.NO: textWidget.append(ControlExample.getResourceString("Result", new String[] {"SWT.NO"})); break; case SWT.CANCEL: textWidget.append( ControlExample.getResourceString("Result", new String[] {"SWT.CANCEL"})); break; case SWT.ABORT: textWidget.append(ControlExample.getResourceString("Result", new String[] {"SWT.ABORT"})); break; case SWT.RETRY: textWidget.append(ControlExample.getResourceString("Result", new String[] {"SWT.RETRY"})); break; case SWT.IGNORE: textWidget.append( ControlExample.getResourceString("Result", new String[] {"SWT.IGNORE"})); break; default: textWidget.append(ControlExample.getResourceString("Result", new String[] {"" + result})); break; } textWidget.append(Text.DELIMITER + Text.DELIMITER); } }