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:org.efaps.esjp.common.file.FileUtil_Base.java

License:Apache License

/**
 * N up.//from  www .  j  a va 2s  . co m
 *
 * @param _parameter Parameter as passed by the eFaps API
 * @param _file the file
 * @param _fileName the file name
 * @return the file
 * @throws EFapsException on error
 */
public File nUpPdf(final Parameter _parameter, final File _file, final String _fileName) throws EFapsException {
    final File ret = getFile(_fileName, "pdf");
    try {
        final int pow = Integer.parseInt(getProperty(_parameter, "NUpPow", "1"));
        final boolean duplicate = "true".equalsIgnoreCase(getProperty(_parameter, "NUpDuplicate", "true"));
        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));

        final Rectangle pageSize = pdfReader.getPageSize(1);

        final Rectangle newSize = pow % 2 == 0 ? new Rectangle(pageSize.getWidth(), pageSize.getHeight())
                : new Rectangle(pageSize.getHeight(), pageSize.getWidth());

        Rectangle unitSize = new Rectangle(pageSize.getWidth(), pageSize.getHeight());

        for (int i = 0; i < pow; i++) {
            unitSize = new Rectangle(unitSize.getHeight() / 2, unitSize.getWidth());
        }

        final int n = (int) Math.pow(2, pow);
        final int r = (int) Math.pow(2, pow / 2);
        final int c = n / r;

        final Document document = new Document(newSize, 0, 0, 0, 0);

        // Create a writer for the outputstream
        final PdfWriter writer = PdfWriter.getInstance(document, outputStream);
        document.open();
        PdfImportedPage page;
        final PdfContentByte cb = writer.getDirectContent();
        // Create a new page in the target for each source page.
        Rectangle currentSize;
        float offsetX;
        float offsetY;
        float factor;

        final int total = pdfReader.getNumberOfPages();
        for (int i = 0; i < total;) {
            if (i % n == 0) {
                document.newPage();
            }
            currentSize = pdfReader.getPageSize(++i);

            factor = Math.min(unitSize.getWidth() / currentSize.getWidth(),
                    unitSize.getHeight() / currentSize.getHeight());
            offsetX = unitSize.getWidth() * (i % n % c)
                    + (unitSize.getWidth() - currentSize.getWidth() * factor) / 2f;
            offsetY = newSize.getHeight() - (unitSize.getHeight() * (i % n % c) + 1)
                    + (unitSize.getHeight() - currentSize.getHeight() * factor) / 2f;

            page = writer.getImportedPage(pdfReader, i);

            cb.addTemplate(page, factor, 0, 0, factor, offsetX, offsetY);

            if (duplicate) {
                for (int y = i + 1; y <= pow + 1; y++) {
                    factor = Math.min(unitSize.getWidth() / currentSize.getWidth(),
                            unitSize.getHeight() / currentSize.getHeight());
                    offsetX = unitSize.getWidth() * (y % n % c)
                            + (unitSize.getWidth() - currentSize.getWidth() * factor) / 2f;
                    offsetY = newSize.getHeight() - unitSize.getHeight() * (y % n / c + 1)
                            + (unitSize.getHeight() - currentSize.getHeight() * factor) / 2f;
                    cb.addTemplate(page, factor, 0, 0, factor, offsetX, offsetY);
                }
            }
        }
        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;
    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 {/*from w w w.j a  va 2  s.  co  m*/
        return 1.0f;
    }
    return (r.getWidth() / PageSize.A3.getWidth() + r.getHeight() / PageSize.A3.getHeight()) / 2;
}

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

License:Open Source License

