Search in sources :

Example 6 with PDBorderStyleDictionary

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

the class AppearanceGeneratorHelper method initializeAppearanceContent.

/**
 * Initialize the content of the appearance stream.
 *
 * Get settings like border style, border width and colors to be used to draw a rectangle and background color
 * around the widget
 *
 * @param widget the field widget
 * @param appearanceStream the appearance stream to be used
 * @throws IOException in case we can't write to the appearance stream
 */
private void initializeAppearanceContent(PDAnnotationWidget widget, PDAppearanceStream appearanceStream) throws IOException {
    try (ByteArrayOutputStream output = new ByteArrayOutputStream();
        PDAppearanceContentStream contents = new PDAppearanceContentStream(appearanceStream, output)) {
        PDAppearanceCharacteristicsDictionary appearanceCharacteristics = widget.getAppearanceCharacteristics();
        // TODO: support more entries like patterns, etc.
        if (appearanceCharacteristics != null) {
            PDColor backgroundColour = appearanceCharacteristics.getBackground();
            if (backgroundColour != null) {
                contents.setNonStrokingColor(backgroundColour);
                PDRectangle bbox = resolveBoundingBox(widget, appearanceStream);
                contents.addRect(bbox.getLowerLeftX(), bbox.getLowerLeftY(), bbox.getWidth(), bbox.getHeight());
                contents.fill();
            }
            float lineWidth = 0f;
            PDColor borderColour = appearanceCharacteristics.getBorderColour();
            if (borderColour != null) {
                contents.setStrokingColor(borderColour);
                lineWidth = 1f;
            }
            PDBorderStyleDictionary borderStyle = widget.getBorderStyle();
            if (borderStyle != null && borderStyle.getWidth() > 0) {
                lineWidth = borderStyle.getWidth();
            }
            if (lineWidth > 0 && borderColour != null) {
                if (Float.compare(lineWidth, 1) != 0) {
                    contents.setLineWidth(lineWidth);
                }
                PDRectangle bbox = resolveBoundingBox(widget, appearanceStream);
                PDRectangle clipRect = applyPadding(bbox, Math.max(DEFAULT_PADDING, lineWidth / 2));
                contents.addRect(clipRect.getLowerLeftX(), clipRect.getLowerLeftY(), clipRect.getWidth(), clipRect.getHeight());
                contents.closeAndStroke();
            }
        }
        writeToStream(output.toByteArray(), appearanceStream);
    }
}
Also used : PDAppearanceCharacteristicsDictionary(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary) PDAppearanceContentStream(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PDBorderStyleDictionary(org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary) PDColor(org.apache.pdfbox.pdmodel.graphics.color.PDColor)

Example 7 with PDBorderStyleDictionary

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

the class PDCircleAppearanceHandler 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() {
    PDAnnotationCircle annotation = (PDAnnotationCircle) 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) PDAnnotationCircle(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationCircle) COSNumber(org.apache.pdfbox.cos.COSNumber) COSBase(org.apache.pdfbox.cos.COSBase) PDBorderStyleDictionary(org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)

Example 8 with PDBorderStyleDictionary

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

the class PDLinkAppearanceHandler 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() {
    PDAnnotationLink annotation = (PDAnnotationLink) 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) COSNumber(org.apache.pdfbox.cos.COSNumber) COSBase(org.apache.pdfbox.cos.COSBase) PDAnnotationLink(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink) PDBorderStyleDictionary(org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)

Aggregations

PDBorderStyleDictionary (org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)8 COSArray (org.apache.pdfbox.cos.COSArray)5 COSBase (org.apache.pdfbox.cos.COSBase)5 COSNumber (org.apache.pdfbox.cos.COSNumber)5 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)3 PDAnnotationLink (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink)3 PDColor (org.apache.pdfbox.pdmodel.graphics.color.PDColor)2 PDActionURI (org.apache.pdfbox.pdmodel.interactive.action.PDActionURI)2 PDAnnotationCircle (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationCircle)2 PDAnnotationSquare (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquare)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)1 PDPage (org.apache.pdfbox.pdmodel.PDPage)1 PDPageContentStream (org.apache.pdfbox.pdmodel.PDPageContentStream)1 PDResources (org.apache.pdfbox.pdmodel.PDResources)1 PDFont (org.apache.pdfbox.pdmodel.font.PDFont)1 PDActionGoTo (org.apache.pdfbox.pdmodel.interactive.action.PDActionGoTo)1 PDAnnotation (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation)1 PDAnnotationFreeText (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationFreeText)1 PDAnnotationHighlight (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationHighlight)1