Search in sources :

Example 1 with PDAppearanceContentStream

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

the class AppearanceGeneratorHelper method insertGeneratedAppearance.

/**
 * Generate and insert text content and clipping around it.
 */
private void insertGeneratedAppearance(PDAnnotationWidget widget, PDAppearanceStream appearanceStream, OutputStream output) throws IOException {
    try (PDAppearanceContentStream contents = new PDAppearanceContentStream(appearanceStream, output)) {
        PDRectangle bbox = resolveBoundingBox(widget, appearanceStream);
        // Acrobat calculates the left and right padding dependent on the offset of the border edge
        // This calculation works for forms having been generated by Acrobat.
        // The minimum distance is always 1f even if there is no rectangle being drawn around.
        float borderWidth = 0;
        if (widget.getBorderStyle() != null) {
            borderWidth = widget.getBorderStyle().getWidth();
        }
        PDRectangle clipRect = applyPadding(bbox, Math.max(1f, borderWidth));
        PDRectangle contentRect = applyPadding(clipRect, Math.max(1f, borderWidth));
        contents.saveGraphicsState();
        // Acrobat always adds a clipping path
        contents.addRect(clipRect.getLowerLeftX(), clipRect.getLowerLeftY(), clipRect.getWidth(), clipRect.getHeight());
        contents.clip();
        // get the font
        PDFont font = defaultAppearance.getFont();
        // calculate the fontSize (because 0 = autosize)
        float fontSize = defaultAppearance.getFontSize();
        if (Float.compare(fontSize, 0) == 0) {
            fontSize = calculateFontSize(font, contentRect);
        }
        // options
        if (field instanceof PDListBox) {
            insertGeneratedSelectionHighlight(contents, appearanceStream, font, fontSize);
        }
        // start the text output
        contents.beginText();
        // write the /DA string
        defaultAppearance.writeTo(contents, fontSize);
        // calculate the y-position of the baseline
        float y;
        // calculate font metrics at font size
        float fontScaleY = fontSize / FONTSCALE;
        float fontBoundingBoxAtSize = font.getBoundingBox().getHeight() * fontScaleY;
        float fontCapAtSize = font.getFontDescriptor().getCapHeight() * fontScaleY;
        float fontDescentAtSize = font.getFontDescriptor().getDescent() * fontScaleY;
        if (field instanceof PDTextField && ((PDTextField) field).isMultiline()) {
            y = contentRect.getUpperRightY() - fontBoundingBoxAtSize;
        } else {
            // Adobe shows the text 'shiftet up' in case the caps don't fit into the clipping area
            if (fontCapAtSize > clipRect.getHeight()) {
                y = clipRect.getLowerLeftY() + -fontDescentAtSize;
            } else {
                // calculate the position based on the content rectangle
                y = clipRect.getLowerLeftY() + (clipRect.getHeight() - fontCapAtSize) / 2;
                // check to ensure that ascents and descents fit
                if (y - clipRect.getLowerLeftY() < -fontDescentAtSize) {
                    float fontDescentBased = -fontDescentAtSize + contentRect.getLowerLeftY();
                    float fontCapBased = contentRect.getHeight() - contentRect.getLowerLeftY() - fontCapAtSize;
                    y = Math.min(fontDescentBased, Math.max(y, fontCapBased));
                }
            }
        }
        // show the text
        float x = contentRect.getLowerLeftX();
        // chars
        if (shallComb()) {
            insertGeneratedCombAppearance(contents, appearanceStream, font, fontSize);
        } else if (field instanceof PDListBox) {
            insertGeneratedListboxAppearance(contents, appearanceStream, contentRect, font, fontSize);
        } else {
            PlainText textContent = new PlainText(value);
            AppearanceStyle appearanceStyle = new AppearanceStyle();
            appearanceStyle.setFont(font);
            appearanceStyle.setFontSize(fontSize);
            // Adobe Acrobat uses the font's bounding box for the leading between the lines
            appearanceStyle.setLeading(font.getBoundingBox().getHeight() * fontScaleY);
            PlainTextFormatter formatter = new PlainTextFormatter.Builder(contents).style(appearanceStyle).text(textContent).width(contentRect.getWidth()).wrapLines(isMultiLine()).initialOffset(x, y).textAlign(field.getQ()).build();
            formatter.format();
        }
        contents.endText();
        contents.restoreGraphicsState();
    }
}
Also used : PDFont(org.apache.pdfbox.pdmodel.font.PDFont) PDAppearanceContentStream(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle)

Example 2 with PDAppearanceContentStream

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

