Example usage for com.lowagie.text PageSize getRectangle

List of usage examples for com.lowagie.text PageSize getRectangle

Introduction

In this page you can find the example usage for com.lowagie.text PageSize getRectangle.

Prototype

public static Rectangle getRectangle(String name) 

Source Link

Document

This method returns a Rectangle based on a String.

Usage

From source file:org.deegree.igeo.commands.VectorPrintCommand.java

License:Open Source License

/**
 * initializes the {@link Document} required for printing using iText
 * /*from w  w  w  .  j a  v  a 2  s .co m*/
 * @return graphic context ({@link Graphics2D}) of the initialized document
 * @throws FileNotFoundException
 * @throws DocumentException
 */
private Graphics2D initDocument() throws FileNotFoundException, DocumentException {
    String pageFormat = printDefinition.getPageFormat();
    Rectangle pageSize;
    if (pageFormat != null)
        pageSize = PageSize.getRectangle(pageFormat);
    else
        pageSize = new Rectangle(printDefinition.getPageWidth(), printDefinition.getPageHeight());
    LOG.logDebug("page size", pageSize);
    // create (pdf) document with selected pages size; set margin and PDF-version
    document = new Document(pageSize);
    document.setMargins(printDefinition.getAreaLeft(),
            printDefinition.getAreaLeft() + printDefinition.getAreaWidth(), printDefinition.getAreaTop(),
            printDefinition.getAreaTop() + printDefinition.getAreaHeight());

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(printDefinition.getTargetFile()));
    writer.setPdfVersion(printDefinition.getPdfVersion());
    document.open();
    PdfContentByte cb = writer.getDirectContent();

    // create canvas
    Graphics2D g = cb.createGraphics(convert(pageSize.getWidth() / 72 * 25.4),
            convert(pageSize.getHeight() / 72 * 25.4));
    LOG.logDebug("canvas size",
            convert(pageSize.getWidth() / 72 * 25.4) + " " + convert(pageSize.getHeight() / 72 * 25.4));

    // required for correct scaling of raster symbols
    int i1 = convert(pageSize.getHeight() / 72 * 25.4);
    int i2 = convert_(pageSize.getHeight() / 72 * 25.4);
    g.translate(0, i1 - i2);
    g.scale(72d / printDefinition.getDpi(), 72d / printDefinition.getDpi());
    return g;
}

From source file:org.deegree.igeo.views.swing.print.VectorPrintDialog.java

License:Open Source License

