Search in sources :

Example 71 with PDRectangle

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

the class PDTextAppearanceHandler method drawInsert.

private void drawInsert(PDAnnotationText annotation, final PDAppearanceContentStream contentStream) throws IOException {
    PDRectangle bbox = adjustRectAndBBox(annotation, 17, 20);
    contentStream.setMiterLimit(4);
    contentStream.setLineJoinStyle(0);
    contentStream.setLineCapStyle(0);
    // value from Adobe
    contentStream.setLineWidth(0.59f);
    contentStream.moveTo(bbox.getWidth() / 2 - 1, bbox.getHeight() - 2);
    contentStream.lineTo(1, 1);
    contentStream.lineTo(bbox.getWidth() - 2, 1);
    contentStream.closeAndFillAndStroke();
}
Also used : PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 72 with PDRectangle

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

the class PDTextAppearanceHandler method drawParagraph.

private void drawParagraph(PDAnnotationText annotation, final PDAppearanceContentStream contentStream) throws IOException {
    PDRectangle bbox = adjustRectAndBBox(annotation, 20, 20);
    float min = Math.min(bbox.getWidth(), bbox.getHeight());
    contentStream.setMiterLimit(4);
    contentStream.setLineJoinStyle(1);
    contentStream.setLineCapStyle(0);
    // value from Adobe
    contentStream.setLineWidth(0.59f);
    // Adobe first fills a white circle with CA ca 0.6, so do we
    contentStream.saveGraphicsState();
    contentStream.setLineWidth(1);
    PDExtendedGraphicsState gs = new PDExtendedGraphicsState();
    gs.setAlphaSourceFlag(false);
    gs.setStrokingAlphaConstant(0.6f);
    gs.setNonStrokingAlphaConstant(0.6f);
    gs.setBlendMode(BlendMode.NORMAL);
    contentStream.setGraphicsStateParameters(gs);
    contentStream.setNonStrokingColor(1f);
    drawCircle2(contentStream, min / 2, min / 2, min / 2 - 1);
    contentStream.fill();
    contentStream.restoreGraphicsState();
    contentStream.saveGraphicsState();
    // rescale so that "?" fits into circle and move "?" to circle center
    // values gathered by trial and error
    contentStream.transform(Matrix.getScaleInstance(0.001f * min / 3, 0.001f * min / 3));
    contentStream.transform(Matrix.getTranslateInstance(850, 900));
    // we get the shape of an Helvetica "?" and use that one.
    // Adobe uses a different font (which one?), or created the shape from scratch.
    Path path = PDType1Font.HELVETICA.getPath("paragraph");
    addPath(contentStream, path);
    contentStream.restoreGraphicsState();
    contentStream.fillAndStroke();
    drawCircle(contentStream, min / 2, min / 2, min / 2 - 1);
    contentStream.stroke();
}
Also used : Path(android.graphics.Path) PDExtendedGraphicsState(com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 73 with PDRectangle

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

the class PDTextAppearanceHandler method drawRightArrow.

private void drawRightArrow(PDAnnotationText annotation, final PDAppearanceContentStream contentStream) throws IOException {
    PDRectangle bbox = adjustRectAndBBox(annotation, 20, 20);
    float min = Math.min(bbox.getWidth(), bbox.getHeight());
    contentStream.setMiterLimit(4);
    contentStream.setLineJoinStyle(1);
    contentStream.setLineCapStyle(0);
    // value from Adobe
    contentStream.setLineWidth(0.59f);
    // Adobe first fills a white circle with CA ca 0.6, so do we
    contentStream.saveGraphicsState();
    contentStream.setLineWidth(1);
    PDExtendedGraphicsState gs = new PDExtendedGraphicsState();
    gs.setAlphaSourceFlag(false);
    gs.setStrokingAlphaConstant(0.6f);
    gs.setNonStrokingAlphaConstant(0.6f);
    gs.setBlendMode(BlendMode.NORMAL);
    contentStream.setGraphicsStateParameters(gs);
    contentStream.setNonStrokingColor(1f);
    drawCircle2(contentStream, min / 2, min / 2, min / 2 - 1);
    contentStream.fill();
    contentStream.restoreGraphicsState();
    contentStream.saveGraphicsState();
    // rescale so that the glyph fits into circle and move it to circle center
    // values gathered by trial and error
    contentStream.transform(Matrix.getScaleInstance(0.001f * min / 1.3f, 0.001f * min / 1.3f));
    contentStream.transform(Matrix.getTranslateInstance(200, 300));
    // we get the shape of a Zapf Dingbats right arrow (0x2794) and use that one.
    // Adobe uses a different font (which one?), or created the shape from scratch.
    Path path = PDType1Font.ZAPF_DINGBATS.getPath("a160");
    addPath(contentStream, path);
    contentStream.restoreGraphicsState();
    // surprisingly, this one not counterclockwise.
    drawCircle(contentStream, min / 2, min / 2, min / 2 - 1);
    contentStream.fillAndStroke();
}
Also used : Path(android.graphics.Path) PDExtendedGraphicsState(com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 74 with PDRectangle

use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle 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)

Example 75 with PDRectangle

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

the class PDAnnotation method getRectangle.

/**
 * The annotation rectangle, defining the location of the annotation on the page in default user space units. This
 * is usually required and should not return null on valid PDF documents. But where this is a parent form field with
 * children, such as radio button collections then the rectangle will be null.
 *
 * @return The Rect value of this annotation.
 */
public PDRectangle getRectangle() {
    COSArray rectArray = (COSArray) dictionary.getDictionaryObject(COSName.RECT);
    PDRectangle rectangle = null;
    if (rectArray != null) {
        if (rectArray.size() == 4 && rectArray.getObject(0) instanceof COSNumber && rectArray.getObject(1) instanceof COSNumber && rectArray.getObject(2) instanceof COSNumber && rectArray.getObject(3) instanceof COSNumber) {
            rectangle = new PDRectangle(rectArray);
        } else {
            Log.w("PdfBox-Android", rectArray + " is not a rectangle array, returning null");
        }
    }
    return rectangle;
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Aggregations

PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)82 IOException (java.io.IOException)19 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)13 PDAppearanceContentStream (com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream)12 PDColor (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)11 Matrix (com.tom_roush.pdfbox.util.Matrix)11 COSArray (com.tom_roush.pdfbox.cos.COSArray)10 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)10 PDAppearanceStream (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream)9 Path (android.graphics.Path)8 AffineTransform (com.tom_roush.harmony.awt.geom.AffineTransform)7 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)6 COSBase (com.tom_roush.pdfbox.cos.COSBase)5 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)5 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)5 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)4 COSName (com.tom_roush.pdfbox.cos.COSName)4 PDExtendedGraphicsState (com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState)4 PDAnnotationTextMarkup (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup)4 PDAnnotationWidget (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)4