Example usage for org.apache.pdfbox.pdmodel.interactive.annotation PDAnnotation setRectangle

List of usage examples for org.apache.pdfbox.pdmodel.interactive.annotation PDAnnotation setRectangle

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.interactive.annotation PDAnnotation setRectangle.

Prototype

public void setRectangle(PDRectangle rectangle) 

Source Link

Document

This will set the rectangle for this annotation.

Usage

From source file:airviewer.AnnotationGenerator.java

/**
 *
 * @param a//from w ww .  j av a  2 s  .c om
 * @throws IOException
 */
public static void resizeAnnotationToContent(PDAnnotation a) throws IOException {
    assert null != a;

    final String contents = a.getContents();
    final boolean hasContents = null != contents && 0 < contents.length();
    if (hasContents) {
        float borderWidth = 0;
        if (null != a.getColor() && a instanceof PDAnnotationMarkup) {
            final PDBorderStyleDictionary borderStyle = ((PDAnnotationMarkup) a).getBorderStyle();
            if (null != borderStyle) {
                borderWidth = Math.abs(borderStyle.getWidth());
            }
        }
        final float margin = MARGIN_SIZE_PDF_POINTS;
        final float fontSize = FONT_SIZE_PDF_POINTS;
        final float textHeight = fontSize + LINE_SPACE_SIZE_PDF_POINTS;
        PDRectangle position = a.getRectangle();
        final float lowerLeftX = position.getLowerLeftX();
        final float lowerLeftY = position.getLowerLeftY();

        float width = FONT.getStringWidth(contents) * fontSize / SIZE_UNITS_PER_PDF_POINT;
        float height = textHeight;

        // Reserve enough width for border and margin at start and end
        width += (borderWidth + MARGIN_SIZE_PDF_POINTS) * 2.0f;
        height += (borderWidth + MARGIN_SIZE_PDF_POINTS) * 2.0f;

        // Replace the annotations existing rectangle
        a.setRectangle(new PDRectangle(lowerLeftX, lowerLeftY, width, height));
    }

}

From source file:org.apache.fop.render.pdf.pdfbox.PDFBoxAdapter.java

License:Apache License

private void moveAnnotations(PDPage page, List pageAnnotations, AffineTransform at) {
    PDRectangle mediaBox = page.getMediaBox();
    PDRectangle cropBox = page.getCropBox();
    PDRectangle viewBox = cropBox != null ? cropBox : mediaBox;
    for (Object obj : pageAnnotations) {
        PDAnnotation annot = (PDAnnotation) obj;
        PDRectangle rect = annot.getRectangle();
        float translateX = (float) (at.getTranslateX() - viewBox.getLowerLeftX());
        float translateY = (float) (at.getTranslateY() - viewBox.getLowerLeftY());
        if (rect != null) {
            rect.setUpperRightX(rect.getUpperRightX() + translateX);
            rect.setLowerLeftX(rect.getLowerLeftX() + translateX);
            rect.setUpperRightY(rect.getUpperRightY() + translateY);
            rect.setLowerLeftY(rect.getLowerLeftY() + translateY);
            annot.setRectangle(rect);
        }/* w ww  . j ava 2s.c  o  m*/
        //            COSArray vertices = (COSArray) annot.getCOSObject().getDictionaryObject("Vertices");
        //            if (vertices != null) {
        //                Iterator iter = vertices.iterator();
        //                while (iter.hasNext()) {
        //                    COSFloat x = (COSFloat) iter.next();
        //                    COSFloat y = (COSFloat) iter.next();
        //                    x.setValue(x.floatValue() + translateX);
        //                    y.setValue(y.floatValue() + translateY);
        //                }
        //            }
    }
}