private void initGUI() {
    try {//from   w  w  w .j a  va  2 s  .  co  m
        {
            GridBagLayout thisLayout = new GridBagLayout();
            thisLayout.rowWeights = new double[] { 0.0, 0.0 };
            thisLayout.rowHeights = new int[] { 531, 16 };
            thisLayout.columnWeights = new double[] { 0.1, 0.1 };
            thisLayout.columnWidths = new int[] { 7, 20 };
            getContentPane().setLayout(thisLayout);
            {
                pnButtons = new JPanel();
                FlowLayout pnButtonsLayout = new FlowLayout();
                pnButtonsLayout.setAlignment(FlowLayout.LEFT);
                pnButtons.setLayout(pnButtonsLayout);
                getContentPane().add(pnButtons, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
                {
                    btPrint = new JButton(Messages.getMessage(getLocale(), "$MD11785"),
                            IconRegistry.getIcon("accept.png"));
                    pnButtons.add(btPrint);
                    btPrint.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent event) {
                            if (tfOutputFile.getText().trim().length() < 3) {
                                DialogFactory.openWarningDialog(appContainer.getViewPlatform(),
                                        VectorPrintDialog.this, Messages.get("$MD11817"),
                                        Messages.get("$MD11818"));
                            } else {
                                doPrint();
                            }
                        }
                    });
                }
                {
                    btCancel = new JButton(Messages.getMessage(getLocale(), "$MD11786"),
                            IconRegistry.getIcon("cancel.png"));
                    btCancel.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent e) {
                            VectorPrintDialog.this.dispose();
                        }
                    });
                    pnButtons.add(btCancel);

                }
            }
            {
                pnHelp = new JPanel();
                FlowLayout pnHelpLayout = new FlowLayout();
                pnHelpLayout.setAlignment(FlowLayout.RIGHT);
                pnHelp.setLayout(pnHelpLayout);
                getContentPane().add(pnHelp, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
                {
                    btHelp = new JButton(Messages.getMessage(getLocale(), "$MD11787"),
                            IconRegistry.getIcon("help.png"));
                    pnHelp.add(btHelp);
                    btHelp.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            HelpFrame hf = HelpFrame.getInstance(new HelpManager(appContainer));
                            hf.setVisible(true);
                            hf.gotoModule("Print");
                        }
                    });
                }
            }
            {
                pnPrint = new JPanel();
                GridBagLayout pnPrintLayout = new GridBagLayout();
                getContentPane().add(pnPrint, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
                pnPrint.setBorder(
                        BorderFactory.createTitledBorder(Messages.getMessage(getLocale(), "$MD11788")));
                pnPrintLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.1 };
                pnPrintLayout.rowHeights = new int[] { 78, 111, 126, 65, 60, 7 };
                pnPrintLayout.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.1 };
                pnPrintLayout.columnWidths = new int[] { 117, 158, 151, 7 };
                pnPrint.setLayout(pnPrintLayout);
                {
                    pnPreview = new PreviewPanel();
                    pnPreview.setLayout(new BorderLayout());
                    pnPrint.add(pnPreview, new GridBagConstraints(0, 0, 2, 3, 0.0, 0.0,
                            GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
                    pnPreview.setBorder(
                            BorderFactory.createTitledBorder(Messages.getMessage(getLocale(), "$MD11789")));
                    {
                        lbPageSize = new JLabel();
                        pnPreview.add(lbPageSize, BorderLayout.SOUTH);
                        lbPageSize.setText(Messages.getMessage(getLocale(), "$MD11790"));
                    }
                }
                {
                    pnFile = new JPanel();
                    FlowLayout pnFileLayout = new FlowLayout();
                    pnFileLayout.setAlignment(FlowLayout.LEFT);
                    pnFile.setLayout(pnFileLayout);
                    pnPrint.add(pnFile, new GridBagConstraints(0, 5, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                            GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
                    pnFile.setBorder(
                            BorderFactory.createTitledBorder(Messages.getMessage(getLocale(), "$MD11791")));
                    {
                        btLoad = new JButton(Messages.getMessage(getLocale(), "$MD11792"),
                                IconRegistry.getIcon("open.gif"));
                        pnFile.add(btLoad);
                        btLoad.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent event) {
                                doLoadSettings();
                            }
                        });
                    }
                    {
                        btSave = new JButton(Messages.getMessage(getLocale(), "$MD11793"),
                                IconRegistry.getIcon("save.gif"));
                        pnFile.add(btSave);
                        btSave.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent event) {
                                doSaveSettings();
                            }
                        });
                    }
                }
                {
                    pnLayoutPosition = new JPanel();
                    GridBagLayout pnLayoutPositionLayout = new GridBagLayout();
                    pnLayoutPositionLayout.rowWeights = new double[] { 0.0, 0.1, 0.1, 0.1 };
                    pnLayoutPositionLayout.rowHeights = new int[] { 39, 7, 7, 7 };
                    pnLayoutPositionLayout.columnWeights = new double[] { 0.0, 0.0, 0.1 };
                    pnLayoutPositionLayout.columnWidths = new int[] { 69, 95, 7 };
                    pnLayoutPosition.setLayout(pnLayoutPositionLayout);
                    pnPrint.add(pnLayoutPosition, new GridBagConstraints(2, 0, 2, 2, 0.0, 0.0,
                            GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
                    pnLayoutPosition.setBorder(
                            BorderFactory.createTitledBorder(Messages.getMessage(getLocale(), "$MD11848")));
                    {
                        lb1 = new JLabel(Messages.getMessage(getLocale(), "$MD11794"));
                        pnLayoutPosition.add(lb1,
                                new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 0), 0, 0));
                    }
                    {
                        lb2 = new JLabel(Messages.getMessage(getLocale(), "$MD11794"));
                        pnLayoutPosition.add(lb2,
                                new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 0), 0, 0));
                    }
                    {
                        lb3 = new JLabel(Messages.getMessage(getLocale(), "$MD11794"));
                        pnLayoutPosition.add(lb3,
                                new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 0), 0, 0));
                    }
                    {
                        lb4 = new JLabel(Messages.getMessage(getLocale(), "$MD11794"));
                        pnLayoutPosition.add(lb4,
                                new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 0), 0, 0));
                    }
                    {
                        spLeft = new JSpinner(new SpinnerNumberModel(20, 0, 100000, 1));
                        pnLayoutPosition.add(spLeft,
                                new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
                        spLeft.addChangeListener(printSizeListener);
                    }
                    {
                        spTop = new JSpinner(new SpinnerNumberModel(20, 0, 100000, 1));
                        pnLayoutPosition.add(spTop,
                                new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
                        spTop.addChangeListener(printSizeListener);
                    }
                    {
                        spWidth = new JSpinner(new SpinnerNumberModel(150, 10, 100000, 1));
                        pnLayoutPosition.add(spWidth,
                                new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
                        spWidth.addChangeListener(printSizeListener);
                    }
                    {
                        spHeight = new JSpinner(new SpinnerNumberModel(200, 10, 100000, 1));
                        pnLayoutPosition.add(spHeight,
                                new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
                        spHeight.addChangeListener(printSizeListener);
                    }
                    {
                        lbLeft = new JLabel(Messages.getMessage(getLocale(), "$MD11795"));
                        pnLayoutPosition.add(lbLeft,
                                new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 0), 0, 0));
                    }
                    {
                        lbTop = new JLabel(Messages.getMessage(getLocale(), "$MD11796"));
                        pnLayoutPosition.add(lbTop,
                                new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 0), 0, 0));
                    }
                    {
                        lbWidth = new JLabel(Messages.getMessage(getLocale(), "$MD11797"));
                        pnLayoutPosition.add(lbWidth,
                                new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 0), 0, 0));
                    }
                    {
                        lbHeight = new JLabel(Messages.getMessage(getLocale(), "$MD11798"));
                        pnLayoutPosition.add(lbHeight,
                                new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 0), 0, 0));
                    }
                }
                {
                    pnMapCoord = new JPanel();
                    GridBagLayout pnMapCoordLayout = new GridBagLayout();
                    pnPrint.add(pnMapCoord, new GridBagConstraints(2, 2, 2, 1, 0.0, 0.0,
                            GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
                    pnMapCoord.setBorder(
                            BorderFactory.createTitledBorder(Messages.getMessage(getLocale(), "$MD11799")));
                    pnMapCoordLayout.rowWeights = new double[] { 0.1, 0.1 };
                    pnMapCoordLayout.rowHeights = new int[] { 7, 7 };
                    pnMapCoordLayout.columnWeights = new double[] { 0.0, 0.1, 0.1 };
                    pnMapCoordLayout.columnWidths = new int[] { 68, 7, 7 };
                    pnMapCoord.setLayout(pnMapCoordLayout);
                    {
                        lbMapLeft = new JLabel(Messages.getMessage(getLocale(), "$MD11800"));
                        pnMapCoord.add(lbMapLeft,
                                new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 0), 0, 0));
                    }
                    {
                        lbMapBottom = new JLabel(Messages.getMessage(getLocale(), "$MD11801"));
                        pnMapCoord.add(lbMapBottom,
                                new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 0), 0, 0));
                    }
                    {
                        spMapLeft = new JSpinner(new SpinnerNumberModel(0, -9E9, 9E9, 0.5));
                        spMapLeft.setValue(mapModel.getEnvelope().getMin().getX());
                        pnMapCoord.add(spMapLeft,
                                new GridBagConstraints(1, 0, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 9), 0, 0));
                        spMapLeft.addChangeListener(printSizeListener);
                    }
                    {
                        spMapBottom = new JSpinner(new SpinnerNumberModel(0, -9E9, 9E9, 0.5));
                        spMapBottom.setValue(mapModel.getEnvelope().getMin().getY());
                        pnMapCoord.add(spMapBottom,
                                new GridBagConstraints(1, 1, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 9), 0, 0));
                        spMapBottom.addChangeListener(printSizeListener);
                    }
                }
                {
                    pnScale = new JPanel();
                    GridBagLayout pnScaleLayout = new GridBagLayout();
                    pnPrint.add(pnScale, new GridBagConstraints(2, 3, 2, 2, 0.0, 0.0, GridBagConstraints.CENTER,
                            GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
                    pnScale.setBorder(
                            BorderFactory.createTitledBorder(Messages.getMessage(getLocale(), "$MD11802")));
                    pnScaleLayout.rowWeights = new double[] { 0.1, 0.1 };
                    pnScaleLayout.rowHeights = new int[] { 7, 7 };
                    pnScaleLayout.columnWeights = new double[] { 0.1, 0.1 };
                    pnScaleLayout.columnWidths = new int[] { 7, 7 };
                    pnScale.setLayout(pnScaleLayout);
                    {
                        rbConst = new JRadioButton(Messages.getMessage(getLocale(), "$MD11803"));
                        pnScale.add(rbConst,
                                new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 0), 0, 0));
                        rbConst.setSelected(true);
                        bg.add(rbConst);
                    }
                    {
                        rbVariable = new JRadioButton(Messages.getMessage(getLocale(), "$MD11804"));
                        pnScale.add(rbVariable,
                                new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 0), 0, 0));
                        bg.add(rbVariable);
                    }
                    {
                        int[] sc = StringTools.toArrayInt(Messages.getMessage(getLocale(), "$MD11805"), ",; ");
                        ListEntry[] le = new ListEntry[sc.length];
                        for (int i = 0; i < sc.length; i++) {
                            le[i] = new ListEntry("1:" + sc[i], sc[i]);
                        }
                        cbScale = new JComboBox(new DefaultComboBoxModel(le));
                        cbScale.setSelectedIndex(6);
                        pnScale.add(cbScale,
                                new GridBagConstraints(0, 1, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 9), 0, 0));
                        cbScale.setEditable(true);
                        cbScale.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent e) {
                                Object o = cbScale.getSelectedItem();
                                if (o instanceof String) {
                                    try {
                                        // handle use defined scales
                                        ListEntry le = null;
                                        if (((String) o).indexOf(":") > 0) {
                                            String[] t = StringTools.toArray((String) o, ":", false);
                                            le = new ListEntry((String) o, Integer.parseInt(t[1].trim()));
                                        } else {
                                            le = new ListEntry("1:" + o, Integer.parseInt((String) o));
                                        }
                                        ((DefaultComboBoxModel) cbScale.getModel()).addElement(le);
                                        cbScale.setSelectedItem(le);
                                    } catch (Exception ex) {
                                        DialogFactory.openWarningDialog(appContainer.getViewPlatform(),
                                                VectorPrintDialog.this, Messages.get("$MD11821"),
                                                Messages.get("$MD11822"));
                                        return;
                                    }
                                }
                                updatePreview();
                            }
                        });
                    }
                }
                {
                    pnFormat = new JPanel();
                    GridBagLayout pnFormatLayout = new GridBagLayout();
                    pnPrint.add(pnFormat, new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0,
                            GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
                    pnFormat.setBorder(
                            BorderFactory.createTitledBorder(Messages.getMessage(getLocale(), "$MD11806")));
                    pnFormatLayout.rowWeights = new double[] { 0.1 };
                    pnFormatLayout.rowHeights = new int[] { 7 };
                    pnFormatLayout.columnWeights = new double[] { 0.1 };
                    pnFormatLayout.columnWidths = new int[] { 7 };
                    pnFormat.setLayout(pnFormatLayout);
                    {
                        String[] tmp = StringTools.toArray(Messages.getMessage(getLocale(), "$MD11807"), ",;",
                                true);
                        ListEntry[] le = new ListEntry[tmp.length / 2 + 1];
                        le[0] = new ListEntry(Messages.getMessage(getLocale(), "$MD11829"), null);
                        for (int i = 0; i < tmp.length; i += 2) {
                            le[i / 2 + 1] = new ListEntry(tmp[i], tmp[i + 1]);
                        }
                        cbPageFormat = new JComboBox(new DefaultComboBoxModel(le));
                        cbPageFormat.setSelectedIndex(2);

                        pnFormat.add(cbPageFormat,
                                new GridBagConstraints(0, 0, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 9), 0, 0));

                        // width
                        lbPageWidth = new JLabel(Messages.getMessage(getLocale(), "$MD11831"));
                        pnFormat.add(lbPageWidth,
                                new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 15, 0, 0), 0, 0));
                        tfPageWidth = new JSpinner(
                                new SpinnerNumberModel(inMM(PageSize.A4.getWidth()), 0, 6080, 1));
                        pnFormat.add(tfPageWidth,
                                new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 0), 0, 0));

                        tfPageWidth.setEnabled(false);
                        lbPageUnitW = new JLabel(Messages.getMessage(getLocale(), "$MD11832"));
                        pnFormat.add(lbPageUnitW,
                                new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 15), 0, 0));

                        // height
                        lbPageHeight = new JLabel(Messages.getMessage(getLocale(), "$MD11830"));
                        pnFormat.add(lbPageHeight,
                                new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 15, 0, 0), 0, 0));
                        tfPageHeight = new JSpinner(
                                new SpinnerNumberModel(inMM(PageSize.A4.getHeight()), 0, 6080, 1));
                        pnFormat.add(tfPageHeight,
                                new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 0), 0, 0));
                        tfPageHeight.setEnabled(false);
                        lbPageUnitH = new JLabel(Messages.getMessage(getLocale(), "$MD11832"));
                        pnFormat.add(lbPageUnitH,
                                new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 15), 0, 0));

                        cbPageFormat.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent e) {

                                int width;
                                int height;
                                ListEntry le = (ListEntry) ((JComboBox) e.getSource()).getSelectedItem();
                                if (le.value != null) {
                                    String value = (String) le.value;
                                    Rectangle rect = PageSize.getRectangle(value);
                                    width = (int) Math.round(rect.getWidth() / 72 * 25.4);
                                    height = (int) Math.round(rect.getHeight() / 72 * 25.4);
                                    tfPageWidth.setEnabled(false);
                                    tfPageHeight.setEnabled(false);
                                } else {
                                    tfPageWidth.setEnabled(true);
                                    tfPageHeight.setEnabled(true);
                                    width = ((Number) tfPageWidth.getValue()).intValue();
                                    height = ((Number) tfPageHeight.getValue()).intValue();
                                }
                                // if page format has been changed max size of printed map must be changed
                                // for new map size (millimeter) left and top border must be considered to
                                // ensure that printed map does not overlap paper at the right and at the bottom

                                width -= (((Number) spLeft.getValue()).intValue() * 2);
                                height -= (((Number) spTop.getValue()).intValue() * 2);
                                spWidth.setValue(width);
                                spHeight.setValue(height);
                                // preview of printed area must be updated
                                updatePreview();
                            }
                        });
                    }
                }
                {
                    pnDPI = new JPanel();
                    GridBagLayout pnDPILayout = new GridBagLayout();
                    pnPrint.add(pnDPI, new GridBagConstraints(0, 4, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                            GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
                    pnDPI.setBorder(
                            BorderFactory.createTitledBorder(Messages.getMessage(getLocale(), "$MD11808")));
                    pnDPILayout.rowWeights = new double[] { 0.1 };
                    pnDPILayout.rowHeights = new int[] { 7 };
                    pnDPILayout.columnWeights = new double[] { 0.1 };
                    pnDPILayout.columnWidths = new int[] { 7 };
                    pnDPI.setLayout(pnDPILayout);
                    {
                        final DefaultComboBoxModel cbDPIModel = new DefaultComboBoxModel(
                                new Integer[] { 72, 96, 150, 300, 600, 1200, 2400 });
                        cbDPI = new JComboBox();
                        pnDPI.add(cbDPI, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 9), 0, 0));
                        cbDPI.setModel(cbDPIModel);
                        cbDPI.setEditable(true);
                        cbDPI.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent e) {
                                Object o = cbDPI.getSelectedItem();
                                if (o instanceof String) {
                                    try {
                                        Integer i = Integer.parseInt(o.toString());
                                        cbDPIModel.addElement(i);
                                        cbDPI.setSelectedItem(i);
                                    } catch (Exception ex) {
                                        DialogFactory.openWarningDialog(appContainer.getViewPlatform(),
                                                VectorPrintDialog.this, Messages.get("$MD11819"),
                                                Messages.get("$MD11820"));
                                    }
                                }
                            }
                        });
                    }
                }
                {
                    pnOutput = new JPanel();
                    GridBagLayout pnOutputLayout = new GridBagLayout();
                    pnOutputLayout.rowWeights = new double[] { 0.1 };
                    pnOutputLayout.rowHeights = new int[] { 7 };
                    pnOutputLayout.columnWeights = new double[] { 0.1, 0.0, 0.1 };
                    pnOutputLayout.columnWidths = new int[] { 7, 94, 7 };
                    pnOutput.setLayout(pnOutputLayout);
                    pnPrint.add(pnOutput, new GridBagConstraints(2, 5, 2, 1, 0.0, 0.0,
                            GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
                    pnOutput.setBorder(
                            BorderFactory.createTitledBorder(Messages.getMessage(getLocale(), "$MD11809")));
                    {
                        btOutputFile = new JButton(Messages.get("$MD11823"));
                        btOutputFile.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent e) {
                                Preferences prefs = Preferences.userNodeForPackage(VectorPrintDialog.class);
                                File file = GenericFileChooser.showSaveDialog(FILECHOOSERTYPE.externalResource,
                                        appContainer, VectorPrintDialog.this, prefs, "print definition",
                                        IGeoFileFilter.PDF);
                                tfOutputFile.setText(file.getAbsolutePath());
                            }
                        });
                        pnOutput.add(btOutputFile,
                                new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 9), 0, 0));
                    }
                    {
                        tfOutputFile = new JTextField();
                        pnOutput.add(tfOutputFile,
                                new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                                        GridBagConstraints.HORIZONTAL, new Insets(0, 9, 0, 0), 0, 0));
                    }
                }
            }
        }
        pnPreview.setAreaLeft(((Number) spLeft.getValue()).intValue());
        pnPreview.setAreaTop(((Number) spTop.getValue()).intValue());
        pnPreview.setAreaWidth(((Number) spWidth.getValue()).intValue());
        pnPreview.setAreaHeight(((Number) spHeight.getValue()).intValue());
        Rectangle rect;
        ListEntry le = (ListEntry) cbPageFormat.getSelectedItem();
        if (le.value != null) {
            rect = PageSize.getRectangle((String) le.value);
        } else {
            rect = new Rectangle(inPt(((Number) tfPageWidth.getValue()).intValue()),
                    inPt(((Number) tfPageHeight.getValue()).intValue()));
        }
        pnPreview.setPageSize(rect);
        this.setBounds(300, 200, 530, 609);
    } catch (Exception e) {
        e.printStackTrace();
    }

    DefaultMapModule<Container> mapModule = getAssignedMapModule();
    final Container jco = mapModule.getMapContainer();
    jco.addMouseListener(ml);
    jco.addMouseMotionListener(mml);
    isActive = true;
    final MapTool<Container> mapTool = mapModule.getMapTool();
    mapTool.addChangeListener(this);

    addWindowFocusListener(new WindowFocusListener() {

        @Override
        public void windowLostFocus(WindowEvent e) {
        }

        @Override
        public void windowGainedFocus(WindowEvent e) {
            mapTool.resetState();
            // it is required to add the listeners again, cause it seems some other module removes all...
            jco.addMouseListener(ml);
            jco.addMouseMotionListener(mml);
            isActive = true;
        }
    });
}

