Search in sources :

Example 1 with PDAnnotationTextMarkup

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

the class PDSquigglyAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationTextMarkup annotation = (PDAnnotationTextMarkup) 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 / 2, rect.getUpperRightX()));
    rect.setUpperRightY(Math.max(maxY + ab.width / 2, rect.getUpperRightY()));
    annotation.setRectangle(rect);
    PDAppearanceContentStream cs = null;
    try {
        cs = getNormalAppearanceAsContentStream();
        setOpacity(cs, annotation.getConstantOpacity());
        cs.setStrokingColor(color);
        // https://stackoverflow.com/questions/9855814/pdf-spec-vs-acrobat-creation-quadpoints
        for (int i = 0; i < pathsArray.length / 8; ++i) {
            // Adobe uses a fixed pattern that assumes a height of 40, and it transforms to that height
            // horizontally and the same / 1.8 vertically.
            // translation apparently based on bottom left, but slightly different in Adobe
            // TODO what if the annotation is not horizontal?
            float height = pathsArray[i * 8 + 1] - pathsArray[i * 8 + 5];
            cs.transform(new Matrix(height / 40f, 0, 0, height / 40f / 1.8f, pathsArray[i * 8 + 4], pathsArray[i * 8 + 5]));
            // Create form, BBox is mostly fixed, except for the horizontal size which is
            // horizontal size divided by the horizontal transform factor from above
            // (almost)
            PDFormXObject form = new PDFormXObject(createCOSStream());
            form.setBBox(new PDRectangle(-0.5f, -0.5f, (pathsArray[i * 8 + 2] - pathsArray[i * 8]) / height * 40f + 0.5f, 13));
            form.setResources(new PDResources());
            form.setMatrix(AffineTransform.getTranslateInstance(0.5f, 0.5f));
            cs.drawForm(form);
            PDFormContentStream formCS = null;
            try {
                formCS = new PDFormContentStream(form);
                PDTilingPattern pattern = new PDTilingPattern();
                pattern.setBBox(new PDRectangle(0, 0, 10, 12));
                pattern.setXStep(10);
                pattern.setYStep(13);
                pattern.setTilingType(PDTilingPattern.TILING_CONSTANT_SPACING_FASTER_TILING);
                pattern.setPaintType(PDTilingPattern.PAINT_UNCOLORED);
                PDPatternContentStream patternCS = null;
                try {
                    patternCS = new PDPatternContentStream(pattern);
                    // from Adobe
                    patternCS.setLineCapStyle(1);
                    patternCS.setLineJoinStyle(1);
                    patternCS.setLineWidth(1);
                    patternCS.setMiterLimit(10);
                    patternCS.moveTo(0, 1);
                    patternCS.lineTo(5, 11);
                    patternCS.lineTo(10, 1);
                    patternCS.stroke();
                } finally {
                    IOUtils.closeQuietly(patternCS);
                }
                COSName patternName = form.getResources().add(pattern);
                // PDColorSpace patternColorSpace = new PDPattern(null, PDDeviceRGB.INSTANCE);
                // PDColor patternColor = new PDColor(color.getComponents(), patternName, patternColorSpace);
                // formCS.setNonStrokingColor(patternColor); TODO: PdfBox-Android
                // With Adobe, the horizontal size is slightly different, don't know why
                formCS.addRect(0, 0, (pathsArray[i * 8 + 2] - pathsArray[i * 8]) / height * 40f, 12);
                formCS.fill();
            } finally {
                IOUtils.closeQuietly(formCS);
            }
        }
    } catch (IOException ex) {
        Log.e("PdfBox-Android", ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(cs);
    }
}
Also used : PDFormContentStream(com.tom_roush.pdfbox.pdmodel.PDFormContentStream) PDAnnotationTextMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup) PDPatternContentStream(com.tom_roush.pdfbox.pdmodel.PDPatternContentStream) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) IOException(java.io.IOException) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor) Matrix(com.tom_roush.pdfbox.util.Matrix) PDTilingPattern(com.tom_roush.pdfbox.pdmodel.graphics.pattern.PDTilingPattern) COSName(com.tom_roush.pdfbox.cos.COSName) PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 2 with PDAnnotationTextMarkup

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

the class PDStrikeoutAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationTextMarkup annotation = (PDAnnotationTextMarkup) 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 / 2, rect.getUpperRightX()));
    rect.setUpperRightY(Math.max(maxY + ab.width / 2, rect.getUpperRightY()));
    annotation.setRectangle(rect);
    PDAppearanceContentStream cs = null;
    try {
        cs = getNormalAppearanceAsContentStream();
        setOpacity(cs, 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, subtract 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];
            float y0 = pathsArray[i * 8 + 5];
            if (Float.compare(len0, 0) != 0) {
                // only if both coordinates are not identical to avoid divide by zero
                x0 += (pathsArray[i * 8] - pathsArray[i * 8 + 4]) / len0 * (len0 / 2 - ab.width);
                y0 += (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];
            float y1 = pathsArray[i * 8 + 7];
            if (Float.compare(len1, 0) != 0) {
                // only if both coordinates are not identical to avoid divide by zero
                x1 += (pathsArray[i * 8 + 2] - pathsArray[i * 8 + 6]) / len1 * (len1 / 2 - ab.width);
                y1 += (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.e("PdfBox-Android", ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(cs);
    }
}
Also used : PDAnnotationTextMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup) PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) IOException(java.io.IOException) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)

Example 3 with PDAnnotationTextMarkup

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

the class COSArrayListTest method setUp.