@Override
public void calculateSize(PdfContext context) {
    assert (null != getText()) : "getText() must be non-null";

    if (getMaxWidthText() > 0.0) {
        limitTextWidth(context, getMaxWidthText());
    }/* w w  w  .  j a va 2s  .c  o m*/
    Rectangle textSize = context.getTextSize(getText(), getFont());

    float margin = getMargin(); // Get the specified value
    if (margin <= 0.0f) {
        margin = 0.5f * getFont().getSize();
    }
    float width = textSize.getWidth() + 2.0f * margin;
    float height = textSize.getHeight() + 2.0f * margin;

    if (null != getConstraint()) {
        if (getConstraint().getWidth() > 0.0f) {
            width = getConstraint().getWidth();
        }
        if (getConstraint().getHeight() > 0.0f) {
            height = getConstraint().getHeight();
        }
    }
    setBounds(new Rectangle(width, height));
}

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

License:Open Source License

@Override
public void render(PdfContext context) {
    @SuppressWarnings("deprecation")
    float w = getSize().getWidth();
    @SuppressWarnings("deprecation")
    float h = getSize().getHeight();
    Rectangle iconRect = new Rectangle(0, 0, w, h);
    Color fillColor = Color.white;
    Color strokeColor = Color.black;
    float[] dashArray = null;
    if (styleInfo != null) {
        fillColor = context.getColor(styleInfo.getFillColor(), styleInfo.getFillOpacity(), Color.white);
        strokeColor = context.getColor(styleInfo.getStrokeColor(), styleInfo.getStrokeOpacity(), Color.black);
        dashArray = context.getDashArray(styleInfo.getDashArray());
    }/*from   ww w .  j av a 2  s. c  o m*/
    float baseWidth = iconRect.getWidth() / 10;
    // draw symbol
    switch (layerType) {
    case RASTER:
        Image img = context.getImage("/images/layer-raster.png");
        context.drawImage(img, iconRect, null);
        break;
    case POINT:
    case MULTIPOINT:
        drawPoint(context, iconRect, fillColor, strokeColor);
        break;
    case LINESTRING:
    case MULTILINESTRING:
        drawLine(context, iconRect, strokeColor, dashArray);
        break;
    case POLYGON:
    case MULTIPOLYGON:
        context.fillRectangle(iconRect, fillColor);
        context.strokeRectangle(iconRect, strokeColor, baseWidth, dashArray);
        break;
    case GEOMETRY:
        drawPoint(context, iconRect, fillColor, strokeColor);
        drawLine(context, iconRect, strokeColor, dashArray);
        break;
    default:
        log.warn("Cannot draw unknown layerType " + layerType);
    }
}

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

License:Open Source License

private void drawLine(PdfContext context, Rectangle iconRect, Color strokeColor, float[] dashArray) {
    float baseWidth = iconRect.getWidth() / 10;
    context.drawRelativePath(new float[] { 0f, 0.75f, 0.25f, 1f }, new float[] { 0f, 0.25f, 0.75f, 1f },
            iconRect, strokeColor, baseWidth * 2, dashArray);
}

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

License:Open Source License

private void drawPoint(PdfContext context, Rectangle iconRect, Color fillColor, Color strokeColor) {
    float baseWidth = iconRect.getWidth() / 10;
    SymbolInfo symbol = styleInfo.getSymbol();
    if (symbol.getImage() != null) {
        try {/* w  w w  . ja v  a 2 s.co m*/
            Image pointImage = Image.getInstance(symbol.getImage().getHref());
            context.drawImage(pointImage, iconRect, iconRect);
        } catch (Exception ex) { // NOSONAR
            log.error("Not able to create image for POINT Symbol", ex);
        }
    } else if (symbol.getRect() != null) {
        context.fillRectangle(iconRect, fillColor);
        context.strokeRectangle(iconRect, strokeColor, baseWidth / 2);
    } else {
        context.fillEllipse(iconRect, fillColor);
        context.strokeEllipse(iconRect, strokeColor, baseWidth / 2);
    }
}

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

License:Open Source License