From source file:org.deegree.igeo.views.swing.print.VectorPrintDialog.java

License:Open Source License

private void updatePreview() {
    if (isVisible()) {
        movePreviewRectangle();/*from ww  w . jav a  2  s  .co m*/

        Rectangle r;
        int pw;
        int ph;
        ListEntry le = (ListEntry) cbPageFormat.getSelectedItem();
        if (le.value != null) {
            tfPageWidth.setEnabled(false);
            tfPageHeight.setEnabled(false);
            r = PageSize.getRectangle((String) le.value);
            pw = (int) Math.round(r.getWidth() / 72 * 25.4);
            ph = (int) Math.round(r.getHeight() / 72 * 25.4);
        } else {
            tfPageWidth.setEnabled(true);
            tfPageHeight.setEnabled(true);
            pw = ((Number) tfPageWidth.getValue()).intValue();
            ph = ((Number) tfPageHeight.getValue()).intValue();
            r = new Rectangle(inPt(pw), inPt(ph));
        }

        lbPageSize.setText(Messages.getMessage(getLocale(), "$MD11816", pw, ph));
        pnPreview.setAreaLeft(((Number) spLeft.getValue()).intValue());
        pnPreview.setAreaTop(((Number) spTop.getValue()).intValue());
        pnPreview.setAreaWidth(((Number) spWidth.getValue()).intValue());
        pnPreview.setAreaHeight(((Number) spHeight.getValue()).intValue());
        pnPreview.setPageSize(r);
        pnPreview.repaint();
    }
}

