java类java.awt.print.PrinterJob的实例源码

DummyPrintTest.java 文件源码 项目:openjdk-jdk10 阅读 21 收藏 0 点赞 0 评论 0
static void thirdPartyPrintLogic(String printerName) throws Exception {
    PrinterJob printerjob = PrinterJob.getPrinterJob();
    printerjob.setCopies(2);
    printerjob.setJobName("myJobName");
    printerjob.setPrintable(new DummyPrintable());
    for (PrintService printService : PrinterJob.lookupPrintServices()) {
        System.out.println("check printer name of service " + printService);
        if (printerName.equals(printService.getName())) {
            System.out.println("correct printer service do print...");
            printerjob.setPrintService(printService);
            printerjob.print();
            break;
        }
    }
}
PrintPreferences.java 文件源码 项目:incubator-netbeans 阅读 25 收藏 0 点赞 0 评论 0
/**
 * Get an instance of {@link java.awt.print.PageFormat}.
 * @param pj {@link java.awt.print.PrinterJob} which is 
 * associated with the default printer.
 * @return an instance of <code>PageFormat</code> that describes the size and
 * orientation of a page to be printed.
 */
public static PageFormat getPageFormat(PrinterJob pj) {
    PageFormat pageFormat = null;
    pageFormat = pj.defaultPage();
    Paper p = pageFormat.getPaper();
    int pageOrientation = getPreferences().getInt(PROP_PAGE_ORIENTATION, pageFormat.getOrientation());
    double paperWidth = getPreferences().getDouble(PROP_PAGE_WIDTH, p.getWidth());
    double paperHeight = getPreferences().getDouble(PROP_PAGE_HEIGHT, p.getHeight());

    double iaWidth = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_WIDTH, p.getImageableWidth());
    double iaHeight = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_HEIGHT, p.getImageableHeight());
    double iaX = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_X, p.getImageableX());
    double iaY = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_Y, p.getImageableY());

    pageFormat.setOrientation(pageOrientation);
    p.setSize(paperWidth, paperHeight);
    p.setImageableArea(iaX, iaY, iaWidth, iaHeight);
    pageFormat.setPaper(p);
    return pageFormat;
}
Config.java 文件源码 项目:incubator-netbeans 阅读 20 收藏 0 点赞 0 评论 0
public PageFormat getPageFormat() {
    PrinterJob job = PrinterJob.getPrinterJob();

    if (myPageFormat == null) {
        myPageFormat = job.defaultPage();

        // restore
        myPageFormat.setOrientation(round(get(PAGE_ORIENTATION, PageFormat.PORTRAIT)));
        Paper paper = myPageFormat.getPaper();

        if (get(PAPER_WIDTH, null) != null && get(PAPER_HEIGHT, null) != null) {
            paper.setSize(get(PAPER_WIDTH, INCH), get(PAPER_HEIGHT, INCH));
        }
        if (get(AREA_X, null) != null && get(AREA_Y, null) != null && get(AREA_WIDTH, null) != null && get(AREA_HEIGHT, null) != null) {
            paper.setImageableArea(get(AREA_X, INCH), get(AREA_Y, INCH), get(AREA_WIDTH, INCH), get(AREA_HEIGHT, INCH));
        }
        myPageFormat.setPaper(paper);
    }
    return myPageFormat;
}
EditorActions.java 文件源码 项目:Tarski 阅读 27 收藏 0 点赞 0 评论 0
/**
 * 
 */
public void actionPerformed(ActionEvent e)
{
    if (e.getSource() instanceof mxGraphComponent)
    {
        mxGraphComponent graphComponent = (mxGraphComponent) e
                .getSource();
        PrinterJob pj = PrinterJob.getPrinterJob();
        PageFormat format = pj.pageDialog(graphComponent
                .getPageFormat());

        if (format != null)
        {
            graphComponent.setPageFormat(format);
            graphComponent.zoomAndCenter();
        }
    }
}
Printer.java 文件源码 项目:incubator-netbeans 阅读 22 收藏 0 点赞 0 评论 0
void print(List<Paper> papers) {
        PrinterJob job = PrinterJob.getPrinterJob();
        myPapers = papers;
//out("SET PAPER: " + myPapers);

        if (job == null) {
            return;
        }
        job.setPrintable(this, Config.getDefault().getPageFormat());

        try {
            if (job.printDialog()) {
                job.print();
            }
        }
        catch (PrinterException e) {
            printError(i18n(Printer.class, "ERR_Printer_Problem", e.getLocalizedMessage())); // NOI18N
        }
        myPapers = null;
    }
