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);
}
}
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;
}
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;
}
Aggregations