Search in sources :

Example 21 with PageFormat

use of java.awt.print.PageFormat in project jdk8u_jdk by JetBrains.

the class RasterPrinterJob method defaultPage.

/**
     * 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 (// ie "C"
    !Locale.getDefault().equals(Locale.ENGLISH) && 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;
}
Also used : PageFormat(java.awt.print.PageFormat) MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Media(javax.print.attribute.standard.Media) Paper(java.awt.print.Paper) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService)

Example 22 with PageFormat

use of java.awt.print.PageFormat in project intellij-community by JetBrains.

the class PrintManager method createPageFormat.

private static PageFormat createPageFormat() {
    PrintSettings printSettings = PrintSettings.getInstance();
    PageFormat pageFormat = new PageFormat();
    Paper paper = new Paper();
    String paperSize = printSettings.PAPER_SIZE;
    double paperWidth = PageSizes.getWidth(paperSize) * 72;
    double paperHeight = PageSizes.getHeight(paperSize) * 72;
    double leftMargin = printSettings.LEFT_MARGIN * 72;
    double rightMargin = printSettings.RIGHT_MARGIN * 72;
    double topMargin = printSettings.TOP_MARGIN * 72;
    double bottomMargin = printSettings.BOTTOM_MARGIN * 72;
    paper.setSize(paperWidth, paperHeight);
    if (printSettings.PORTRAIT_LAYOUT) {
        pageFormat.setOrientation(PageFormat.PORTRAIT);
        paperWidth -= leftMargin + rightMargin;
        paperHeight -= topMargin + bottomMargin;
        paper.setImageableArea(leftMargin, topMargin, paperWidth, paperHeight);
    } else {
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
        paperWidth -= topMargin + bottomMargin;
        paperHeight -= leftMargin + rightMargin;
        paper.setImageableArea(rightMargin, topMargin, paperWidth, paperHeight);
    }
    pageFormat.setPaper(paper);
    return pageFormat;
}
Also used : PageFormat(java.awt.print.PageFormat) Paper(java.awt.print.Paper)

Example 23 with PageFormat

use of java.awt.print.PageFormat in project intellij-community by JetBrains.

the class PrintManager method executePrint.

public static void executePrint(DataContext dataContext) {
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null)
        return;
    PsiDirectory psiDirectory = null;
    PsiElement psiElement = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
    if (psiElement instanceof PsiDirectory) {
        psiDirectory = (PsiDirectory) psiElement;
    }
    PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(dataContext);
    String shortFileName = null;
    String directoryName = null;
    if (psiFile != null || psiDirectory != null) {
        if (psiFile != null) {
            shortFileName = psiFile.getName();
            if (psiDirectory == null) {
                psiDirectory = psiFile.getContainingDirectory();
            }
        }
        if (psiDirectory != null) {
            directoryName = psiDirectory.getVirtualFile().getPresentableUrl();
        }
    }
    Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    String text = null;
    if (editor != null) {
        if (editor.getSelectionModel().hasSelection()) {
            text = CodeEditorBundle.message("print.selected.text.radio");
        } else {
            text = psiFile == null ? "Console text" : null;
        }
    }
    List<PsiFile> psiFiles = getSelectedPsiFiles(dataContext);
    PrintDialog printDialog = new PrintDialog(shortFileName, directoryName, text, psiFiles.size(), project);
    printDialog.reset();
    if (!printDialog.showAndGet()) {
        return;
    }
    printDialog.apply();
    final PageFormat pageFormat = createPageFormat();
    final BasePainter painter;
    PrintSettings printSettings = PrintSettings.getInstance();
    if (printSettings.getPrintScope() == PrintSettings.PRINT_FILE && psiFiles.size() > 1) {
        painter = new MultiFilePainter(psiFiles, printSettings.EVEN_NUMBER_OF_PAGES);
    } else if (printSettings.getPrintScope() == PrintSettings.PRINT_DIRECTORY) {
        List<PsiFile> filesList = ContainerUtil.newArrayList();
        boolean isRecursive = printSettings.isIncludeSubdirectories();
        addToPsiFileList(psiDirectory, filesList, isRecursive);
        painter = new MultiFilePainter(filesList, printSettings.EVEN_NUMBER_OF_PAGES);
    } else {
        if (psiFile == null && editor == null)
            return;
        TextPainter textPainter = psiFile != null ? initTextPainter(psiFile) : initTextPainter((DocumentEx) editor.getDocument(), project);
        if (textPainter == null)
            return;
        if (printSettings.getPrintScope() == PrintSettings.PRINT_SELECTED_TEXT && editor != null && editor.getSelectionModel().hasSelection()) {
            textPainter.setSegment(editor.getSelectionModel().getSelectionStart(), editor.getSelectionModel().getSelectionEnd());
        }
        painter = textPainter;
    }
    final PrinterJob printerJob = PrinterJob.getPrinterJob();
    try {
        printerJob.setPrintable(painter, pageFormat);
        if (!printerJob.printDialog()) {
            return;
        }
    } catch (Exception e) {
        LOG.warn(e);
    }
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    ProgressManager.getInstance().run(new Task.Backgroundable(project, CodeEditorBundle.message("print.progress"), true, PerformInBackgroundOption.ALWAYS_BACKGROUND) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            try {
                painter.setProgress(indicator);
                printerJob.print();
            } catch (ProcessCanceledException e) {
                LOG.info("Cancelled");
                printerJob.cancel();
            } catch (PrinterException e) {
                LOG.warn(e);
                String message = ObjectUtils.notNull(e.getMessage(), e.getClass().getName());
                Notifications.Bus.notify(new Notification("Print", CommonBundle.getErrorTitle(), message, NotificationType.ERROR));
            } catch (Exception e) {
                LOG.error(e);
            } finally {
                painter.dispose();
            }
        }
    });
}
Also used : PrinterException(java.awt.print.PrinterException) PrinterException(java.awt.print.PrinterException) Notification(com.intellij.notification.Notification) PrinterJob(java.awt.print.PrinterJob) Project(com.intellij.openapi.project.Project) PageFormat(java.awt.print.PageFormat) ArrayList(java.util.ArrayList) List(java.util.List) Editor(com.intellij.openapi.editor.Editor)

Example 24 with PageFormat

use of java.awt.print.PageFormat in project adempiere by adempiere.

the class CPaper method pageSetupDialog.

//	isLandscape
/*************************************************************************/
/**
	 * 	Show Dialog and Set Paper
	 *  @param job printer job
	 *  @return true if changed.
	 */
