Search in sources :

Example 1 with PDAnnotationMarkup

use of com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup in project PdfBox-Android by TomRoush.

the class PDInkAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationMarkup ink = (PDAnnotationMarkup) getAnnotation();
    // PDF spec does not mention /Border for ink annotations, but it is used if /BS is not available
    AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(ink, ink.getBorderStyle());
    PDColor color = ink.getColor();
    if (color == null || color.getComponents().length == 0 || Float.compare(ab.width, 0) == 0) {
        return;
    }
    PDAppearanceContentStream cs = null;
    try {
        cs = getNormalAppearanceAsContentStream();
        setOpacity(cs, ink.getConstantOpacity());
        cs.setStrokingColor(color);
        if (ab.dashArray != null) {
            cs.setLineDashPattern(ab.dashArray, 0);
        }
        cs.setLineWidth(ab.width);
        for (float[] pathArray : ink.getInkList()) {
            int nPoints = pathArray.length / 2;
            // in an implementation-dependent way" - we do lines.
            for (int i = 0; i < nPoints; ++i) {
                float x = pathArray[i * 2];
                float y = pathArray[i * 2 + 1];
                if (i == 0) {
                    cs.moveTo(x, y);
                } else {
                    cs.lineTo(x, y);
                }
            }
            cs.stroke();
        }
    } catch (IOException ex) {
        Log.e("PdfBox-Android", ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(cs);
    }
}
Also used : PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PDAnnotationMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup) IOException(java.io.IOException) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)

Example 2 with PDAnnotationMarkup

use of com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup in project PdfBox-Android by TomRoush.

the class PDPolygonAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();
    float lineWidth = getLineWidth();
    PDRectangle rect = annotation.getRectangle();
    // Adjust rectangle even if not empty
    // CTAN-example-Annotations.pdf p2
    float minX = Float.MAX_VALUE;
    float minY = Float.MAX_VALUE;
    float maxX = Float.MIN_VALUE;
    float maxY = Float.MIN_VALUE;
    float[][] pathArray = getPathArray(annotation);
    if (pathArray == null) {
        return;
    }
    for (int i = 0; i < pathArray.length; ++i) {
        for (int j = 0; j < pathArray[i].length / 2; ++j) {
            float x = pathArray[i][j * 2];
            float y = pathArray[i][j * 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 - lineWidth, rect.getLowerLeftX()));
    rect.setLowerLeftY(Math.min(minY - lineWidth, rect.getLowerLeftY()));
    rect.setUpperRightX(Math.max(maxX + lineWidth, rect.getUpperRightX()));
    rect.setUpperRightY(Math.max(maxY + lineWidth, rect.getUpperRightY()));
    annotation.setRectangle(rect);
    PDAppearanceContentStream contentStream = null;
    try {
        contentStream = getNormalAppearanceAsContentStream();
        boolean hasStroke = contentStream.setStrokingColorOnDemand(getColor());
        boolean hasBackground = contentStream.setNonStrokingColorOnDemand(annotation.getInteriorColor());
        setOpacity(contentStream, annotation.getConstantOpacity());
        contentStream.setBorderLine(lineWidth, annotation.getBorderStyle(), annotation.getBorder());
        PDBorderEffectDictionary borderEffect = annotation.getBorderEffect();
        if (borderEffect != null && borderEffect.getStyle().equals(PDBorderEffectDictionary.STYLE_CLOUDY)) {
            CloudyBorder cloudyBorder = new CloudyBorder(contentStream, borderEffect.getIntensity(), lineWidth, getRectangle());
            cloudyBorder.createCloudyPolygon(pathArray);
            annotation.setRectangle(cloudyBorder.getRectangle());
            PDAppearanceStream appearanceStream = annotation.getNormalAppearanceStream();
            appearanceStream.setBBox(cloudyBorder.getBBox());
            appearanceStream.setMatrix(cloudyBorder.getMatrix());
        } else {
            for (int i = 0; i < pathArray.length; i++) {
                float[] pointsArray = pathArray[i];
                // first array shall be of size 2 and specify the moveto operator
                if (i == 0 && pointsArray.length == 2) {
                    contentStream.moveTo(pointsArray[0], pointsArray[1]);
                } else {
                    // entries of length 2 shall be treated as lineto operator
                    if (pointsArray.length == 2) {
                        contentStream.lineTo(pointsArray[0], pointsArray[1]);
                    } else if (pointsArray.length == 6) {
                        contentStream.curveTo(pointsArray[0], pointsArray[1], pointsArray[2], pointsArray[3], pointsArray[4], pointsArray[5]);
                    }
                }
            }
            contentStream.closePath();
        }
        contentStream.drawShape(lineWidth, hasStroke, hasBackground);
    } catch (IOException e) {
        Log.e("PdfBox-Android", e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(contentStream);
    }
}
Also used : PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PDAppearanceStream(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream) PDBorderEffectDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderEffectDictionary) PDAnnotationMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) IOException(java.io.IOException)

Example 3 with PDAnnotationMarkup

use of com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup in project PdfBox-Android by TomRoush.

the class PDPolygonAppearanceHandler method getLineWidth.

/**
 * 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() {
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();
    PDBorderStyleDictionary bs = annotation.getBorderStyle();
    if (bs != null) {
        return bs.getWidth();
    }
    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(com.tom_roush.pdfbox.cos.COSArray) PDAnnotationMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDBorderStyleDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)

Example 4 with PDAnnotationMarkup

use of com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup in project PdfBox-Android by TomRoush.

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() {
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();
    PDBorderStyleDictionary bs = annotation.getBorderStyle();
    if (bs != null) {
        return bs.getWidth();
    }
    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(com.tom_roush.pdfbox.cos.COSArray) PDAnnotationMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDBorderStyleDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)

Example 5 with PDAnnotationMarkup

use of com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup in project PdfBox-Android by TomRoush.

the class PDSquareAppearanceHandler method getLineWidth.

/**
 * 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() {
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();
    PDBorderStyleDictionary bs = annotation.getBorderStyle();
    if (bs != null) {
        return bs.getWidth();
    }
    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(com.tom_roush.pdfbox.cos.COSArray) PDAnnotationMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDBorderStyleDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)

Aggregations

PDAnnotationMarkup (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup)9 PDAppearanceContentStream (com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream)5 IOException (java.io.IOException)5 COSArray (com.tom_roush.pdfbox.cos.COSArray)4 COSBase (com.tom_roush.pdfbox.cos.COSBase)4 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)4 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)4 PDBorderStyleDictionary (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)4 PDColor (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)3 PDAppearanceStream (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream)2 PDBorderEffectDictionary (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderEffectDictionary)2 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)1 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)1 AppearanceStyle (com.tom_roush.pdfbox.pdmodel.interactive.annotation.layout.AppearanceStyle)1 PlainText (com.tom_roush.pdfbox.pdmodel.interactive.annotation.layout.PlainText)1 PlainTextFormatter (com.tom_roush.pdfbox.pdmodel.interactive.annotation.layout.PlainTextFormatter)1 Matrix (com.tom_roush.pdfbox.util.Matrix)1 Matcher (java.util.regex.Matcher)1