ImageableAreaTest.java 文件源码 项目:openjdk-jdk10 阅读 29 收藏 0 点赞 0 评论 0
private static void printWithJavaPrintDialog() {
    final JTable table = createAuthorTable(50);
    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();
    if (printAccepted) {
        try {
            job.print();
            closeFrame();
        } catch (PrinterException e) {
            throw new RuntimeException(e);
        }
    }
}
PrintSettings.java 文件源码 项目:incubator-netbeans 阅读 25 收藏 0 点赞 0 评论 0
/**
* @return <tt>null</tt> Shows pageDialog, however.
*/
public java.awt.Component getCustomEditor() {
    PageFormat pf = (PageFormat) getValue();
    PrinterJob pj = PrinterJob.getPrinterJob();
    PageFormat npf = pj.pageDialog(pf);

    //setValue(npf);
    ((PrintSettings)PrintSettings.findObject(PrintSettings.class)).setPageFormat((PageFormat) npf.clone());
    pj.cancel();

    return null;
}
AbstractChartPanel.java 文件源码 项目:rapidminer 阅读 31 收藏 0 点赞 0 评论 0
/**
 * Creates a print job for the chart.
 */

@Override
public void createChartPrintJob() {

    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    PageFormat pf2 = job.pageDialog(pf);
    if (pf2 != pf) {
        job.setPrintable(this, pf2);
        if (job.printDialog()) {
            try {
                job.print();
            } catch (PrinterException e) {
                JOptionPane.showMessageDialog(this, e);
            }
        }
    }

}
PrintAttributeUpdateTest.java 文件源码 项目:openjdk-jdk10 阅读 19 收藏 0 点赞 0 评论 0
public static void main(String args[]) throws Exception {
    String[] instructions
            = {
                "Select Pages Range From instead of All in print dialog. ",
                "Then select Print"
            };
    SwingUtilities.invokeAndWait(() -> {
        JOptionPane.showMessageDialog((Component) null,
                instructions, "Instructions",
                JOptionPane.INFORMATION_MESSAGE);
    });
    HashPrintRequestAttributeSet as = new HashPrintRequestAttributeSet();
    PrinterJob j = PrinterJob.getPrinterJob();
    j.setPageable(new PrintAttributeUpdateTest());
    as.add(DialogTypeSelection.NATIVE);
    j.printDialog(as);
    if (as.containsKey(PageRanges.class) == false) {
        throw new RuntimeException("Print Dialog did not update "
                + " attribute set with page range");
    }
    Attribute attrs[] = as.toArray();
    for (int i = 0; i < attrs.length; i++) {
        System.out.println("attr " + attrs[i]);
    }
    j.print(as);
}
ImageableAreaTest.java 文件源码 项目:openjdk-jdk10 阅读 23 收藏 0 点赞 0 评论 0
private static void printOneRowWithJavaPrintDialog() {
    final JTable table = createAuthorTable(1);
    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();
    if (printAccepted) {
        try {
            job.print();
            closeFrame();
        } catch (PrinterException e) {
            throw new RuntimeException(e);
        }
    }
}
PrintDialog.java 文件源码 项目:ramus 阅读 37 收藏 0 点赞 0 评论 0
@Override
protected void onOk() {
    super.onOk();
    final PrinterJob job = framework.getPrinterJob("idef0");
    try {
        printable.setPrintFunctions(panel.getSelectedFunctions());
        job.setPrintable(printable.createPrintable(), printable
                .getPageFormat());
        job.setJobName(printable.getJobName());
        if (job.printDialog()) {

            job.print();
        }
    } catch (final Exception e) {
        e.printStackTrace();
    }
}
FontPanel.java 文件源码 项目:OpenJSharp 阅读 34 收藏 0 点赞 0 评论 0
public void doPrint( int i ) {
    if ( printer == null ) {
        printer = PrinterJob.getPrinterJob();
        page = printer.defaultPage();
    }
    printMode = i;
    printer.setPrintable( fc, page );

    if ( printer.printDialog() ) {
        try {
            printer.print();
        }
        catch ( Exception e ) {
            f2dt.fireChangeStatus( "ERROR: Printing Failed; See Stack Trace", true );
        }
    }
}
WrongPaperPrintingTest.java 文件源码 项目:openjdk-jdk10 阅读 22 收藏 0 点赞 0 评论 0
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);
        }
    }
}
PageDlgApp.java 文件源码 项目:openjdk-jdk10 阅读 19 收藏 0 点赞 0 评论 0
public static void main(String[] args) throws Exception {

        String[] instructions =
         {
             "Visual inspection of the dialog is needed. ",
             "It should be a Printer Job Setup Dialog",
             "Do nothing except Cancel",
             "You must NOT press OK",
         };
        SwingUtilities.invokeAndWait(() -> {
            JOptionPane.showMessageDialog(
                    (Component) null,
                    instructions,
                    "information", JOptionPane.INFORMATION_MESSAGE);
        });
        PrinterJob pj = PrinterJob.getPrinterJob();
        PrintRequestAttributeSet pSet = new HashPrintRequestAttributeSet();
        pSet.add(DialogTypeSelection.NATIVE);
        if ((pj.pageDialog(pSet)) != null) {
            throw
            new RuntimeException("PrinterJob.pageDialog(PrintRequestAttributeSet)"
                        + " does not return null when dialog is cancelled");
        }
    }