From source file:org.efaps.esjp.common.file.FileUtil_Base.java

License:Apache License

/**
 * Resize./*w w w.  ja  v  a2s.  c om*/
 *
 * @param _parameter Parameter as passed by the eFaps API
 * @param _file the file
 * @param _fileName the file name
 * @param _pageSize the page size
 * @return the file
 * @throws EFapsException on error
 */
public File resizePdf(final Parameter _parameter, final File _file, final String _fileName,
        final String _pageSize) throws EFapsException {
    final Document document = new Document();

    final File ret = getFile(_fileName, "pdf");
    try {
        final File destFile = new File(_file.getPath() + ".tmp");
        FileUtils.copyFile(_file, destFile);
        final OutputStream outputStream = new FileOutputStream(ret);
        final PdfReader pdfReader = new PdfReader(new FileInputStream(destFile));

        // Create a writer for the outputstream
        final PdfWriter writer = PdfWriter.getInstance(document, outputStream);
        document.open();
        PdfImportedPage page;
        final PdfContentByte cb = writer.getDirectContent();
        int pageOfCurrentReaderPDF = 0;
        // Create a new page in the target for each source page.
        while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
            document.newPage();
            pageOfCurrentReaderPDF++;
            page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
            document.setPageSize(page.getWidth() <= page.getHeight() ? PageSize.getRectangle(_pageSize)
                    : PageSize.getRectangle(_pageSize).rotate());
            final float widthFactor = document.getPageSize().getWidth() / page.getWidth();
            final float heightFactor = document.getPageSize().getHeight() / page.getHeight();
            final float factor = Math.min(widthFactor, heightFactor);
            final float offsetX = (document.getPageSize().getWidth() - page.getWidth() * factor) / 2;
            final float offsetY = (document.getPageSize().getHeight() - page.getHeight() * factor) / 2;
            cb.addTemplate(page, factor, 0, 0, factor, offsetX, offsetY);
        }
        pageOfCurrentReaderPDF = 0;
        outputStream.flush();
        document.close();
        outputStream.close();
    } catch (final FileNotFoundException e) {
        LOG.error("FileNotFoundException", e);
    } catch (final IOException e) {
        LOG.error("IOException", e);
    } catch (final DocumentException e) {
        LOG.error("DocumentException", e);
    }
    return ret;
}

