Example usage for com.lowagie.text Rectangle getWidth

List of usage examples for com.lowagie.text Rectangle getWidth

Introduction

In this page you can find the example usage for com.lowagie.text Rectangle getWidth.

Prototype

public float getWidth() 

Source Link

Document

Returns the width of the rectangle.

Usage

From source file:net.sf.sze.service.impl.converter.PdfConverterImpl.java

License:GNU General Public License

/**
 * Orders the pages from an DIN-A4-document on a DIN-A3-document.
 * @param reader reader for DIN-A4-document
 * @param pdfFileA3 file in which the A3-document will be saved.
 * @param pages page-numbers in the order they will placed the paper in the
 * order left, right-side must be a multiple of 4. 0 is interpreted as an
 * empty-page.//from  w ww. jav a2s .  com
 * @throws DocumentException problems in iText.
 * @throws IOException io-problems.
 */
private void createA3Subdocument(PdfReader reader, File pdfFileA3, int... pages)
        throws DocumentException, IOException {
    if (pages.length % 4 != 0) {
        throw new IllegalArgumentException("The number of pages must be a " + "multiple of 4.");
    }

    // we retrieve the size of the first page
    final Rectangle psize = reader.getPageSize(1);
    final float width = psize.getWidth();
    final float leftMargin = 0f;
    final float topMargin = 0f;

    // step 1: creation of a document-object
    final Document document = new Document(PageSize.A3.rotate());
    // step 2: we create a writer that listens to the document
    final PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFileA3));
    writer.setPDFXConformance(PdfWriter.PDFA1B);
    // step 3: we open the document
    document.open();
    addPdfAInfosToDictonary(writer);

    // step 4: we add content
    final PdfContentByte cb = writer.getDirectContent();
    final PdfTemplate[] pdfPages = new PdfTemplate[pages.length];
    for (int i = 0; i < pdfPages.length; i++) {
        final int pageNr = pages[i];
        final PdfTemplate page;
        if (pageNr == EMPTY_PAGE) {
            page = writer.getImportedPage(getEmptyPDFPage(psize), 1);
        } else {
            page = writer.getImportedPage(reader, pageNr);
        }

        if (i % 2 == 0) {
            document.newPage();
            cb.addTemplate(page, 1f, 0f, 0f, 1f, leftMargin, topMargin);
        } else {
            cb.addTemplate(page, 1f, 0f, 0f, 1f, width + leftMargin, topMargin);
        }

    }

    writer.createXmpMetadata();
    // step 5: we close the document
    document.close();
}

From source file:net.sourceforge.fenixedu.util.report.ReportsUtils.java

License:Open Source License

