Search in sources :

Example 1 with Book

use of java.awt.print.Book 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 2 with Book

use of java.awt.print.Book 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)

Example 3 with Book

use of java.awt.print.Book in project megameklab by MegaMek.

the class UnitPrintManager method printAllUnits.

public static boolean printAllUnits(Vector<Entity> loadedUnits, boolean singlePrint) {
    Book book = new Book();
    List<Infantry> infList = new ArrayList<>();
    List<BattleArmor> baList = new ArrayList<>();
    List<Protomech> protoList = new ArrayList<>();
    List<Entity> unprintable = new ArrayList<>();
    HashPrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(MediaSizeName.NA_LETTER);
    aset.add(new MediaPrintableArea(0, 0, 8.5f, 11, MediaPrintableArea.INCH));
    PrinterJob masterPrintJob = PrinterJob.getPrinterJob();
    if (!masterPrintJob.printDialog(aset)) {
        return true;
    }
    PageFormat pageFormat = new PageFormat();
    pageFormat = masterPrintJob.getPageFormat(null);
    Paper p = pageFormat.getPaper();
    p.setImageableArea(0, 0, p.getWidth(), p.getHeight());
    pageFormat.setPaper(p);
    Tank tank1 = null;
    Tank wige1 = null;
    Tank dualTurret1 = null;
    for (Entity unit : loadedUnits) {
        if (unit instanceof Mech) {
            UnitUtil.removeOneShotAmmo(unit);
            UnitUtil.expandUnitMounts((Mech) unit);
            book.append(new PrintMech((Mech) unit, book.getNumberOfPages()), pageFormat);
        } else if ((unit instanceof LargeSupportTank) || ((unit instanceof Tank) && (unit.getMovementMode() != EntityMovementMode.VTOL) && ((Tank) unit).isSuperHeavy())) {
            book.append(new PrintLargeSupportVehicle((Tank) unit), pageFormat);
        } else if (unit instanceof VTOL) {
            book.append(new PrintVTOL((VTOL) unit), pageFormat);
        } else if (unit.getMovementMode() == EntityMovementMode.WIGE) {
            if (singlePrint) {
                book.append(new PrintVehicle((Tank) unit, null), pageFormat);
            } else if (null != wige1) {
                book.append(new PrintVehicle(wige1, (Tank) unit), pageFormat);
                wige1 = null;
            } else {
                wige1 = (Tank) unit;
            }
        } else if ((unit instanceof Tank) && ((unit.getMovementMode() == EntityMovementMode.NAVAL) || (unit.getMovementMode() == EntityMovementMode.SUBMARINE) || (unit.getMovementMode() == EntityMovementMode.HYDROFOIL))) {
            unprintable.add(unit);
        // book.append(new PrintNavalVehicle((Tank) unit), pageFormat);
        } else if (unit instanceof Tank) {
            if (!((Tank) unit).hasNoDualTurret()) {
                if (singlePrint) {
                    book.append(new PrintDualTurretVehicle((Tank) unit, null), pageFormat);
                } else if (null != dualTurret1) {
                    book.append(new PrintDualTurretVehicle(dualTurret1, (Tank) unit), pageFormat);
                    dualTurret1 = null;
                } else {
                    dualTurret1 = (Tank) unit;
                }
            } else {
                if (singlePrint) {
                    book.append(new PrintVehicle((Tank) unit, null), pageFormat);
                } else if (null != tank1) {
                    book.append(new PrintVehicle(tank1, (Tank) unit), pageFormat);
                    tank1 = null;
                } else {
                    tank1 = (Tank) unit;
                }
            }
        } else if (unit.hasETypeFlag(Entity.ETYPE_AERO) && !unit.hasETypeFlag(Entity.ETYPE_JUMPSHIP)) {
            if (unit instanceof Dropship) {
                if (unit.getMovementMode() == EntityMovementMode.AERODYNE) {
                    book.append(new PrintAerodyne((Dropship) unit), pageFormat);
                } else {
                    book.append(new PrintSpheroid((Dropship) unit), pageFormat);
                }
            } else if (unit instanceof FixedWingSupport) {
                book.append(new PrintFixedWingSupport((FixedWingSupport) unit), pageFormat);
            } else if (unit instanceof ConvFighter) {
                book.append(new PrintConventionalFighter((ConvFighter) unit), pageFormat);
            } else if (unit instanceof SmallCraft) {
                if (unit.getMovementMode() == EntityMovementMode.AERODYNE) {
                    book.append(new PrintSmallCraftAerodyne((SmallCraft) unit), pageFormat);
                } else {
                    book.append(new PrintSmallCraftSpheroid((SmallCraft) unit), pageFormat);
                }
            } else {
                book.append(new PrintAero((Aero) unit), pageFormat);
            }
        } else if (unit instanceof BattleArmor) {
            baList.add((BattleArmor) unit);
            if (singlePrint || baList.size() > 4) {
                book.append(new PrintBattleArmor(baList), pageFormat);
                baList = new ArrayList<>();
            }
        } else if (unit instanceof Infantry) {
            infList.add((Infantry) unit);
            if (singlePrint || infList.size() > 3) {
                book.append(new PrintInfantry(infList), pageFormat);
                infList = new ArrayList<>();
            }
        } else if (unit instanceof Protomech) {
            protoList.add((Protomech) unit);
            if (singlePrint || protoList.size() > 4) {
                book.append(new PrintProtomech(protoList), pageFormat);
                protoList = new ArrayList<>();
            }
        } else {
            // TODO: show a message dialog that lists the unprintable units
            unprintable.add(unit);
        }
    }
    if (unprintable.size() > 0) {
        JOptionPane.showMessageDialog(null, "Printing is not currently supported for the following units:\n" + unprintable.stream().map(en -> en.getChassis() + " " + en.getModel()).collect(Collectors.joining("\n")));
    }
    if (null != wige1) {
        book.append(new PrintVehicle(wige1, null), pageFormat);
    }
    if (null != tank1) {
        book.append(new PrintVehicle(tank1, null), pageFormat);
    }
    if (null != dualTurret1) {
        book.append(new PrintDualTurretVehicle(dualTurret1, null), pageFormat);
    }
    if (baList.size() > 0) {
        book.append(new PrintBattleArmor(baList), pageFormat);
    }
    if (infList.size() > 0) {
        book.append(new PrintInfantry(infList), pageFormat);
    }
    if (protoList.size() > 0) {
        book.append(new PrintProtomech(protoList), pageFormat);
    }
    masterPrintJob.setPageable(book);
    if (loadedUnits.size() > 1) {
        masterPrintJob.setJobName(loadedUnits.get(0).getShortNameRaw() + " etc");
    } else if (loadedUnits.size() > 0) {
        masterPrintJob.setJobName(loadedUnits.get(0).getShortNameRaw());
    }
    PrintTask task = new PrintTask(masterPrintJob, aset);
    task.execute();
    return true;
}
Also used : PrintAero(megameklab.com.ui.Aero.Printing.PrintAero) Dropship(megamek.common.Dropship) Aero(megamek.common.Aero) PrintLargeSupportVehicle(megameklab.com.ui.Vehicle.Printing.PrintLargeSupportVehicle) PrintSmallCraftSpheroid(megameklab.com.ui.Aero.Printing.PrintSmallCraftSpheroid) PrintBattleArmor(megameklab.com.ui.BattleArmor.Printing.PrintBattleArmor) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrinterJob(java.awt.print.PrinterJob) PrintTask(megameklab.com.printing.PrintTask) Vector(java.util.Vector) BattleArmor(megamek.common.BattleArmor) PrintMech(megameklab.com.printing.PrintMech) PrintInfantry(megameklab.com.ui.Infantry.Printing.PrintInfantry) UnitLoadingDialog(megamek.client.ui.swing.UnitLoadingDialog) EntityListFile(megamek.common.EntityListFile) SmallCraft(megamek.common.SmallCraft) PageFormat(java.awt.print.PageFormat) JFileChooser(javax.swing.JFileChooser) JFrame(javax.swing.JFrame) Infantry(megamek.common.Infantry) KeyStroke(javax.swing.KeyStroke) MechFileParser(megamek.common.MechFileParser) Frame(java.awt.Frame) PrintFixedWingSupport(megameklab.com.ui.Aero.Printing.PrintFixedWingSupport) Mech(megamek.common.Mech) VTOL(megamek.common.VTOL) JMenu(javax.swing.JMenu) KeyEvent(java.awt.event.KeyEvent) PrintAerodyne(megameklab.com.ui.Dropship.Printing.PrintAerodyne) Collectors(java.util.stream.Collectors) FixedWingSupport(megamek.common.FixedWingSupport) Book(java.awt.print.Book) Protomech(megamek.common.Protomech) PrintVehicle(megameklab.com.ui.Vehicle.Printing.PrintVehicle) List(java.util.List) LargeSupportTank(megamek.common.LargeSupportTank) Tank(megamek.common.Tank) Entity(megamek.common.Entity) MediaSizeName(javax.print.attribute.standard.MediaSizeName) ActionListener(java.awt.event.ActionListener) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) PrintSpheroid(megameklab.com.ui.Dropship.Printing.PrintSpheroid) Paper(java.awt.print.Paper) PrintDualTurretVehicle(megameklab.com.ui.Vehicle.Printing.PrintDualTurretVehicle) ArrayList(java.util.ArrayList) JMenuItem(javax.swing.JMenuItem) UnitPrintQueueDialog(megameklab.com.ui.dialog.UnitPrintQueueDialog) PrintSmallCraftAerodyne(megameklab.com.ui.Aero.Printing.PrintSmallCraftAerodyne) UnitSelectorDialog(megamek.client.ui.swing.UnitSelectorDialog) PrintConventionalFighter(megameklab.com.ui.Aero.Printing.PrintConventionalFighter) ConvFighter(megamek.common.ConvFighter) PrintVTOL(megameklab.com.ui.Vehicle.Printing.PrintVTOL) EntityMovementMode(megamek.common.EntityMovementMode) JOptionPane(javax.swing.JOptionPane) ActionEvent(java.awt.event.ActionEvent) MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) File(java.io.File) PrintProtomech(megameklab.com.ui.ProtoMek.Printing.PrintProtomech) Entity(megamek.common.Entity) PrintSmallCraftSpheroid(megameklab.com.ui.Aero.Printing.PrintSmallCraftSpheroid) PrintAero(megameklab.com.ui.Aero.Printing.PrintAero) PrintFixedWingSupport(megameklab.com.ui.Aero.Printing.PrintFixedWingSupport) PrintDualTurretVehicle(megameklab.com.ui.Vehicle.Printing.PrintDualTurretVehicle) ArrayList(java.util.ArrayList) PrinterJob(java.awt.print.PrinterJob) MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) PageFormat(java.awt.print.PageFormat) Book(java.awt.print.Book) PrintProtomech(megameklab.com.ui.ProtoMek.Printing.PrintProtomech) PrintVehicle(megameklab.com.ui.Vehicle.Printing.PrintVehicle) PrintAerodyne(megameklab.com.ui.Dropship.Printing.PrintAerodyne) PrintBattleArmor(megameklab.com.ui.BattleArmor.Printing.PrintBattleArmor) BattleArmor(megamek.common.BattleArmor) PrintFixedWingSupport(megameklab.com.ui.Aero.Printing.PrintFixedWingSupport) FixedWingSupport(megamek.common.FixedWingSupport) PrintTask(megameklab.com.printing.PrintTask) PrintLargeSupportVehicle(megameklab.com.ui.Vehicle.Printing.PrintLargeSupportVehicle) LargeSupportTank(megamek.common.LargeSupportTank) Dropship(megamek.common.Dropship) ConvFighter(megamek.common.ConvFighter) PrintSmallCraftAerodyne(megameklab.com.ui.Aero.Printing.PrintSmallCraftAerodyne) LargeSupportTank(megamek.common.LargeSupportTank) Tank(megamek.common.Tank) SmallCraft(megamek.common.SmallCraft) PrintInfantry(megameklab.com.ui.Infantry.Printing.PrintInfantry) Infantry(megamek.common.Infantry) PrintInfantry(megameklab.com.ui.Infantry.Printing.PrintInfantry) VTOL(megamek.common.VTOL) PrintVTOL(megameklab.com.ui.Vehicle.Printing.PrintVTOL) Protomech(megamek.common.Protomech) PrintProtomech(megameklab.com.ui.ProtoMek.Printing.PrintProtomech) Paper(java.awt.print.Paper) PrintMech(megameklab.com.printing.PrintMech) Mech(megamek.common.Mech) PrintMech(megameklab.com.printing.PrintMech) PrintSpheroid(megameklab.com.ui.Dropship.Printing.PrintSpheroid) PrintBattleArmor(megameklab.com.ui.BattleArmor.Printing.PrintBattleArmor) PrintConventionalFighter(megameklab.com.ui.Aero.Printing.PrintConventionalFighter) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintVTOL(megameklab.com.ui.Vehicle.Printing.PrintVTOL)

Aggregations

Book (java.awt.print.Book)3 PageFormat (java.awt.print.PageFormat)3 PrinterJob (java.awt.print.PrinterJob)3 Paper (java.awt.print.Paper)2 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)2 FieldInformation (CCDD.CcddClassesDataTable.FieldInformation)1 TableInsertionPoint (CCDD.CcddConstants.TableInsertionPoint)1 Component (java.awt.Component)1 Dimension (java.awt.Dimension)1 Frame (java.awt.Frame)1 GraphicsConfiguration (java.awt.GraphicsConfiguration)1 GraphicsDevice (java.awt.GraphicsDevice)1 Point (java.awt.Point)1 Rectangle (java.awt.Rectangle)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 KeyEvent (java.awt.event.KeyEvent)1 Printable (java.awt.print.Printable)1 PrinterException (java.awt.print.PrinterException)1 File (java.io.File)1