From source file:org.geomajas.plugin.print.command.print.LayoutAsSinglePageDoc.java

License:Open Source License

private static float getPageSizeRelativeToA3(PrintGetTemplateExtRequest request) {
    Rectangle r;//w ww . j  a  va2 s . co  m
    if (request.getPageSize() != null) {
        r = PageSize.getRectangle(request.getPageSize());
    } else if (request.getTemplate().getPage().getLayoutConstraint().getWidth() > 0
            && request.getTemplate().getPage().getLayoutConstraint().getHeight() > 0) {
        float width = request.getTemplate().getPage().getLayoutConstraint().getWidth();
        float height = request.getTemplate().getPage().getLayoutConstraint().getHeight();
        r = new Rectangle(0, 0, width, height);
    } else {
        return 1.0f;
    }
    return (r.getWidth() / PageSize.A3.getWidth() + r.getHeight() / PageSize.A3.getHeight()) / 2;
}

From source file:org.geomajas.plugin.print.component.impl.PageComponentImpl.java

License:Open Source License

public void setSize(String size, boolean landscape) {
    Rectangle rect = null;/*from ww  w.jav a  2 s  .  c o m*/
    if (landscape) {
        rect = PageSize.getRectangle(size).rotate();
    } else {
        rect = PageSize.getRectangle(size);
    }
    setBounds(rect);
    getConstraint().setWidth(rect.getWidth());
    getConstraint().setHeight(rect.getHeight());
}

