Search in sources :

Example 1 with PDAnnotationPolyline

use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationPolyline in project pdfbox by apache.

the class PDPolylineAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationPolyline annotation = (PDAnnotationPolyline) getAnnotation();
    PDRectangle rect = annotation.getRectangle();
    float[] pathsArray = annotation.getVertices();
    if (pathsArray == null) {
        return;
    }
    AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(annotation, annotation.getBorderStyle());
    PDColor color = annotation.getColor();
    if (color == null || color.getComponents().length == 0 || Float.compare(ab.width, 0) == 0) {
        return;
    }
    // Adjust rectangle even if not empty
    // CTAN-example-Annotations.pdf and pdf_commenting_new.pdf p11
    // TODO in a class structure this should be overridable
    float minX = Float.MAX_VALUE;
    float minY = Float.MAX_VALUE;
    float maxX = Float.MIN_VALUE;
    float maxY = Float.MIN_VALUE;
    for (int i = 0; i < pathsArray.length / 2; ++i) {
        float x = pathsArray[i * 2];
        float y = pathsArray[i * 2 + 1];
        minX = Math.min(minX, x);
        minY = Math.min(minY, y);
        maxX = Math.max(maxX, x);
        maxY = Math.max(maxY, y);
    }
    rect.setLowerLeftX(Math.min(minX - ab.width / 2, rect.getLowerLeftX()));
    rect.setLowerLeftY(Math.min(minY - ab.width / 2, rect.getLowerLeftY()));
    rect.setUpperRightX(Math.max(maxX + ab.width, rect.getUpperRightX()));
    rect.setUpperRightY(Math.max(maxY + ab.width, rect.getUpperRightY()));
    annotation.setRectangle(rect);
    try {
        try (PDAppearanceContentStream cs = getNormalAppearanceAsContentStream()) {
            handleOpacity(annotation.getConstantOpacity());
            cs.setStrokingColor(color);
            if (ab.dashArray != null) {
                cs.setLineDashPattern(ab.dashArray, 0);
            }
            cs.setLineWidth(ab.width);
            for (int i = 0; i < pathsArray.length / 2; ++i) {
                float x = pathsArray[i * 2];
                float y = pathsArray[i * 2 + 1];
                if (i == 0) {
                    cs.moveTo(x, y);
                } else {
                    cs.lineTo(x, y);
                }
            }
            cs.stroke();
        }
    } catch (IOException ex) {
        LOG.error(ex);
    }
}
Also used : PDAnnotationPolyline(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationPolyline) PDAppearanceContentStream(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) IOException(java.io.IOException) PDColor(org.apache.pdfbox.pdmodel.graphics.color.PDColor)

Example 2 with PDAnnotationPolyline

use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationPolyline in project pdfbox by apache.

the class PDPolylineAppearanceHandler method getLineWidth.

// TODO DRY, this code is from polygonAppearanceHandler so it's double
/**
 * Get the line with of the border.
 *
 * Get the width of the line used to draw a border around the annotation.
 * This may either be specified by the annotation dictionaries Border
 * setting or by the W entry in the BS border style dictionary. If both are
 * missing the default width is 1.
 *
 * @return the line width
 */
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth() {
    PDAnnotationPolyline annotation = (PDAnnotationPolyline) getAnnotation();
    PDBorderStyleDictionary bs = annotation.getBorderStyle();
    if (bs != null) {
        return bs.getWidth();
    } else {
        COSArray borderCharacteristics = annotation.getBorder();
        if (borderCharacteristics.size() >= 3) {
            COSBase base = borderCharacteristics.getObject(2);
            if (base instanceof COSNumber) {
                return ((COSNumber) base).floatValue();
            }
        }
    }
    return 1;
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) PDAnnotationPolyline(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationPolyline) COSNumber(org.apache.pdfbox.cos.COSNumber) COSBase(org.apache.pdfbox.cos.COSBase) PDBorderStyleDictionary(org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)

Aggregations

PDAnnotationPolyline (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationPolyline)2 IOException (java.io.IOException)1 COSArray (org.apache.pdfbox.cos.COSArray)1 COSBase (org.apache.pdfbox.cos.COSBase)1 COSNumber (org.apache.pdfbox.cos.COSNumber)1 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)1 PDColor (org.apache.pdfbox.pdmodel.graphics.color.PDColor)1 PDAppearanceContentStream (org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream)1 PDBorderStyleDictionary (org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)1