Search in sources :

Example 1 with PDAppearanceContentStream

use of com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream 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 PDAppearanceContentStream

use of com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream 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 PDAppearanceContentStream

use of com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream in project PdfBox-Android by TomRoush.

the class PDTextAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationText annotation = (PDAnnotationText) getAnnotation();
    if (!SUPPORTED_NAMES.contains(annotation.getName())) {
        return;
    }
    PDAppearanceContentStream contentStream = null;
    try {
        contentStream = getNormalAppearanceAsContentStream();
        PDColor bgColor = getColor();
        if (bgColor == null) {
            // White is used by Adobe when /C entry is missing
            contentStream.setNonStrokingColor(1f);
        } else {
            contentStream.setNonStrokingColor(bgColor);
        }
        // stroking color is always black which is the PDF default
        setOpacity(contentStream, annotation.getConstantOpacity());
        String annotationTypeName = annotation.getName();
        if (PDAnnotationText.NAME_NOTE.equals(annotationTypeName)) {
            drawNote(annotation, contentStream);
        } else if (PDAnnotationText.NAME_CROSS.equals(annotationTypeName)) {
            drawCross(annotation, contentStream);
        } else if (PDAnnotationText.NAME_CIRCLE.equals(annotationTypeName)) {
            drawCircles(annotation, contentStream);
        } else if (PDAnnotationText.NAME_INSERT.equals(annotationTypeName)) {
            drawInsert(annotation, contentStream);
        } else if (PDAnnotationText.NAME_HELP.equals(annotationTypeName)) {
            drawHelp(annotation, contentStream);
        } else if (PDAnnotationText.NAME_PARAGRAPH.equals(annotationTypeName)) {
            drawParagraph(annotation, contentStream);
        } else if (PDAnnotationText.NAME_NEW_PARAGRAPH.equals(annotationTypeName)) {
            drawNewParagraph(annotation, contentStream);
        } else if (PDAnnotationText.NAME_STAR.equals(annotationTypeName)) {
            drawStar(annotation, contentStream);
        } else if (PDAnnotationText.NAME_CHECK.equals(annotationTypeName)) {
            drawCheck(annotation, contentStream);
        } else if (PDAnnotationText.NAME_RIGHT_ARROW.equals(annotationTypeName)) {
            drawRightArrow(annotation, contentStream);
        } else if (PDAnnotationText.NAME_RIGHT_POINTER.equals(annotationTypeName)) {
            drawRightPointer(annotation, contentStream);
        } else if (PDAnnotationText.NAME_CROSS_HAIRS.equals(annotationTypeName)) {
            drawCrossHairs(annotation, contentStream);
        } else if (PDAnnotationText.NAME_UP_ARROW.equals(annotationTypeName)) {
            drawUpArrow(annotation, contentStream);
        } else if (PDAnnotationText.NAME_UP_LEFT_ARROW.equals(annotationTypeName)) {
            drawUpLeftArrow(annotation, contentStream);
        } else if (PDAnnotationText.NAME_COMMENT.equals(annotationTypeName)) {
            drawComment(annotation, contentStream);
        } else if (PDAnnotationText.NAME_KEY.equals(annotationTypeName)) {
            drawKey(annotation, contentStream);
        }
    } catch (IOException e) {
        Log.e("PdfBox-Android", e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(contentStream);
    }
}
Also used : PDAnnotationText(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationText) PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) IOException(java.io.IOException) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)

Example 4 with PDAppearanceContentStream

use of com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream in project PdfBox-Android by TomRoush.

the class PDAbstractAppearanceHandler method getAppearanceEntryAsContentStream.

private PDAppearanceContentStream getAppearanceEntryAsContentStream(PDAppearanceEntry appearanceEntry, boolean compress) throws IOException {
    PDAppearanceStream appearanceStream = appearanceEntry.getAppearanceStream();
    setTransformationMatrix(appearanceStream);
    // ensure there are resources
    PDResources resources = appearanceStream.getResources();
    if (resources == null) {
        resources = new PDResources();
        appearanceStream.setResources(resources);
    }
    return new PDAppearanceContentStream(appearanceStream, compress);
}
Also used : PDAppearanceStream(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream) PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources)

Example 5 with PDAppearanceContentStream

use of com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream in project PdfBox-Android by TomRoush.

the class PDInkAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationMarkup ink = (PDAnnotationMarkup) getAnnotation();
    // PDF spec does not mention /Border for ink annotations, but it is used if /BS is not available
    AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(ink, ink.getBorderStyle());
    PDColor color = ink.getColor();
    if (color == null || color.getComponents().length == 0 || Float.compare(ab.width, 0) == 0) {
        return;
    }
    PDAppearanceContentStream cs = null;
    try {
        cs = getNormalAppearanceAsContentStream();
        setOpacity(cs, ink.getConstantOpacity());
        cs.setStrokingColor(color);
        if (ab.dashArray != null) {
            cs.setLineDashPattern(ab.dashArray, 0);
        }
        cs.setLineWidth(ab.width);
        for (float[] pathArray : ink.getInkList()) {
            int nPoints = pathArray.length / 2;
            // in an implementation-dependent way" - we do lines.
            for (int i = 0; i < nPoints; ++i) {
                float x = pathArray[i * 2];
                float y = pathArray[i * 2 + 1];
                if (i == 0) {
                    cs.moveTo(x, y);
                } else {
                    cs.lineTo(x, y);
                }
            }
            cs.stroke();
        }
    } catch (IOException ex) {
        Log.e("PdfBox-Android", ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(cs);
    }
}
Also used : PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PDAnnotationMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup) IOException(java.io.IOException) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)

Aggregations

PDAppearanceContentStream (com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream)15 IOException (java.io.IOException)14 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)12 PDColor (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)10 PDAnnotationMarkup (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup)5 PDAppearanceStream (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream)5 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)4 PDAnnotationTextMarkup (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup)4 PDBorderEffectDictionary (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderEffectDictionary)4 PDFormContentStream (com.tom_roush.pdfbox.pdmodel.PDFormContentStream)2 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)2 PDAnnotationSquareCircle (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquareCircle)2 Matrix (com.tom_roush.pdfbox.util.Matrix)2 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)1 COSName (com.tom_roush.pdfbox.cos.COSName)1 PDPatternContentStream (com.tom_roush.pdfbox.pdmodel.PDPatternContentStream)1 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)1 PDType1Font (com.tom_roush.pdfbox.pdmodel.font.PDType1Font)1 PDTilingPattern (com.tom_roush.pdfbox.pdmodel.graphics.pattern.PDTilingPattern)1 PDExtendedGraphicsState (com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState)1