From source file:org.geomajas.plugin.printing.command.print.PrintGetTemplateCommand.java

License:Open Source License

private float getPageSizeRelativeToA3(PrintGetTemplateRequest request) {
    Rectangle r;//from  www.j  a  v a 2  s  .  c om
    if (request.getPageSize() != null) {
        r = PageSize.getRectangle(request.getPageSize());
    } else {
        float width = request.getTemplate().getPage().getLayoutConstraint().getWidth();
        float height = request.getTemplate().getPage().getLayoutConstraint().getHeight();
        r = new Rectangle(0, 0, width, height);
    }
    return (r.getWidth() / PageSize.A3.getWidth() + r.getHeight() / PageSize.A3.getHeight()) / 2;
}

From source file:org.jaffa.modules.printing.services.PdfHelper.java

License:Open Source License

/**
 * Scale the pages of the input pdfOutput document to the given pageSize.
 * @param pdfOutput The PDF document to rescale, in the form of a ByteArrayOutputStream.
 * @param pageSize The new page size to which to scale to PDF document, e.g. "A4".
 * @param noEnlarge If true, center pages instead of enlarging them.
 *        Use noEnlarge if the new page size is larger than the old one
 *        and the pages should be centered instead of enlarged.
 * @param preserveAspectRatio If true, the aspect ratio will be preserved.
 * @return The PDF document with its pages scaled to the input pageSize.
 *//*  w  w w . j a  va2s .  co m*/