static public byte[] stampPdfAt(byte[] originalPdf, byte[] toStampPdf, int positionX, int positionY) {
    try {/*from   w ww.  j a v  a2 s  .com*/
        PdfReader originalPdfReader = new PdfReader(originalPdf);
        PdfReader toStampPdfReader = new PdfReader(toStampPdf);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        PdfStamper stamper = new PdfStamper(originalPdfReader, stream);

        PdfImportedPage importedPage = stamper.getImportedPage(toStampPdfReader, 1);

        PdfContentByte overContent = stamper.getOverContent(1);

        Rectangle pageSizeWithRotation = originalPdfReader.getPageSizeWithRotation(1);
        Rectangle pageSizeWithRotationStamper = toStampPdfReader.getPageSizeWithRotation(1);

        logger.info(
                String.format("[ %s, %s]", pageSizeWithRotation.getWidth(), pageSizeWithRotation.getHeight()));
        logger.info(String.format("[ %s, %s]", pageSizeWithRotationStamper.getWidth(),
                pageSizeWithRotationStamper.getHeight()));

        Image image = Image.getInstance(importedPage);

        overContent.addImage(image, image.getWidth(), 0f, 0f, image.getHeight(), positionX, positionY);

        stamper.close();

        originalPdfReader.close();
        toStampPdfReader.close();

        return stream.toByteArray();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
}

From source file:org.alchemy.core.AlcSession.java

License:Open Source License

/** Adds a pdfReadPage to an existing pdf file
 * //w  w  w  .j  a  v  a  2  s.  c  o  m
 * @param mainPdf   The main pdf with multiple pages.
 *                  Also used as the destination file.
 * @param tempPdf   The 'new' pdf with one pdfReadPage to be added to the main pdf
 * @return
 */
boolean addPageToPdf(File mainPdf, File tempPdf) {
    try {
        // Destination file created in the temp dir then we will move it
        File dest = new File(DIR_TEMP, "Alchemy.pdf");
        OutputStream output = new FileOutputStream(dest);

        PdfReader reader = new PdfReader(mainPdf.getPath());
        PdfReader newPdf = new PdfReader(tempPdf.getPath());

        // See if the size of the canvas has increased
        // Size of the most recent temp PDF
        com.lowagie.text.Rectangle currentSize = newPdf.getPageSizeWithRotation(1);
        // Size of the session pdf at present
        com.lowagie.text.Rectangle oldSize = reader.getPageSizeWithRotation(1);
        // Sizes to be used from now on
        float pdfWidth = oldSize.getWidth();
        float pdfHeight = oldSize.getHeight();
        if (currentSize.getWidth() > pdfWidth) {
            pdfWidth = currentSize.getWidth();
        }
        if (currentSize.getHeight() > pdfHeight) {
            pdfHeight = currentSize.getHeight();
        }

        // Use the new bigger canvas size if required
        com.lowagie.text.Document document = new com.lowagie.text.Document(
                new com.lowagie.text.Rectangle(pdfWidth, pdfHeight), 0, 0, 0, 0);
        PdfCopy copy = new PdfCopy(document, output);

        // Copy the meta data
        document.addTitle("Alchemy Session");
        document.addAuthor(USER_NAME);
        document.addCreator("Alchemy <http://al.chemy.org>");
        copy.setXmpMetadata(reader.getMetadata());
        document.open();

        // Holds the PDF
        PdfContentByte cb = copy.getDirectContent();

        // Add each page from the main PDF
        for (int i = 0; i < reader.getNumberOfPages();) {
            ++i;
            document.newPage();
            cb.setDefaultColorspace(PdfName.CS, PdfName.DEVICERGB);
            PdfImportedPage page = copy.getImportedPage(reader, i);
            copy.addPage(page);
        }
        // Add the last (new) page
        document.newPage();
        PdfImportedPage lastPage = copy.getImportedPage(newPdf, 1);
        copy.addPage(lastPage);
        output.flush();
        document.close();
        output.close();

        if (dest.exists()) {
            // Save the location of the main pdf
            String mainPdfPath = mainPdf.getPath();
            // Delete the old file
            if (mainPdf.exists()) {
                mainPdf.delete();
            }
            // The final joined up pdf file
            File joinPdf = new File(mainPdfPath);
            // Rename the file
            boolean success = dest.renameTo(joinPdf);
            if (!success) {
                System.err.println("Error moving Pdf");
                return false;
            }

        } else {
            System.err.println("File does not exist?!: " + dest.getAbsolutePath());
            return false;
        }
        return true;

    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:org.caisi.tickler.web.TicklerPrinter.java

License:Open Source License

public void footer() {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();/*from   www.  jav a 2s .  c o m*/

    Date now = new Date();
    String promoTxt = OscarProperties.getInstance().getProperty("FORMS_PROMOTEXT");
    if (promoTxt == null) {
        promoTxt = new String();
    }

    String strFooter = promoTxt + " " + formatter.format(now);

    float textBase = document.bottom();
    cb.beginText();
    cb.setFontAndSize(font.getBaseFont(), FONTSIZE);
    Rectangle page = document.getPageSize();
    float width = page.getWidth();

    cb.showTextAligned(PdfContentByte.ALIGN_CENTER, strFooter, (width / 2.0f), textBase - 20, 0);

    strFooter = "-" + writer.getPageNumber() + "-";
    cb.showTextAligned(PdfContentByte.ALIGN_CENTER, strFooter, (width / 2.0f), textBase - 10, 0);

    cb.endText();
    cb.restoreState();
}

From source file:org.cyberoam.iview.charts.Chart.java

License:Open Source License

/**
 * This Event handler Method adds Header and Footer in PDF File
 *///from  w  w  w. jav a2  s .  c  o m
public void onEndPage(PdfWriter writer, Document document) {
    try {
        if (document.getPageNumber() > 1) {
            String seperator = System.getProperty("file.separator");
            //String path=System.getProperty("catalina.home") +seperator+"webapps" +seperator+"ROOT" + seperator + "images" + seperator;
            String path = InitServlet.contextPath + seperator + "images" + seperator;
            Image imgHead = Image.getInstance(path + "iViewPDFHeader.JPG");
            Image imgFoot = Image.getInstance(path + "iViewPDFFooter.JPG");
            Rectangle page = document.getPageSize();

            PdfPTable head = new PdfPTable(1);
            head.addCell(imgHead);
            head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
            head.writeSelectedRows(0, -1, document.leftMargin() - 10,
                    page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent());

            PdfPTable foot = new PdfPTable(1);
            foot.addCell(imgFoot);
            foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
            foot.writeSelectedRows(0, -1, document.leftMargin() - 10, document.bottomMargin() + 24,
                    writer.getDirectContent());
        }
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:org.cytoscape.io.internal.write.graphics.PDFWriter.java

License:Open Source License

@Override
public void run(TaskMonitor taskMonitor) throws Exception {
    // TODO should be accomplished with renderer properties
    // view.setPrintingTextAsShape(!exportTextAsFont);

    taskMonitor.setProgress(0.0);/*from   ww w  .  j  a va 2  s  .  c  o m*/
    taskMonitor.setStatusMessage("Creating PDF image...");

    logger.debug("PDF Rendering start");
    final Rectangle pageSize = PageSize.LETTER;
    final Document document = new Document(pageSize);

    logger.debug("Document created: " + document);

    final PdfWriter writer = PdfWriter.getInstance(document, stream);
    document.open();

    taskMonitor.setProgress(0.1);

    final PdfContentByte canvas = writer.getDirectContent();
    logger.debug("CB0 created: " + canvas.getClass());

    final float pageWidth = pageSize.getWidth();
    final float pageHeight = pageSize.getHeight();

    logger.debug("Page W: " + pageWidth + " Page H: " + pageHeight);
    final DefaultFontMapper fontMapper = new DefaultFontMapper();
    logger.debug("FontMapper created = " + fontMapper);
    Graphics2D g = null;
    logger.debug("!!!!! Enter block 2");

    engine.getProperties().setProperty("exportTextAsShape", new Boolean(!exportTextAsFont).toString());

    taskMonitor.setProgress(0.2);

    if (exportTextAsFont) {
        g = canvas.createGraphics(pageWidth, pageHeight, new DefaultFontMapper());
    } else {
        g = canvas.createGraphicsShapes(pageWidth, pageHeight);
    }

    taskMonitor.setProgress(0.4);

    logger.debug("##### G2D created: " + g);

    double imageScale = Math.min(pageSize.getWidth() / width, pageSize.getHeight() / height);
    g.scale(imageScale, imageScale);

    logger.debug("##### Start Rendering Phase 2: " + engine.toString());
    engine.printCanvas(g);
    logger.debug("##### Canvas Rendering Done: ");

    taskMonitor.setProgress(0.8);

    g.dispose();
    document.close();
    writer.close();

    stream.close();

    logger.debug("PDF rendering finished.");
    taskMonitor.setProgress(1.0);
}

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  .jav  a2  s  .  c om*/
 * @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  ww. ja  va  2s . c o  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  w  w w. ja v 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.drools.verifier.doc.DroolsDocsComponentFactory.java

License:Apache License

public void onEndPage(PdfWriter writer, Document document) {

    try {//from w w  w.  j a  va 2  s  .c om
        Image image = Image.getInstance(DroolsDocsBuilder.class.getResource("guvnor-webapp.png")); // TODO this image never existed
        image.setAlignment(Image.RIGHT);
        image.scaleAbsolute(100, 30);
        Rectangle page = document.getPageSize();
        PdfPTable head = new PdfPTable(2);

        PdfPCell cell1 = new PdfPCell(image);
        cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell1.setBorder(0);

        head.addCell(cell1);

        PdfPCell cell2 = new PdfPCell(new Phrase(currentDate, DroolsDocsComponentFactory.HEADER_FOOTER_TEXT));
        cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell2.setBorder(0);

        head.addCell(cell2);

        head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        head.writeSelectedRows(0, -1, document.leftMargin(),
                page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent());

    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}