@SuppressWarnings("deprecation")
private void generateWarningMessage(PdfContext context) {
    warning = resourceBundle.getString("ErrorRetrievingLegend");

    Rectangle textSize = context.getTextSize(warning, getFont());
    float margin = 0.5f * getFont().getSize();

    setBounds(new Rectangle(textSize.getWidth() + 2.0f * margin, textSize.getHeight() + 2 * margin));
}

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;
    if (landscape) {
        rect = PageSize.getRectangle(size).rotate();
    } else {/*from   www  .  ja  v a 2 s .com*/
        rect = PageSize.getRectangle(size);
    }
    setBounds(rect);
    getConstraint().setWidth(rect.getWidth());
    getConstraint().setHeight(rect.getHeight());
}

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

License:Open Source License

private void layoutChild(PrintComponent<?> child, Rectangle box) {
    LayoutConstraint layoutConstraint = child.getConstraint();
    float bx = box.getLeft();
    float by = box.getBottom();
    float bw = box.getWidth();
    float bh = box.getHeight();
    float cw = child.getBounds().getWidth();
    float ch = child.getBounds().getHeight();
    float marginX = layoutConstraint.getMarginX();
    float marginY = layoutConstraint.getMarginY();
    float absw = layoutConstraint.getWidth();
    float absh = layoutConstraint.getHeight();
    float x = 0;//from ww  w  . j  a  va 2s  .  c o m
    float y = 0;
    float w = cw;
    float h = ch;
    switch (layoutConstraint.getAlignmentX()) {
    case LayoutConstraint.LEFT:
        x = bx + marginX;
        break;
    case LayoutConstraint.CENTER:
        x = bx + (bw - cw) / 2;
        break;
    case LayoutConstraint.RIGHT:
        x = bx + bw - marginX - cw;
        break;
    case LayoutConstraint.JUSTIFIED:
        x = bx + marginX;
        w = bw - 2 * marginX;
        break;
    case LayoutConstraint.ABSOLUTE:
        x = marginX;
        w = absw;
        break;
    default:
        throw new IllegalStateException("Unknown X alignment " + layoutConstraint.getAlignmentX());
    }
    switch (layoutConstraint.getAlignmentY()) {
    case LayoutConstraint.BOTTOM:
        y = by + marginY;
        break;
    case LayoutConstraint.CENTER:
        y = by + (bh - ch) / 2;
        break;
    case LayoutConstraint.TOP:
        y = by + bh - marginY - ch;
        break;
    case LayoutConstraint.JUSTIFIED:
        y = by + marginY;
        h = bh - 2 * marginY;
        break;
    case LayoutConstraint.ABSOLUTE:
        y = marginY;
        h = absh;
        break;
    default:
        throw new IllegalStateException("Unknown Y alignment " + layoutConstraint.getAlignmentY());
    }
    child.setBounds(new Rectangle(x, y, x + w, y + h));
}

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

License:Open Source License