the class PDHighlightAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationHighlight annotation = (PDAnnotationHighlight) 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;
    }
    // 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
    // TODO padding should consider the curves too; needs to know in advance where the curve is
    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);
    }
    // get the delta used for curves and use it for padding
    float maxDelta = 0;
    for (int i = 0; i < pathsArray.length / 8; ++i) {
        // one of the two is 0, depending whether the rectangle is
        // horizontal or vertical
        // if it is diagonal then... uh...
        float delta = Math.max((pathsArray[i + 0] - pathsArray[i + 4]) / 4, (pathsArray[i + 1] - pathsArray[i + 5]) / 4);
        maxDelta = Math.max(delta, maxDelta);
    }
    rect.setLowerLeftX(Math.min(minX - ab.width / 2 - maxDelta, rect.getLowerLeftX()));
    rect.setLowerLeftY(Math.min(minY - ab.width / 2 - maxDelta, rect.getLowerLeftY()));
    rect.setUpperRightX(Math.max(maxX + ab.width + maxDelta, rect.getUpperRightX()));
    rect.setUpperRightY(Math.max(maxY + ab.width + maxDelta, rect.getUpperRightY()));
    annotation.setRectangle(rect);
    try {
        try (PDAppearanceContentStream cs = getNormalAppearanceAsContentStream()) {
            prepareResources();
            PDExtendedGraphicsState r0 = new PDExtendedGraphicsState();
            PDExtendedGraphicsState r1 = new PDExtendedGraphicsState();
            r0.setAlphaSourceFlag(false);
            r0.setStrokingAlphaConstant(annotation.getConstantOpacity());
            r0.setNonStrokingAlphaConstant(annotation.getConstantOpacity());
            r1.setAlphaSourceFlag(false);
            r1.setBlendMode(BlendMode.MULTIPLY);
            cs.setGraphicsStateParameters(r0);
            cs.setGraphicsStateParameters(r1);
            // TODO replace with document.getDocument().createCOSStream()
            // or call new PDFormXObject(document)
            PDFormXObject frm1 = new PDFormXObject(new COSStream());
            PDFormXObject frm2 = new PDFormXObject(new COSStream());
            frm1.setResources(new PDResources());
            try (PDFormContentStream mwfofrmCS = new PDFormContentStream(frm1)) {
                mwfofrmCS.drawForm(frm2);
            }
            frm1.setBBox(annotation.getRectangle());
            COSDictionary groupDict = new COSDictionary();
            groupDict.setItem(COSName.S, COSName.TRANSPARENCY);
            // TODO PDFormXObject.setGroup() is missing
            frm1.getCOSObject().setItem(COSName.GROUP, groupDict);
            cs.drawForm(frm1);
            frm2.setBBox(annotation.getRectangle());
            try (PDFormContentStream frm2CS = new PDFormContentStream(frm2)) {
                frm2CS.setNonStrokingColor(color);
                int of = 0;
                while (of + 7 < pathsArray.length) {
                    // quadpoints spec sequence is incorrect, correct one is (4,5 0,1 2,3 6,7)
                    // https://stackoverflow.com/questions/9855814/pdf-spec-vs-acrobat-creation-quadpoints
                    // for "curvy" highlighting, two Bézier control points are used that seem to have a
                    // distance of about 1/4 of the height.
                    // note that curves won't appear if outside of the rectangle
                    float delta = 0;
                    if (Float.compare(pathsArray[of + 0], pathsArray[of + 4]) == 0 && Float.compare(pathsArray[of + 1], pathsArray[of + 3]) == 0 && Float.compare(pathsArray[of + 2], pathsArray[of + 6]) == 0 && Float.compare(pathsArray[of + 5], pathsArray[of + 7]) == 0) {
                        // horizontal highlight
                        delta = (pathsArray[of + 1] - pathsArray[of + 5]) / 4;
                    } else if (Float.compare(pathsArray[of + 1], pathsArray[of + 5]) == 0 && Float.compare(pathsArray[of + 0], pathsArray[of + 2]) == 0 && Float.compare(pathsArray[of + 3], pathsArray[of + 7]) == 0 && Float.compare(pathsArray[of + 4], pathsArray[of + 6]) == 0) {
                        // vertical highlight
                        delta = (pathsArray[of + 0] - pathsArray[of + 4]) / 4;
                    }
                    frm2CS.moveTo(pathsArray[of + 4], pathsArray[of + 5]);
                    if (Float.compare(pathsArray[of + 0], pathsArray[of + 4]) == 0) {
                        // horizontal highlight
                        frm2CS.curveTo(pathsArray[of + 4] - delta, pathsArray[of + 5] + delta, pathsArray[of + 0] - delta, pathsArray[of + 1] - delta, pathsArray[of + 0], pathsArray[of + 1]);
                    } else if (Float.compare(pathsArray[of + 5], pathsArray[of + 1]) == 0) {
                        // vertical highlight
                        frm2CS.curveTo(pathsArray[of + 4] + delta, pathsArray[of + 5] + delta, pathsArray[of + 0] - delta, pathsArray[of + 1] + delta, pathsArray[of + 0], pathsArray[of + 1]);
                    } else {
                        frm2CS.lineTo(pathsArray[of + 0], pathsArray[of + 1]);
                    }
                    frm2CS.lineTo(pathsArray[of + 2], pathsArray[of + 3]);
                    if (Float.compare(pathsArray[of + 2], pathsArray[of + 6]) == 0) {
                        // horizontal highlight
                        frm2CS.curveTo(pathsArray[of + 2] + delta, pathsArray[of + 3] - delta, pathsArray[of + 6] + delta, pathsArray[of + 7] + delta, pathsArray[of + 6], pathsArray[of + 7]);
                    } else if (Float.compare(pathsArray[of + 3], pathsArray[of + 7]) == 0) {
                        // vertical highlight
                        frm2CS.curveTo(pathsArray[of + 2] - delta, pathsArray[of + 3] - delta, pathsArray[of + 6] + delta, pathsArray[of + 7] - delta, pathsArray[of + 6], pathsArray[of + 7]);
                    } else {
                        frm2CS.lineTo(pathsArray[of + 6], pathsArray[of + 7]);
                    }
                    frm2CS.fill();
                    of += 8;
                }
            }
        }
    } catch (IOException ex) {
        LOG.error(ex);
    }
}
Also used : PDFormContentStream(org.apache.pdfbox.pdmodel.PDFormContentStream) COSStream(org.apache.pdfbox.cos.COSStream) COSDictionary(org.apache.pdfbox.cos.COSDictionary) PDExtendedGraphicsState(org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState) PDAnnotationHighlight(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationHighlight) PDResources(org.apache.pdfbox.pdmodel.PDResources) IOException(java.io.IOException) PDColor(org.apache.pdfbox.pdmodel.graphics.color.PDColor) PDAppearanceContentStream(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream) PDFormXObject(org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle)

