Search in sources :

Example 21 with PrinterJob

use of java.awt.print.PrinterJob in project pdfbox by apache.

the class Printing method printWithPaper.

/**
 * Prints using a custom page size and custom margins.
 */
private static void printWithPaper(PDDocument document) throws IOException, PrinterException {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPageable(new PDFPageable(document));
    // define custom paper
    Paper paper = new Paper();
    // 1/72 inch
    paper.setSize(306, 396);
    // no margins
    paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
    // custom page format
    PageFormat pageFormat = new PageFormat();
    pageFormat.setPaper(paper);
    // override the page format
    Book book = new Book();
    // append all pages
    book.append(new PDFPrintable(document), pageFormat, document.getNumberOfPages());
    job.setPageable(book);
    job.print();
}
Also used : PDFPageable(org.apache.pdfbox.printing.PDFPageable) PDFPrintable(org.apache.pdfbox.printing.PDFPrintable) PageFormat(java.awt.print.PageFormat) Book(java.awt.print.Book) Paper(java.awt.print.Paper) PrinterJob(java.awt.print.PrinterJob)

Example 22 with PrinterJob

use of java.awt.print.PrinterJob in project pdfbox by apache.

the class Printing method printWithAttributes.

/**
 * 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();
    // pages 1 to 1
    attr.add(new PageRanges(1, 1));
    job.print(attr);
}
Also used : PDFPageable(org.apache.pdfbox.printing.PDFPageable) PageRanges(javax.print.attribute.standard.PageRanges) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrinterJob(java.awt.print.PrinterJob) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 23 with PrinterJob

use of java.awt.print.PrinterJob in project gephi by gephi.

the class SimpleHTMLReport method printButtonActionPerformed.

// </editor-fold>//GEN-END:initComponents
private void printButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_printButtonActionPerformed
    PrinterJob pjob = PrinterJob.getPrinterJob();
    PageFormat pf = pjob.defaultPage();
    pjob.setPrintable(this, pf);
    try {
        if (pjob.printDialog()) {
            pjob.print();
        }
    } catch (PrinterException e) {
        Exceptions.printStackTrace(e);
    }
}
Also used : PageFormat(java.awt.print.PageFormat) PrinterException(java.awt.print.PrinterException) PrinterJob(java.awt.print.PrinterJob)

Example 24 with PrinterJob

use of java.awt.print.PrinterJob in project logisim-evolution by reds-heig.

the class Print method doPrint.

public static void doPrint(Project proj) {
    CircuitJList list = new CircuitJList(proj, true);
    Frame frame = proj.getFrame();
    if (list.getModel().getSize() == 0) {
        JOptionPane.showMessageDialog(proj.getFrame(), Strings.get("printEmptyCircuitsMessage"), Strings.get("printEmptyCircuitsTitle"), JOptionPane.YES_NO_OPTION);
        return;
    }
    ParmsPanel parmsPanel = new ParmsPanel(list);
    int action = JOptionPane.showConfirmDialog(frame, parmsPanel, Strings.get("printParmsTitle"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
    if (action != JOptionPane.OK_OPTION)
        return;
    List<Circuit> circuits = list.getSelectedCircuits();
    if (circuits.isEmpty())
        return;
    PageFormat format = new PageFormat();
    Printable print = new MyPrintable(proj, circuits, parmsPanel.getHeader(), parmsPanel.getRotateToFit(), parmsPanel.getPrinterView());
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(print, format);
    if (job.printDialog() == false)
        return;
    try {
        job.print();
    } catch (PrinterException e) {
        JOptionPane.showMessageDialog(proj.getFrame(), StringUtil.format(Strings.get("printError"), e.toString()), Strings.get("printErrorTitle"), JOptionPane.ERROR_MESSAGE);
    }
}
Also used : PageFormat(java.awt.print.PageFormat) Circuit(com.cburch.logisim.circuit.Circuit) Printable(java.awt.print.Printable) PrinterException(java.awt.print.PrinterException) PrinterJob(java.awt.print.PrinterJob)

Example 25 with PrinterJob

use of java.awt.print.PrinterJob in project CCDD by nasa.

the class CcddJTableHandler method printTable.

/**
 ********************************************************************************************
 * Output the table to the user-selected printer (or file)
 *
 * @param tableName
 *            table name; displayed at the top of each printed page
 *
 * @param fieldHandler
 *            data field handler; null if no data fields are associated with the table
 *
 * @param parent
 *            parent window for this table
 *
 * @param orientation
 *            page orientation; e.g., PageFormat.LANDSCAPE or PageFormat.PORTRAIT
 ********************************************************************************************
 */