@Override
public void calculateSize(PdfContext context) {
    // clear labels and sizes to make this method idempotent
    tickLabels.clear();//from   w w w.  j  a  va  2 s  .  co  m
    tickSizes.clear();

    // the width must be set !!
    float width = getConstraint().getWidth();

    ClientMapInfo map = configurationService.getMapInfo(getMap().getMapId(), getMap().getApplicationId());
    boolean isEnglishUnits = false;
    boolean isEnglishMiles = false;
    float pxPUnit = getMap().getPpUnit();
    String englishUnit = "ft";
    // Calculate the labels
    if (map != null) {
        UnitType displayUnitType = map.getDisplayUnitType();
        log.debug("calculateSize getMap.getId({}), res {}", getMap().getId(), map);
        Crs crs;
        try {
            crs = geoService.getCrs2(map.getCrs());
            unit = crs.getCoordinateSystem().getAxis(0).getUnit().toString();
            if (UnitType.ENGLISH == displayUnitType || UnitType.ENGLISH_FOOT == displayUnitType) {
                isEnglishUnits = true;
                if (!unit.toUpperCase().contains("F")) {
                    pxPUnit = pxPUnit / FEET_PER_METER;
                }

                // check for yards conversion
                if (UnitType.ENGLISH == displayUnitType && (width / pxPUnit) >= 3
                        && (width / pxPUnit) < FEET_PER_MILE) {
                    pxPUnit = pxPUnit * 3f;
                    englishUnit = "yd";
                }

                // check for miles conversion
                if ((width / pxPUnit) > FEET_PER_MILE) {
                    pxPUnit = pxPUnit * FEET_PER_MILE;
                    englishUnit = "mi";
                    isEnglishMiles = true;
                }
            }
        } catch (Exception e) { // NOSONAR
            throw new IllegalStateException("Could not calculate map unit", e);
        }
    }
    // Calculate width in map units and round.
    float widthInUnits = width / pxPUnit;

    // Calculate ideal tic width in units
    float ticWidthInUnits = widthInUnits / getTicNumber();

    // Calculate minimal tic width in units
    int ticLog;
    float minTicWidthInUnits;
    if (!isEnglishMiles) {
        ticLog = (int) Math.floor(Math.log10(ticWidthInUnits) / 3);
        minTicWidthInUnits = (float) Math.pow(10, ticLog * 3);
    } else {
        ticLog = (int) Math.floor(Math.log10(ticWidthInUnits));
        minTicWidthInUnits = (float) Math.pow(10, ticLog);
    }

    // Find the highest nice number
    float nicestNumber = 0;
    for (float allowTickNumber : ALLOW_TICK_NUMBERS) {
        if (minTicWidthInUnits * allowTickNumber > ticWidthInUnits) {
            break;
        } else {
            nicestNumber = allowTickNumber;
        }
    }
    ticWidthInUnits = minTicWidthInUnits * nicestNumber;
    ticWidth = ticWidthInUnits * pxPUnit;
    ticHeight = ticWidth * 0.3f;
    int ticCount = getTicNumber();

    // font = new Font("Dialog", Font.PLAIN, (int) (0.8 * ticHeight));
    // set the Unit Prefixes

    String units;
    if (!isEnglishUnits && !isEnglishMiles) {
        units = ((ticLog >= -2 && ticLog <= 2) ? UNIT_PREFIXES[ticLog + 2] : "*10^" + (ticLog * 3)) + unit;
    } else {
        units = englishUnit;
    }

    // First pass to check if all fractions can be ignored
    boolean ignoreAllFractions = true;
    for (int i = 0; i <= ticCount; i++) {
        double valueLabel;
        if (!isEnglishUnits && !isEnglishMiles) {
            valueLabel = i * ticWidthInUnits / Math.pow(10, 3 * ticLog);

        } else {
            valueLabel = i * ticWidthInUnits;
        }
        double rounded = Math.round(valueLabel);
        if (Math.abs(valueLabel - (double) rounded) > 0.001) {
            ignoreAllFractions = false;
            break; // Abort
        }
    }

    for (int i = 0; i <= ticCount; i++) {
        String label;

        if (!isEnglishUnits && !isEnglishMiles) {
            label = scaleLabelOnPrint(i * ticWidthInUnits / Math.pow(10, 3 * ticLog), ignoreAllFractions);

        } else {
            label = scaleLabelOnPrint(i * ticWidthInUnits, ignoreAllFractions);

        }

        if (i == ticCount) {
            label += " " + units;
        }
        tickLabels.add(label);
        tickSizes.add(context.getTextSize(label, font));
    }

    // Calculate size
    width = ticWidth * ticCount;
    Rectangle first = context.getTextSize(tickLabels.get(0), font);
    Rectangle last = context.getTextSize(tickLabels.get(ticCount), font);
    width += 0.5 * first.getWidth();
    width += 0.5 * last.getWidth();
    float height = ticHeight;
    height += first.getHeight();
    setBounds(new Rectangle(0, 0, width, height));
}