BannerTest.java 文件源码 项目:openjdk-jdk10 阅读 19 收藏 0 点赞 0 评论 0
public static void main(String[] args)  throws Exception {
    job = PrinterJob.getPrinterJob();
    PrintService prtSrv = job.getPrintService();
    if (job.getPrintService() == null) {
        System.out.println("No printers. Test cannot continue");
        return;
    }
    if (!prtSrv.isAttributeCategorySupported(JobSheets.class)) {
        return;
    }
    SwingUtilities.invokeAndWait(() -> {
        doTest(BannerTest::printTest);
    });
    mainThread = Thread.currentThread();
    try {
        Thread.sleep(180000);
    } catch (InterruptedException e) {
        if (!testPassed && testGeneratedInterrupt) {
            throw new RuntimeException("Banner page did not print");
        }
    }
    if (!testGeneratedInterrupt) {
        throw new RuntimeException("user has not executed the test");
    }
}
WPrinterJob.java 文件源码 项目:OpenJSharp 阅读 22 收藏 0 点赞 0 评论 0
private void setPrinterNameAttrib(String printerName) {
    PrintService service = this.getPrintService();

    if (printerName == null) {
        return;
    }

    if (service != null && printerName.equals(service.getName())) {
        return;
    } else {
        PrintService []services = PrinterJob.lookupPrintServices();
        for (int i=0; i<services.length; i++) {
            if (printerName.equals(services[i].getName())) {

                try {
                    this.setPrintService(services[i]);
                } catch (PrinterException e) {
                }
                return;
            }
        }
    }
//** END Functions called by native code for querying/updating attributes

}
ChartPanel.java 文件源码 项目:parabuild-ci 阅读 28 收藏 0 点赞 0 评论 0
/**
 * Creates a print job for the chart.
 */