/*
    * Create thre new different annotations an add them to the Java List/Array as
    * well as PDFBox List/Array implementations.
    */
@Before
public void setUp() throws Exception {
    annotationsList = new ArrayList<PDAnnotation>();
    PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);
    PDAnnotationLink txtLink = new PDAnnotationLink();
    PDAnnotationSquareCircle aCircle = new PDAnnotationSquareCircle(PDAnnotationSquareCircle.SUB_TYPE_CIRCLE);
    annotationsList.add(txtMark);
    annotationsList.add(txtLink);
    annotationsList.add(aCircle);
    assertTrue(annotationsList.size() == 3);
    tbcAnnotationsList = new ArrayList<PDAnnotation>();
    tbcAnnotationsList.add(txtMark);
    tbcAnnotationsList.add(txtLink);
    tbcAnnotationsList.add(aCircle);
    assertTrue(tbcAnnotationsList.size() == 3);
    annotationsArray = new COSArray();
    annotationsArray.add(txtMark);
    annotationsArray.add(txtLink);
    annotationsArray.add(aCircle);
    assertTrue(annotationsArray.size() == 3);
    tbcAnnotationsArray = new COSBase[3];
    tbcAnnotationsArray[0] = txtMark.getCOSObject();
    tbcAnnotationsArray[1] = txtLink.getCOSObject();
    tbcAnnotationsArray[2] = aCircle.getCOSObject();
    assertTrue(tbcAnnotationsArray.length == 3);
    // add the annotations to the page
    pdPage = new PDPage();
    pdPage.setAnnotations(annotationsList);
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDAnnotationTextMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup) PDAnnotation(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation) PDAnnotationLink(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink) PDAnnotationSquareCircle(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquareCircle) Before(org.junit.Before)

Example 4 with PDAnnotationTextMarkup

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

the class PDHighlightAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationTextMarkup annotation = (PDAnnotationTextMarkup) 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);
    PDAppearanceContentStream cs = null;
    try {
        cs = getNormalAppearanceAsContentStream();
        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(createCOSStream());
        PDFormXObject frm2 = new PDFormXObject(createCOSStream());
        frm1.setResources(new PDResources());
        PDFormContentStream mwfofrmCS = null;
        try {
            mwfofrmCS = new PDFormContentStream(frm1);
            mwfofrmCS.drawForm(frm2);
        } finally {
            IOUtils.closeQuietly(mwfofrmCS);
        }
        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());
        PDFormContentStream frm2CS = null;
        try {
            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;
            }
        } finally {
            IOUtils.closeQuietly(frm2CS);
        }
    } catch (IOException ex) {
        Log.e("PdfBox-Android", ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(cs);
    }
}
Also used : PDFormContentStream(com.tom_roush.pdfbox.pdmodel.PDFormContentStream) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) PDExtendedGraphicsState(com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState) PDAnnotationTextMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) IOException(java.io.IOException) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor) PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 5 with PDAnnotationTextMarkup

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

the class PDUnderlineAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationTextMarkup annotation = (PDAnnotationTextMarkup) 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 / 2, rect.getUpperRightX()));
    rect.setUpperRightY(Math.max(maxY + ab.width / 2, rect.getUpperRightY()));
    annotation.setRectangle(rect);
    PDAppearanceContentStream cs = null;
    try {
        cs = getNormalAppearanceAsContentStream();
        setOpacity(cs, 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) {
            // Adobe doesn't use the lower coordinate for the line, it uses lower + delta / 7.
            // 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];
            float y0 = pathsArray[i * 8 + 5];
            if (Float.compare(len0, 0) != 0) {
                // only if both coordinates are not identical to avoid divide by zero
                x0 += (pathsArray[i * 8] - pathsArray[i * 8 + 4]) / len0 * len0 / 7;
                y0 += (pathsArray[i * 8 + 1] - pathsArray[i * 8 + 5]) / len0 * (len0 / 7);
            }
            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];
            float y1 = pathsArray[i * 8 + 7];
            if (Float.compare(len1, 0) != 0) {
                // only if both coordinates are not identical to avoid divide by zero
                x1 += (pathsArray[i * 8 + 2] - pathsArray[i * 8 + 6]) / len1 * len1 / 7;
                y1 += (pathsArray[i * 8 + 3] - pathsArray[i * 8 + 7]) / len1 * len1 / 7;
            }
            cs.moveTo(x0, y0);
            cs.lineTo(x1, y1);
        }
        cs.stroke();
    } catch (IOException ex) {
        Log.e("PdfBox-Android", ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(cs);
    }
}
Also used : PDAnnotationTextMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup) PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) IOException(java.io.IOException) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)

Aggregations

PDAnnotationTextMarkup (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup)5 PDAppearanceContentStream (com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream)4 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)4 PDColor (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)4 IOException (java.io.IOException)4 PDFormContentStream (com.tom_roush.pdfbox.pdmodel.PDFormContentStream)2 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)2 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)2 COSArray (com.tom_roush.pdfbox.cos.COSArray)1 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)1 COSName (com.tom_roush.pdfbox.cos.COSName)1 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)1 PDPatternContentStream (com.tom_roush.pdfbox.pdmodel.PDPatternContentStream)1 PDTilingPattern (com.tom_roush.pdfbox.pdmodel.graphics.pattern.PDTilingPattern)1 PDExtendedGraphicsState (com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState)1 PDAnnotation (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation)1 PDAnnotationLink (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink)1 PDAnnotationSquareCircle (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquareCircle)1 Matrix (com.tom_roush.pdfbox.util.Matrix)1 Before (org.junit.Before)1