Example usage for com.itextpdf.text Rectangle setLeft

List of usage examples for com.itextpdf.text Rectangle setLeft

Introduction

In this page you can find the example usage for com.itextpdf.text Rectangle setLeft.

Prototype

public void setLeft(final float llx) 

Source Link

Document

Sets the lower left x-coordinate.

Usage

From source file:at.laborg.briss.CropManager.java

License:Open Source License

private static Rectangle calculateScaledRectangle(List<Rectangle> boxes, Float[] ratios, int rotation) {
    if (ratios == null || boxes.size() == 0)
        return null;
    Rectangle smallestBox = null;
    // find smallest box
    float smallestSquare = Float.MAX_VALUE;
    for (Rectangle box : boxes) {
        if (box != null) {
            if (smallestBox == null) {
                smallestBox = box;/*from ww w .  java  2  s . c  om*/
            }
            if (smallestSquare > box.getWidth() * box.getHeight()) {
                // set new smallest box
                smallestSquare = box.getWidth() * box.getHeight();
                smallestBox = box;
            }
        }
    }
    if (smallestBox == null)
        return null; // no useable box was found

    // rotate the ratios according to the rotation of the page
    float[] rotRatios = rotateRatios(ratios, rotation);

    // use smallest box as basis for calculation
    Rectangle scaledBox = new Rectangle(smallestBox);

    scaledBox.setLeft(smallestBox.getLeft() + (smallestBox.getWidth() * rotRatios[0]));
    scaledBox.setBottom(smallestBox.getBottom() + (smallestBox.getHeight() * rotRatios[1]));
    scaledBox.setRight(smallestBox.getLeft() + (smallestBox.getWidth() * (1 - rotRatios[2])));
    scaledBox.setTop(smallestBox.getBottom() + (smallestBox.getHeight() * (1 - rotRatios[3])));

    return scaledBox;
}

From source file:com.chaschev.itext.ColumnTextBuilder.java

License:Apache License

public ColumnTextBuilder addTruncatedLine(Chunk chunk, boolean addEllipsis) {
    final float pixelsForEllipsis = 6;

    try {//from w  ww.  ja  v a2 s  .  c  o m
        ColumnText dup = ColumnText.duplicate(columnText);

        final Rectangle oneLineRectangle = new Rectangle(simpleColumnRectangle);

        oneLineRectangle.setTop(dup.getYLine());

        final float fontHeight = calcApproximateFontHeight(chunk.getFont()) * 1.6f;

        oneLineRectangle.setBottom(dup.getYLine() - fontHeight);

        if (addEllipsis) {
            oneLineRectangle.setRight(oneLineRectangle.getRight() - pixelsForEllipsis);
        }

        dup.setSimpleColumn(oneLineRectangle);
        dup.addText(chunk);

        final int status = dup.go();

        float yLine;

        if (addEllipsis && ColumnText.hasMoreText(status)) {
            oneLineRectangle.setLeft(dup.getLastX() + 2);
            oneLineRectangle.setRight(oneLineRectangle.getRight() + pixelsForEllipsis * 2);

            dup = ColumnText.duplicate(dup);

            dup.setSimpleColumn(oneLineRectangle);

            final Chunk ellipses = new Chunk("...\n", chunk.getFont());

            dup.setText(new Phrase(ellipses));
            dup.go();
            yLine = dup.getYLine();
        } else {
            yLine = dup.getYLine();
        }

        setYLine(yLine);

        return this;

    } catch (DocumentException e) {
        throw Exceptions.runtime(e);
    }
}

From source file:com.chaschev.itext.TwoColumnsSectionBuilder.java

License:Apache License

private void applyPadding(Rectangle r, boolean isLeft) {
    if (isLeft) {
        r.setRight(r.getRight() - hPadding);
    } else {//from  ww  w  .  ja v a  2  s .c om
        r.setLeft(r.getLeft() + hPadding);
    }
}