public boolean pageSetupDialog(PrinterJob job) {
    PrintRequestAttributeSet prats = getPrintRequestAttributeSet();
    //	Page Dialog
    PageFormat pf = job.pageDialog(prats);
    setPrintRequestAttributeSet(prats);
    return true;
}
Also used : PageFormat(java.awt.print.PageFormat) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 25 with PageFormat

use of java.awt.print.PageFormat in project adempiere by adempiere.

the class PrintUtil method testPS.

//	dump
/*************************************************************************/
/**
	 * 	Test Print Services
	 */
private static void testPS() {
    PrintService ps = getDefaultPrintService();
    ServiceUIFactory factory = ps.getServiceUIFactory();
    System.out.println(factory);
    if (factory != null) {
        System.out.println("Factory");
        JPanel p0 = (JPanel) factory.getUI(ServiceUIFactory.ABOUT_UIROLE, ServiceUIFactory.JDIALOG_UI);
        p0.setVisible(true);
        JPanel p1 = (JPanel) factory.getUI(ServiceUIFactory.ADMIN_UIROLE, ServiceUIFactory.JDIALOG_UI);
        p1.setVisible(true);
        JPanel p2 = (JPanel) factory.getUI(ServiceUIFactory.MAIN_UIROLE, ServiceUIFactory.JDIALOG_UI);
        p2.setVisible(true);
    }
    System.out.println("1----------");
    PrinterJob pj = PrinterJob.getPrinterJob();
    PrintRequestAttributeSet pratts = getDefaultPrintRequestAttributes();
    //	Page Dialog
    PageFormat pf = pj.pageDialog(pratts);
    System.out.println("Pratts Size = " + pratts.size());
    Attribute[] atts = pratts.toArray();
    for (int i = 0; i < atts.length; i++) System.out.println(atts[i].getName() + " = " + atts[i] + " - " + atts[i].getCategory());
    System.out.println("PageFormat h=" + pf.getHeight() + ",w=" + pf.getWidth() + " - x=" + pf.getImageableX() + ",y=" + pf.getImageableY() + " - ih=" + pf.getImageableHeight() + ",iw=" + pf.getImageableWidth() + " - Orient=" + pf.getOrientation());
    ps = pj.getPrintService();
    System.out.println("PrintService = " + ps.getName());
    //	Print Dialog
    System.out.println("2----------");
    pj.printDialog(pratts);
    System.out.println("Pratts Size = " + pratts.size());
    atts = pratts.toArray();
    for (int i = 0; i < atts.length; i++) System.out.println(atts[i].getName() + " = " + atts[i] + " - " + atts[i].getCategory());
    pf = pj.defaultPage();
    System.out.println("PageFormat h=" + pf.getHeight() + ",w=" + pf.getWidth() + " - x=" + pf.getImageableX() + ",y=" + pf.getImageableY() + " - ih=" + pf.getImageableHeight() + ",iw=" + pf.getImageableWidth() + " - Orient=" + pf.getOrientation());
    ps = pj.getPrintService();
    System.out.println("PrintService= " + ps.getName());
    System.out.println("3----------");
    try {
        pj.setPrintService(ps);
    } catch (PrinterException pe) {
        System.out.println(pe);
    }
    pf = pj.validatePage(pf);
    System.out.println("PageFormat h=" + pf.getHeight() + ",w=" + pf.getWidth() + " - x=" + pf.getImageableX() + ",y=" + pf.getImageableY() + " - ih=" + pf.getImageableHeight() + ",iw=" + pf.getImageableWidth() + " - Orient=" + pf.getOrientation());
    ps = pj.getPrintService();
    System.out.println("PrintService= " + ps.getName());
    System.out.println("4----------");
    pj.printDialog();
}
Also used : JPanel(javax.swing.JPanel) PageFormat(java.awt.print.PageFormat) Attribute(javax.print.attribute.Attribute) PrinterException(java.awt.print.PrinterException) PrintService(javax.print.PrintService) ServiceUIFactory(javax.print.ServiceUIFactory) PrinterJob(java.awt.print.PrinterJob) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet)

Aggregations

PageFormat (java.awt.print.PageFormat)32 Paper (java.awt.print.Paper)12 PrinterException (java.awt.print.PrinterException)11 PrinterJob (java.awt.print.PrinterJob)10 PrintService (javax.print.PrintService)9 Printable (java.awt.print.Printable)6 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)6 MediaSize (javax.print.attribute.standard.MediaSize)6 HeadlessException (java.awt.HeadlessException)5 StreamPrintService (javax.print.StreamPrintService)5 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)5 Media (javax.print.attribute.standard.Media)5 Graphics2D (java.awt.Graphics2D)4 BufferedImage (java.awt.image.BufferedImage)4 File (java.io.File)4 MediaSizeName (javax.print.attribute.standard.MediaSizeName)4 OrientationRequested (javax.print.attribute.standard.OrientationRequested)4 AffineTransform (java.awt.geom.AffineTransform)3 Rectangle2D (java.awt.geom.Rectangle2D)3 IOException (java.io.IOException)3