public static byte[] scalePdfPages(byte[] pdfOutput, String pageSize, boolean noEnlarge,
        boolean preserveAspectRatio) throws FormPrintException {
    if (pageSize == null || pdfOutput == null) {
        return pdfOutput;
    }

    // Get the dimensions of the given pageSize in PostScript points.
    // A PostScript point is a 72th of an inch.
    float dimX;
    float dimY;
    Rectangle rectangle;
    try {
        rectangle = PageSize.getRectangle(pageSize);
    } catch (Exception ex) {
        FormPrintException e = new PdfProcessingException(
                "scalePdfPages  - Invalid page size = " + pageSize + "  ");
        log.error(" scalePdfPages  - Invalid page size: " + pageSize + ".  " + ex.getMessage() + ". ");
        throw e;
    }
    if (rectangle != null) {
        dimX = rectangle.getWidth();
        dimY = rectangle.getHeight();
    } else {
        FormPrintException e = new PdfProcessingException("scalePdfPages  - Invalid page size: " + pageSize);
        log.error(" scalePdfPages  - Invalid page size: " + pageSize);
        throw e;
    }
    //Create portrait and landscape rectangles for the given page size.
    Rectangle portraitPageSize;
    Rectangle landscapePageSize;
    if (dimY > dimX) {
        portraitPageSize = new Rectangle(dimX, dimY);
        landscapePageSize = new Rectangle(dimY, dimX);
    } else {
        portraitPageSize = new Rectangle(dimY, dimX);
        landscapePageSize = new Rectangle(dimX, dimY);
    }

    // Remove the document rotation before resizing the document.
    byte[] output = removeRotation(pdfOutput);
    PdfReader currentReader = null;
    try {
        currentReader = new PdfReader(output);
    } catch (IOException ex) {
        FormPrintException e = new PdfProcessingException("scalePdfPages  - Failed to create a PDF Reader");
        log.error(" scalePdfPages  - Failed to create a PDF Reader ");
        throw e;
    }

    OutputStream baos = new ByteArrayOutputStream();
    Rectangle newSize = new Rectangle(dimX, dimY);
    Document document = new Document(newSize, 0, 0, 0, 0);
    PdfWriter writer = null;
    try {
        writer = PdfWriter.getInstance(document, baos);
    } catch (DocumentException ex) {
        FormPrintException e = new PdfProcessingException("scalePdfPages  - Failed to create a PDF Writer");
        log.error(" scalePdfPages  - Failed to create a PDF Writer ");
        throw e;
    }
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    PdfImportedPage page;
    float offsetX, offsetY;
    for (int i = 1; i <= currentReader.getNumberOfPages(); i++) {
        Rectangle currentSize = currentReader.getPageSizeWithRotation(i);
        if (currentReader.getPageRotation(i) != 0) {
            FormPrintException e = new PdfProcessingException("Page Rotation, "
                    + currentReader.getPageRotation(i) + ", must be removed to re-scale the form.");
            log.error(" Page Rotation, " + currentReader.getPageRotation(i)
                    + ", must be removed to re-scale the form. ");
            throw e;
        }
        //Reset the page size for each page because there may be a mix of sizes in the document.
        float currentWidth = currentSize.getWidth();
        float currentHeight = currentSize.getHeight();
        if (currentWidth > currentHeight) {
            newSize = landscapePageSize;
        } else {
            newSize = portraitPageSize;
        }
        document.setPageSize(newSize);
        document.newPage();
        float factorX = newSize.getWidth() / currentSize.getWidth();
        float factorY = newSize.getHeight() / currentSize.getHeight();
        // Use noEnlarge if the new page size is larger than the old one
        // and the pages should be centered instead of enlarged.
        if (noEnlarge) {
            if (factorX > 1) {
                factorX = 1;
            }
            if (factorY > 1) {
                factorY = 1;
            }
        }
        if (preserveAspectRatio) {
            factorX = Math.min(factorX, factorY);
            factorY = factorX;
        }
        offsetX = (newSize.getWidth() - (currentSize.getWidth() * factorX)) / 2f;
        offsetY = (newSize.getHeight() - (currentSize.getHeight() * factorY)) / 2f;
        page = writer.getImportedPage(currentReader, i);
        cb.addTemplate(page, factorX, 0, 0, factorY, offsetX, offsetY);
    }
    document.close();
    return ((ByteArrayOutputStream) baos).toByteArray();
}

From source file:org.mapfish.print.config.layout.Page.java

License:Open Source License

public Rectangle getPageSizeRect(RenderingContext context, PJsonObject params) {
    final Rectangle result = PageSize.getRectangle(getPageSize(context, params));
    if (landscape) {
        return result.rotate();
    } else {/*from   ww w .j  av  a2 s  .c  o m*/
        return result;
    }
}

From source file:org.mapfish.print.config.layout.Page.java

License:Open Source License

public void setPageSize(String pageSize) {
    this.pageSize = pageSize;
    try {/*  w ww . ja v a 2  s.  c om*/
        PageSize.getRectangle(pageSize);
    } catch (RuntimeException e) {
        throw new InvalidValueException("pageSize", pageSize);
    }
}