From source file:com.vectorprint.report.itext.EventHelper.java

License:Open Source License

/**
 * Calls the super and {@link Advanced#draw(com.itextpdf.text.Rectangle, java.lang.String) } for each Advanced styler
 * registered. Adds a debugging link for images when in debug mode.
 *
 * @param writer//from  ww w .  j av a2  s.c  om
 * @param document
 * @param rect
 * @param genericTag
 * @see #addDelayedStyler(java.lang.String, java.util.Collection, com.itextpdf.text.Chunk) 
 * @see Advanced#addDelayedData(java.lang.String, com.itextpdf.text.Chunk)
 * @see VectorPrintDocument
 */
@Override
public final void onGenericTag(PdfWriter writer, Document document, final Rectangle rect, String genericTag) {
    //      if (log.isLoggable(Level.FINE)) {
    //         Collection<Advanced> av = doOnGenericTag.get(genericTag);
    //         String data = null;
    //         if (av!=null) {
    //            for (Advanced a : av) {
    //               data += a.getDelayed(genericTag).getDataPart();
    //               break;
    //            }
    //         }
    //         System.out.println("wrapped: " + carriageReturns.toString() + ", " + genericTag + " " + data + " " + rect.toString() + ", x=" + rect.getLeft());
    //      }
    if (doOnGenericTag.get(genericTag) != null && !genericTag.startsWith(VectorPrintDocument.DRAWNEAR)
            && !genericTag.startsWith(VectorPrintDocument.DRAWSHADOW)) {
        int i = -1;
        for (Advanced a : doOnGenericTag.get(genericTag)) {
            ++i;
            if (genericTag.startsWith(DefaultElementProducer.ADV) && Integer
                    .parseInt(genericTag.replace(DefaultElementProducer.ADV, "")) > maxTagForGenericTagOnPage) {
                continue;
            }
            try {
                if (a.shouldDraw(a.getDelayed(genericTag).getData())) {
                    if (a instanceof DebugStyler && imageChunks.containsKey(genericTag)) {
                        Chunk wrapper = imageChunks.get(genericTag);
                        Object[] atts = (Object[]) wrapper.getAttributes().get(Chunk.IMAGE);
                        Rectangle shifted = new Rectangle(rect);
                        shifted.setLeft(shifted.getLeft() + (Float) atts[1]);
                        shifted.setRight(shifted.getRight() + (Float) atts[1]);
                        shifted.setTop(shifted.getTop() + (Float) atts[2]);
                        shifted.setBottom(shifted.getBottom() + (Float) atts[2]);
                        a.draw(shifted, genericTag);
                    } else if (!genericTag.startsWith(VectorPrintDocument.IMG_DEBUG)) {
                        a.draw(rect, genericTag);
                    }
                }
            } catch (VectorPrintException ex) {
                throw new VectorPrintRuntimeException(ex);
            }
        }
    }
    // images
    if (genericTag.startsWith(VectorPrintDocument.IMG_DEBUG)
            && getSettings().getBooleanProperty(false, DEBUG)) {
        // only now we can define a goto action, we know the position of the image
        if (rectangles.containsKey(genericTag)) {
            Rectangle rectangle = imageRectFromChunk(genericTag, rect);
            DebugHelper.debugAnnotation(rectangle, genericTag.replaceFirst(VectorPrintDocument.IMG_DEBUG, ""),
                    writer);
        } else {
            DebugHelper.debugAnnotation(rect, genericTag.replaceFirst(VectorPrintDocument.IMG_DEBUG, ""),
                    writer);
        }
    }
    if (genericTag.startsWith(VectorPrintDocument.DRAWNEAR)) {
        Rectangle rectangle = imageRectFromChunk(genericTag, rect);
        com.vectorprint.report.itext.style.stylers.Image image = (com.vectorprint.report.itext.style.stylers.Image) doOnGenericTag
                .get(genericTag).iterator().next();
        short i = -1;
        for (Advanced a : doOnGenericTag.get(genericTag)) {
            try {
                if (++i > 0 && a.shouldDraw(a.getDelayed(genericTag).getData())) {
                    if (getSettings().getBooleanProperty(false, DEBUG)) {
                        DebugHelper.styleLink(writer.getDirectContent(), a.getStyleClass(), "draw near",
                                rectangle.getLeft(), rectangle.getTop(), getSettings(), elementProducer);
                    }
                    a.draw(rectangle, genericTag);
                }
            } catch (VectorPrintException ex) {
                throw new VectorPrintRuntimeException(ex);
            }

        }
    }
    if (genericTag.startsWith(VectorPrintDocument.DRAWSHADOW)) {
        // we know the position of the image
        Rectangle r = imageRectFromChunk(genericTag, rect);
        com.vectorprint.report.itext.style.stylers.Image image = (com.vectorprint.report.itext.style.stylers.Image) doOnGenericTag
                .get(genericTag).iterator().next();
        try {
            image.drawShadow(r.getLeft(), r.getBottom(), r.getWidth(), r.getHeight(),
                    genericTag.replaceFirst(VectorPrintDocument.DRAWSHADOW, ""));
        } catch (VectorPrintException ex) {
            throw new VectorPrintRuntimeException(ex);
        }
    }
}