public void createChartPrintJob() {

    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    PageFormat pf2 = job.pageDialog(pf);
    if (pf2 != pf) {
        job.setPrintable(this, pf2);
        if (job.printDialog()) {
            try {
                job.print();
            }
            catch (PrinterException e) {
                JOptionPane.showMessageDialog(this, e);
            }
        }
    }

}
WPrinterJob.java 文件源码 项目:jdk8u-jdk 阅读 19 收藏 0 点赞 0 评论 0
private void setPrinterNameAttrib(String printerName) {
    PrintService service = this.getPrintService();

    if (printerName == null) {
        return;
    }

    if (service != null && printerName.equals(service.getName())) {
        return;
    } else {
        PrintService []services = PrinterJob.lookupPrintServices();
        for (int i=0; i<services.length; i++) {
            if (printerName.equals(services[i].getName())) {

                try {
                    this.setPrintService(services[i]);
                } catch (PrinterException e) {
                }
                return;
            }
        }
    }
//** END Functions called by native code for querying/updating attributes

}
TexturePaintPrintingTest.java 文件源码 项目:jdk8u-jdk 阅读 23 收藏 0 点赞 0 评论 0
private static void printTexture() {
    f = new JFrame("Texture Printing Test");
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    final TexturePaintPrintingTest gpt = new TexturePaintPrintingTest();
    Container c = f.getContentPane();
    c.add(BorderLayout.CENTER, gpt);

    final JButton print = new JButton("Print");
    print.addActionListener(new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPrintable(gpt);
            final boolean doPrint = job.printDialog();
            if (doPrint) {
                try {
                    job.print();
                } catch (PrinterException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }
    });
    c.add(print, BorderLayout.SOUTH);

    f.pack();
    f.setVisible(true);
}
FontPanel.java 文件源码 项目:openjdk-jdk10 阅读 28 收藏 0 点赞 0 评论 0
public void doPrint( int i ) {
    if ( printer == null ) {
        printer = PrinterJob.getPrinterJob();
        page = printer.defaultPage();
    }
    printMode = i;
    printer.setPrintable( fc, page );

    if ( printer.printDialog() ) {
        try {
            printer.print();
        }
        catch ( Exception e ) {
            f2dt.fireChangeStatus( "ERROR: Printing Failed; See Stack Trace", true );
        }
    }
}
PathGraphics.java 文件源码 项目:openjdk-jdk10 阅读 23 收藏 0 点赞 0 评论 0
protected PathGraphics(Graphics2D graphics, PrinterJob printerJob,
                       Printable painter, PageFormat pageFormat,
                       int pageIndex, boolean canRedraw) {
    super(graphics, printerJob);

    mPainter = painter;
    mPageFormat = pageFormat;
    mPageIndex = pageIndex;
    mCanRedraw = canRedraw;
}
PrintDlgPageable.java 文件源码 项目:openjdk-jdk10 阅读 23 收藏 0 点赞 0 评论 0
private static void printTest() {
    PrinterJob pj = PrinterJob.getPrinterJob();
    PageableHandler handler = new PageableHandler();
    pj.setPageable(handler);

    PrintRequestAttributeSet pSet =  new HashPrintRequestAttributeSet();
    pSet.add(DialogTypeSelection.COMMON);
    pj.printDialog(pSet);
}
DlgAttrsBug.java 文件源码 项目:openjdk-jdk10 阅读 19 收藏 0 点赞 0 评论 0
private static void printTest() {
    PrinterJob job = PrinterJob.getPrinterJob();
    if (job.getPrintService() == null) {
        System.out.println("No printers. Test cannot continue");
        return;
    }
    job.setPrintable(new DlgAttrsBug());
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(new Copies(5));
    aset.add(new PageRanges(3,4));
    aset.add(DialogTypeSelection.NATIVE);
    job.printDialog(aset);
}
Win32PrintService.java 文件源码 项目:openjdk-jdk10 阅读 84 收藏 0 点赞 0 评论 0
public PrintRequestAttributeSet
    showDocumentProperties(PrinterJob job,
                           Window owner,
                           PrintService service,
                           PrintRequestAttributeSet aset) {

    if (!(job instanceof WPrinterJob)) {
        return null;
    }
    WPrinterJob wJob = (WPrinterJob)job;
    return wJob.showDocumentProperties(owner, service, aset);
}
UMLDiagramPanel.java 文件源码 项目:onprom 阅读 26 收藏 0 点赞 0 评论 0
public void print() {
  PrinterJob printJob = PrinterJob.getPrinterJob();
  printJob.setPrintable((g, format, page) -> {
    if (page > 0) {
      return Printable.NO_SUCH_PAGE;
    }
    // get the bounds of the component
    Rectangle drawingArea = getDrawingArea();
    double cHeight = drawingArea.getSize().getHeight();
    double cWidth = drawingArea.getSize().getWidth();
    // get the bounds of the printable area
    double pHeight = format.getImageableHeight();
    double pWidth = format.getImageableWidth();
    double pXStart = format.getImageableX();
    double pYStart = format.getImageableY();
    //find ratio
    double xRatio = pWidth / cWidth;
    double yRatio = pHeight / cHeight;
    Graphics2D g2d = (Graphics2D) g;
    //translate and scale accordingly
    g2d.translate(pXStart, pYStart);
    g2d.scale(xRatio, yRatio);
    paintDrawing(g2d, drawingArea.x, drawingArea.y);
    return Printable.PAGE_EXISTS;
  });
  if (printJob.printDialog()) {
    try {
      printJob.print();
    } catch (PrinterException e) {
      UIUtility.error(e.getMessage());
    }
  }
}
PrintPdf.java 文件源码 项目:NICON 阅读 20 收藏 0 点赞 0 评论 0
private void initialize(byte[] pdfContent, String jobName,String nomeImpressora) throws IOException, PrinterException {
    ByteBuffer bb = ByteBuffer.wrap(pdfContent);

    PDFFile pdfFile = new PDFFile(bb);
    PDFPrintPage pages = new PDFPrintPage(pdfFile);

    PrintService[] pservices = PrinterJob.lookupPrintServices();

    System.out.println(pservices.length);
    if (pservices.length > 0) {
        for (PrintService ps : pservices) {
            System.out.println("Impressora Encontrada: " + ps.getName());

            if (ps.getName().contains(nomeImpressora)) {
                System.out.println("Impressora Selecionada: " + nomeImpressora);
                impressora = ps;
                break;
            }
        }
    }
    if (impressora != null) {
        pjob = PrinterJob.getPrinterJob();
        pjob.setPrintService(impressora);

        PageFormat pf = PrinterJob.getPrinterJob().defaultPage();

        pjob.setJobName(jobName);
        Book book = new Book();
        book.append(pages, pf, pdfFile.getNumPages());
        pjob.setPageable(book);


        Paper paper = new Paper();
        paper.setSize(getWidth, getHeight);
        paper.setImageableArea(margin, (int) margin / 4, getWidth - margin * 2, getHeight - margin * 2);
        //paper.setImageableArea(margin,margin, paper.getWidth()-margin*2,paper.getHeight()-margin*2);
        pf.setOrientation(orientacao);
        pf.setPaper(paper);
    }
}
Application.java 文件源码 项目:JDigitalSimulator 阅读 23 收藏 0 点赞 0 评论 0
public void printWorksheet() {
    PrinterJob print = PrinterJob.getPrinterJob();
    print.setPrintable(desktop.getSelectedFrame());
    if(print.printDialog(new HashPrintRequestAttributeSet()))
        try { print.print(); }
    catch(PrinterException e) {}
}
Application.java 文件源码 项目:JDigitalSimulator 阅读 30 收藏 0 点赞 0 评论 0
public void printWorksheetLevel() {
    PrinterJob print = PrinterJob.getPrinterJob();
    PrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
    set.add(OrientationRequested.LANDSCAPE);
    print.setPrintable(oscilloscope);
    if(print.printDialog(set))
        try { print.print(); }
    catch(PrinterException e) {}
}
TestCheckSystemDefaultBannerOption.java 文件源码 项目:openjdk-jdk10 阅读 20 收藏 0 点赞 0 评论 0
public static void main (String[] args) throws Exception {

        job = PrinterJob.getPrinterJob();
        PrintService prtSrv = job.getPrintService();
        if (prtSrv == null) {
            System.out.println("No printers. Test cannot continue");
            return;
        }
        // do not run the test if JobSheet category is not supported
        if (!prtSrv.isAttributeCategorySupported(JobSheets.class)) {
            return;
        }
        // check system default banner option and let user know what to expect
        JobSheets js = (JobSheets)job.getPrintService().
                getDefaultAttributeValue(JobSheets.class);
        if (js != null && js.equals(JobSheets.NONE)) {
            noJobSheet = true;
        }
        SwingUtilities.invokeAndWait(() -> {
            doTest(TestCheckSystemDefaultBannerOption::printTest);
        });
        mainThread = Thread.currentThread();
        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            if (!testPassed && testGeneratedInterrupt) {
                String banner = noJobSheet ? "Banner page" : " No Banner page";
                throw new RuntimeException(banner + " is printed");
            }
        }
        if (!testGeneratedInterrupt) {
            throw new RuntimeException("user has not executed the test");
        }
    }
LinearGradientPrintingTest.java 文件源码 项目:openjdk-jdk10 阅读 24 收藏 0 点赞 0 评论 0
public static void createUI() {
    f = new JFrame("LinearGradient Printing Test");
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    final LinearGradientPrintingTest gpt = new LinearGradientPrintingTest();
    Container c = f.getContentPane();
    c.add(BorderLayout.CENTER, gpt);

    final JButton print = new JButton("Print");
    print.addActionListener(new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPrintable(gpt);
            final boolean doPrint = job.printDialog();
            if (doPrint) {
                try {
                    job.print();
                } catch (PrinterException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }
    });
    c.add(print, BorderLayout.SOUTH);

    f.pack();
    f.setVisible(true);
}


问题


面经


文章

微信
公众号

扫码关注公众号