use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream in project pdfbox by apache.
the class PDPolygonAppearanceHandler method generateNormalAppearance.
@Override
public void generateNormalAppearance() {
PDAnnotationPolygon annotation = (PDAnnotationPolygon) 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 / 2, rect.getLowerLeftX()));
rect.setLowerLeftY(Math.min(minY - lineWidth / 2, rect.getLowerLeftY()));
rect.setUpperRightX(Math.max(maxX + lineWidth, rect.getUpperRightX()));
rect.setUpperRightY(Math.max(maxY + lineWidth, rect.getUpperRightY()));
annotation.setRectangle(rect);
try {
try (PDAppearanceContentStream contentStream = getNormalAppearanceAsContentStream()) {
boolean hasStroke = contentStream.setStrokingColorOnDemand(getColor());
boolean hasBackground = contentStream.setNonStrokingColorOnDemand(annotation.getInteriorColor());
handleOpacity(annotation.getConstantOpacity());
contentStream.setBorderLine(lineWidth, annotation.getBorderStyle());
// setBorderLine(), or use AnnotationBorder class
if (annotation.getBorderStyle() == null) {
COSArray border = annotation.getBorder();
if (border.size() > 3 && border.getObject(3) instanceof COSArray) {
contentStream.setLineDashPattern(((COSArray) border.getObject(3)).toFloatArray(), 0);
}
}
// the differences rectangle
// TODO: this only works for border effect solid. Cloudy needs a
// different approach.
setRectDifference(lineWidth);
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.drawShape(lineWidth, hasStroke, hasBackground);
}
} catch (IOException e) {
LOG.error(e);
}
}
use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream in project pdfbox by apache.
the class PDSquareAppearanceHandler method generateNormalAppearance.
@Override
public void generateNormalAppearance() {
float lineWidth = getLineWidth();
try {
PDAnnotationSquare annotation = (PDAnnotationSquare) getAnnotation();
try (PDAppearanceContentStream contentStream = getNormalAppearanceAsContentStream()) {
boolean hasStroke = contentStream.setStrokingColorOnDemand(getColor());
boolean hasBackground = contentStream.setNonStrokingColorOnDemand(annotation.getInteriorColor());
handleOpacity(annotation.getConstantOpacity());
contentStream.setBorderLine(lineWidth, annotation.getBorderStyle());
// handle the border box
//
// There are two options. The handling is not part of the PDF specification but
// implementation specific to Adobe Reader
// - if /RD is set the border box is the /Rect entry inset by the respective
// border difference.
// - if /RD is not set the border box is defined by the /Rect entry. The /RD entry will
// be set to be the line width and the /Rect is enlarged by the /RD amount
PDRectangle borderBox = null;
float[] rectDifferences = annotation.getRectDifferences();
if (rectDifferences.length == 0) {
borderBox = getPaddedRectangle(getRectangle(), lineWidth / 2);
// the differences rectangle
// TODO: this only works for border effect solid. Cloudy needs a different approach.
annotation.setRectDifferences(lineWidth / 2);
annotation.setRectangle(addRectDifferences(getRectangle(), annotation.getRectDifferences()));
// when the normal appearance stream was generated BBox and Matrix have been set to the
// values of the original /Rect. As the /Rect was changed that needs to be adjusted too.
annotation.getNormalAppearanceStream().setBBox(getRectangle());
AffineTransform transform = AffineTransform.getTranslateInstance(-getRectangle().getLowerLeftX(), -getRectangle().getLowerLeftY());
annotation.getNormalAppearanceStream().setMatrix(transform);
} else {
borderBox = applyRectDifferences(getRectangle(), rectDifferences);
borderBox = getPaddedRectangle(borderBox, lineWidth / 2);
}
contentStream.addRect(borderBox.getLowerLeftX(), borderBox.getLowerLeftY(), borderBox.getWidth(), borderBox.getHeight());
contentStream.drawShape(lineWidth, hasStroke, hasBackground);
}
} catch (IOException e) {
LOG.error(e);
}
}
use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream in project pdfbox by apache.
the class PDUnderlineAppearanceHandler method generateNormalAppearance.
@Override
public void generateNormalAppearance() {
PDAnnotationUnderline annotation = (PDAnnotationUnderline) getAnnotation();
PDRectangle rect = annotation.getRectangle();
float[] pathsArray = annotation.getQuadPoints();
if (pathsArray == null) {
return;
}
AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(annotation, annotation.getBorderStyle());
PDColor color = annotation.getColor();
if (color == null || color.getComponents().length == 0) {
return;
}
if (Float.compare(ab.width, 0) == 0) {
// value found in adobe reader
ab.width = 1.5f;
}
// Adjust rectangle even if not empty, see PLPDF.com-MarkupAnnotations.pdf
// TODO in a class structure this should be overridable
// this is similar to polyline but different data type
// all coordinates (unlike painting) are used because I'm lazy
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);
// https://stackoverflow.com/questions/9855814/pdf-spec-vs-acrobat-creation-quadpoints
for (int i = 0; i < pathsArray.length / 8; ++i) {
// only lower coords are used
cs.moveTo(pathsArray[i * 8 + 4], pathsArray[i * 8 + 5]);
cs.lineTo(pathsArray[i * 8 + 6], pathsArray[i * 8 + 7]);
}
cs.stroke();
}
} catch (IOException ex) {
LOG.error(ex);
}
}
Aggregations