From source file:info.longnetpro.examples.PdfLibExamples.java

public static void generatePdf()
        throws DocumentException, URISyntaxException, MalformedURLException, IOException {
    String licFile = getLicenseFilePath();
    loadLicenseFile(licFile);//from  ww  w .j a v a  2s  .com

    String dest = getTargetFilePath();
    Document doc = new Document();
    PdfWriter.getInstance(doc, new FileOutputStream(dest));
    Rectangle pageSize = PageSize.LETTER;
    Rectangle rect = new Rectangle(0f, 0f, 50f, 100f);
    rect.setBorder(15);
    rect.setBorderColor(BaseColor.RED);
    rect.setBorderWidth(.5f);
    rect.setBackgroundColor(BaseColor.BLUE);
    doc.setPageSize(pageSize);
    doc.open();

    Page page = new Page(pageSize.getWidth(), pageSize.getHeight());
    ContentBox rpage = page.margin(new Float[] { 10f, 50f, 10f, 50f });

    for (Anchor anchor : Anchor.values()) {
        ContentBox box = new ContentBox(50f, 100f);
        float offx = anchor.equals(Anchor.CENTER) ? -50f : 0f;
        float offy = anchor.equals(Anchor.CENTER) ? -50f : 0f;

        ContentBox rr = anchor.anchorElement(rpage, box, offx, offy);

        if (anchor.equals(Anchor.BOTTOM_LEFT)) {
            float[] dim = box.scaleByPercentage(0.5f);
            //rr = rr.reposition(dim[0], dim[1], Anchor.TOP_RIGHT);
        }

        rect.setLeft(rr.getLeft());
        rect.setBottom(rr.getBottom());
        rect.setRight(rr.getRight());
        rect.setTop(rr.getTop());
        doc.add(rect);
    }
    String imageFile = getImageFilePath();
    Image image = Image.getInstance(imageFile);

    float width = Measurement.dotsToUserUnits(image.getWidth(), 1200);
    float height = Measurement.dotsToUserUnits(image.getHeight(), 1200);

    System.out.println(width + " " + height);

    ContentBox img = Anchor.BOTTOM_LEFT.anchorElement(rpage, width, height);

    image.scaleToFit(width, height);
    image.setAbsolutePosition(img.getLeft(), img.getBottom());

    System.out.println(image.getWidth() + " " + image.getHeight());
    System.out.println(image.getAbsoluteX() + " " + image.getAbsoluteY());

    doc.add(image);
    doc.close();
}