Example 3 with PDAppearanceContentStream

use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream 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 4 with PDAppearanceContentStream

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

the class PDStrikeoutAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationStrikeout annotation = (PDAnnotationStrikeout) 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
    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) {
                // get mid point between bounds, substract the line width to approximate what Adobe is doing
                // See e.g. CTAN-example-Annotations.pdf and PLPDF.com-MarkupAnnotations.pdf
                // and https://bugs.ghostscript.com/show_bug.cgi?id=693664
                // do the math for diagonal annotations with this weird old trick:
                // https://stackoverflow.com/questions/7740507/extend-a-line-segment-a-specific-distance
                float len0 = (float) (Math.sqrt(Math.pow(pathsArray[i * 8] - pathsArray[i * 8 + 4], 2) + Math.pow(pathsArray[i * 8 + 1] - pathsArray[i * 8 + 5], 2)));
                float x0 = pathsArray[i * 8 + 4] + (pathsArray[i * 8] - pathsArray[i * 8 + 4]) / len0 * (len0 / 2 - ab.width);
                float y0 = pathsArray[i * 8 + 5] + (pathsArray[i * 8 + 1] - pathsArray[i * 8 + 5]) / len0 * (len0 / 2 - ab.width);
                float len1 = (float) (Math.sqrt(Math.pow(pathsArray[i * 8 + 2] - pathsArray[i * 8 + 6], 2) + Math.pow(pathsArray[i * 8 + 3] - pathsArray[i * 8 + 7], 2)));
                float x1 = pathsArray[i * 8 + 6] + (pathsArray[i * 8 + 2] - pathsArray[i * 8 + 6]) / len1 * (len1 / 2 - ab.width);
                float y1 = pathsArray[i * 8 + 7] + (pathsArray[i * 8 + 3] - pathsArray[i * 8 + 7]) / len1 * (len1 / 2 - ab.width);
                cs.moveTo(x0, y0);
                cs.lineTo(x1, y1);
            }
            cs.stroke();
        }
    } catch (IOException ex) {
        LOG.error(ex);
    }
}
Also used : PDAppearanceContentStream(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream) PDAnnotationStrikeout(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationStrikeout) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) IOException(java.io.IOException) PDColor(org.apache.pdfbox.pdmodel.graphics.color.PDColor)

Example 5 with PDAppearanceContentStream

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

the class PDAbstractAppearanceHandler method getAppearanceEntryAsContentStream.

private PDAppearanceContentStream getAppearanceEntryAsContentStream(PDAppearanceEntry appearanceEntryToStream) throws IOException {
    PDAppearanceStream appearanceStream = appearanceEntryToStream.getAppearanceStream();
    setTransformationMatrix(appearanceStream);
    return new PDAppearanceContentStream(appearanceStream);
}
Also used : PDAppearanceStream(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream) PDAppearanceContentStream(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream)

Aggregations

PDAppearanceContentStream (org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream)13 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)11 IOException (java.io.IOException)10 PDColor (org.apache.pdfbox.pdmodel.graphics.color.PDColor)8 COSArray (org.apache.pdfbox.cos.COSArray)2 AffineTransform (java.awt.geom.AffineTransform)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 COSDictionary (org.apache.pdfbox.cos.COSDictionary)1 COSStream (org.apache.pdfbox.cos.COSStream)1 PDFormContentStream (org.apache.pdfbox.pdmodel.PDFormContentStream)1 PDResources (org.apache.pdfbox.pdmodel.PDResources)1 PDFont (org.apache.pdfbox.pdmodel.font.PDFont)1 PDType1Font (org.apache.pdfbox.pdmodel.font.PDType1Font)1 PDFormXObject (org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject)1 PDExtendedGraphicsState (org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState)1 PDAnnotationCircle (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationCircle)1 PDAnnotationHighlight (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationHighlight)1 PDAnnotationInk (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationInk)1 PDAnnotationLine (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLine)1 PDAnnotationLink (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink)1