protected void printTable(String tableName, CcddFieldHandler fieldHandler, Component parent, int orientation) {
    try {
        GraphicsConfiguration gc;
        // Create a printer job
        PrinterJob printerJob = PrinterJob.getPrinterJob();
        // The native print dialog does not allow simple positioning on the screen relative to
        // another component. However, the ServiceUI.printDialog() method, which calls
        // PrinterJob.printDialog(), does allow setting the dialog's x and y coordinates. The
        // dimensions of the print dialog must be known in order to center it over its parent,
        // but the size is unknown until the dialog is instantiated. Therefore, a dummy dialog
        // is created using the same call within ServiceUI.printDialog() and the dialog's size
        // is taken from it. The dialog's x, y coordinates can then be determined
        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
        DocFlavor flavor = null;
        PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, attributes);
        PrintService defaultService = printerJob.getPrintService();
        // Get the dialog/frame that contains the table
        Component comp = table.getTopLevelAncestor();
        // Create a dummy dialog in order to obtain the print dialog's dimensions
        ServiceDialog dialog = new ServiceDialog(comp.getGraphicsConfiguration(), 0, 0, services, 0, flavor, attributes, (Dialog) null);
        Rectangle newDlgSize = dialog.getBounds();
        dialog.dispose();
        // Get the array of graphics devices (this accounts for multiple screens)
        GraphicsDevice[] gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
        // Check if more than one screen exists
        if (gd.length > 1) {
            // Get the graphics configuration for the screen on which the component resides
            gc = gd[0].getDefaultConfiguration();
        } else // Only one screen is present
        {
            // Get the component's graphics configuration
            gc = comp.getGraphicsConfiguration();
        }
        // Now that the dialog's size is known the print dialog's position can be calculated so
        // as to center it over calling component, adjusting the location so that the dialog
        // appears fully on the screen in which the component resides
        Dimension compSize = comp.getSize();
        Point adjLocation = CcddDialogHandler.adjustDialogLocationForScreen(new Rectangle(comp.getX() + ((compSize.width - newDlgSize.width) / 2), comp.getY() + ((compSize.height - newDlgSize.height) / 2), newDlgSize.width, newDlgSize.height));
        // selected printer
        if (ServiceUI.printDialog(gc, adjLocation.x, adjLocation.y, services, defaultService, flavor, attributes) != null) {
            // Set the page format
            PageFormat pageFormat = new PageFormat();
            pageFormat.setOrientation(orientation);
            // Create a book object for the table and data fields (if applicable)
            Book book = new Book();
            // Determine the number of pages to print the table. The printable object is
            // altered during the page counting process, so it cannot be reused when creating
            // the page wrapper below
            int tblPages = getNumberOfPages(getPrintable(JTable.PrintMode.FIT_WIDTH, new MessageFormat(tableName), new MessageFormat("page {0}")), pageFormat);
            // Add the table to the book object
            book.append(new PageWrapper(getPrintable(JTable.PrintMode.FIT_WIDTH, new MessageFormat(tableName), new MessageFormat("page {0}")), 0), pageFormat, tblPages);
            // Check if data fields are provided
            if (fieldHandler != null && !fieldHandler.getFieldInformation().isEmpty()) {
                String fields = "";
                // Step through each data field
                for (FieldInformation fieldInfo : fieldHandler.getFieldInformation()) {
                    // Append the field name and value to the output string
                    fields += "   " + fieldInfo.getFieldName() + ":  " + fieldInfo.getValue() + "\n";
                }
                // Place the field information into a text area
                JTextArea fldTxtArea = new JTextArea(fields);
                // Get the printable object for the text area
                Printable fldPrintable = fldTxtArea.getPrintable(new MessageFormat("Data Fields for " + tableName), new MessageFormat("page {0}"));
                // Add the fields to the book object
                book.append(new PageWrapper(fldPrintable, tblPages), pageFormat, getNumberOfPages(fldPrintable, pageFormat));
            }
            // Output the book object to the selected printer or file
            printerJob.setPageable(book);
            printerJob.print();
        }
    } catch (PrinterException pe) {
        // Inform the user that printing failed
        new CcddDialogHandler().showMessageDialog(parent, "<html><b>Table '" + tableName + "' printing failed; cause '" + pe.getMessage() + "'", "Print Fail", JOptionPane.WARNING_MESSAGE, DialogOption.OK_OPTION);
    }
}
Also used : ServiceDialog(sun.print.ServiceDialog) JTextArea(javax.swing.JTextArea) Rectangle(java.awt.Rectangle) PrinterException(java.awt.print.PrinterException) GraphicsConfiguration(java.awt.GraphicsConfiguration) PrinterJob(java.awt.print.PrinterJob) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) PrintService(javax.print.PrintService) PageFormat(java.awt.print.PageFormat) GraphicsDevice(java.awt.GraphicsDevice) Book(java.awt.print.Book) Component(java.awt.Component) JComponent(javax.swing.JComponent) JTextComponent(javax.swing.text.JTextComponent) MessageFormat(java.text.MessageFormat) Dimension(java.awt.Dimension) Point(java.awt.Point) TableInsertionPoint(CCDD.CcddConstants.TableInsertionPoint) Point(java.awt.Point) TableInsertionPoint(CCDD.CcddConstants.TableInsertionPoint) Printable(java.awt.print.Printable) DocFlavor(javax.print.DocFlavor) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) FieldInformation(CCDD.CcddClassesDataTable.FieldInformation)

Aggregations

PrinterJob (java.awt.print.PrinterJob)57 PrinterException (java.awt.print.PrinterException)28 PageFormat (java.awt.print.PageFormat)22 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)19 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)17 PDFPageable (org.apache.pdfbox.printing.PDFPageable)9 IOException (java.io.IOException)7 Paper (java.awt.print.Paper)6 Printable (java.awt.print.Printable)5 File (java.io.File)5 Copies (javax.print.attribute.standard.Copies)5 ActionEvent (java.awt.event.ActionEvent)4 PrintService (javax.print.PrintService)4 Container (java.awt.Container)3 Book (java.awt.print.Book)3 JobName (javax.print.attribute.standard.JobName)3 PageRanges (javax.print.attribute.standard.PageRanges)3 AbstractAction (javax.swing.AbstractAction)3 JButton (javax.swing.JButton)3 JFrame (javax.swing.JFrame)3