Search in sources :

Example 1 with Matrix

use of com.tom_roush.pdfbox.util.Matrix in project PdfBox-Android by TomRoush.

the class PDAbstractContentStream method drawImage.

/**
 * Draw an inline image at the x,y coordinates and a certain width and height.
 *
 * @param inlineImage The inline image to draw.
 * @param x The x-coordinate to draw the inline image.
 * @param y The y-coordinate to draw the inline image.
 * @param width The width of the inline image to draw.
 * @param height The height of the inline image to draw.
 *
 * @throws IOException If there is an error writing to the stream.
 * @throws IllegalStateException If the method was called within a text block.
 */
public void drawImage(PDInlineImage inlineImage, float x, float y, float width, float height) throws IOException {
    if (inTextMode) {
        throw new IllegalStateException("Error: drawImage is not allowed within a text block.");
    }
    saveGraphicsState();
    transform(new Matrix(width, 0, 0, height, x, y));
    // create the image dictionary
    StringBuilder sb = new StringBuilder();
    sb.append(OperatorName.BEGIN_INLINE_IMAGE);
    sb.append("\n /W ");
    sb.append(inlineImage.getWidth());
    sb.append("\n /H ");
    sb.append(inlineImage.getHeight());
    sb.append("\n /CS ");
    sb.append("/");
    sb.append(inlineImage.getColorSpace().getName());
    if (inlineImage.getDecode() != null && inlineImage.getDecode().size() > 0) {
        sb.append("\n /D ");
        sb.append("[");
        for (COSBase base : inlineImage.getDecode()) {
            sb.append(((COSNumber) base).intValue());
            sb.append(" ");
        }
        sb.append("]");
    }
    if (inlineImage.isStencil()) {
        sb.append("\n /IM true");
    }
    sb.append("\n /BPC ");
    sb.append(inlineImage.getBitsPerComponent());
    // image dictionary
    write(sb.toString());
    writeLine();
    // binary data
    writeOperator(OperatorName.BEGIN_INLINE_IMAGE_DATA);
    writeBytes(inlineImage.getData());
    writeLine();
    writeOperator(OperatorName.END_INLINE_IMAGE);
    restoreGraphicsState();
}
Also used : Matrix(com.tom_roush.pdfbox.util.Matrix) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 2 with Matrix

use of com.tom_roush.pdfbox.util.Matrix in project PdfBox-Android by TomRoush.

the class LayerUtility method appendFormAsLayer.

/**
 * Places the given form over the existing content of the indicated page (like an overlay).
 * The form is enveloped in a marked content section to indicate that it's part of an
 * optional content group (OCG), here used as a layer. This optional group is returned and
 * can be enabled and disabled through methods on {@link PDOptionalContentProperties}.
 * <p>
 * You may want to call {@link #wrapInSaveRestore(PDPage) wrapInSaveRestore(PDPage)} before calling this method to make
 * sure that the graphics state is reset.
 *
 * @param targetPage the target page
 * @param form the form to place
 * @param transform the transformation matrix that controls the placement of your form. You'll
 * need this if your page has a crop box different than the media box, or if these have negative
 * coordinates, or if you want to scale or adjust your form.
 * @param layerName the name for the layer/OCG to produce
 * @return the optional content group that was generated for the form usage
 * @throws IOException if an I/O error occurs
 */
public PDOptionalContentGroup appendFormAsLayer(PDPage targetPage, PDFormXObject form, AffineTransform transform, String layerName) throws IOException {
    PDDocumentCatalog catalog = targetDoc.getDocumentCatalog();
    PDOptionalContentProperties ocprops = catalog.getOCProperties();
    if (ocprops == null) {
        ocprops = new PDOptionalContentProperties();
        catalog.setOCProperties(ocprops);
    }
    if (ocprops.hasGroup(layerName)) {
        throw new IllegalArgumentException("Optional group (layer) already exists: " + layerName);
    }
    PDRectangle cropBox = targetPage.getCropBox();
    if ((cropBox.getLowerLeftX() < 0 || cropBox.getLowerLeftY() < 0) && transform.isIdentity()) {
        // PDFBOX-4044
        Log.w("PdfBox-Android", "Negative cropBox " + cropBox + " and identity transform may make your form invisible");
    }
    PDOptionalContentGroup layer = new PDOptionalContentGroup(layerName);
    ocprops.addGroup(layer);
    PDPageContentStream contentStream = new PDPageContentStream(targetDoc, targetPage, AppendMode.APPEND, !DEBUG);
    contentStream.beginMarkedContent(COSName.OC, layer);
    contentStream.saveGraphicsState();
    contentStream.transform(new Matrix(transform));
    contentStream.drawForm(form);
    contentStream.restoreGraphicsState();
    contentStream.endMarkedContent();
    contentStream.close();
    return layer;
}
Also used : Matrix(com.tom_roush.pdfbox.util.Matrix) PDOptionalContentGroup(com.tom_roush.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentGroup) PDOptionalContentProperties(com.tom_roush.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentProperties) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)

Example 3 with Matrix

use of com.tom_roush.pdfbox.util.Matrix 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 4 with Matrix

use of com.tom_roush.pdfbox.util.Matrix in project PdfBox-Android by TomRoush.

the class SetMatrix method process.

@Override
public void process(Operator operator, List<COSBase> arguments) throws MissingOperandException {
    if (arguments.size() < 6) {
        throw new MissingOperandException(operator, arguments);
    }
    if (!checkArrayTypesClass(arguments, COSNumber.class)) {
        return;
    }
    COSNumber a = (COSNumber) arguments.get(0);
    COSNumber b = (COSNumber) arguments.get(1);
    COSNumber c = (COSNumber) arguments.get(2);
    COSNumber d = (COSNumber) arguments.get(3);
    COSNumber e = (COSNumber) arguments.get(4);
    COSNumber f = (COSNumber) arguments.get(5);
    Matrix matrix = new Matrix(a.floatValue(), b.floatValue(), c.floatValue(), d.floatValue(), e.floatValue(), f.floatValue());
    context.setTextMatrix(matrix);
    context.setTextLineMatrix(matrix.clone());
}
Also used : Matrix(com.tom_roush.pdfbox.util.Matrix) MissingOperandException(com.tom_roush.pdfbox.contentstream.operator.MissingOperandException) COSNumber(com.tom_roush.pdfbox.cos.COSNumber)

Example 5 with Matrix

use of com.tom_roush.pdfbox.util.Matrix in project PdfBox-Android by TomRoush.

the class BeginText method process.

@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException {
    context.setTextMatrix(new Matrix());
    context.setTextLineMatrix(new Matrix());
    context.beginText();
}
Also used : Matrix(com.tom_roush.pdfbox.util.Matrix)

Aggregations

Matrix (com.tom_roush.pdfbox.util.Matrix)31 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)11 AffineTransform (com.tom_roush.harmony.awt.geom.AffineTransform)9 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)9 PDGraphicsState (com.tom_roush.pdfbox.pdmodel.graphics.state.PDGraphicsState)7 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)3 COSBase (com.tom_roush.pdfbox.cos.COSBase)3 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)3 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)3 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)3 RectF (android.graphics.RectF)2 BoundingBox (com.tom_roush.fontbox.util.BoundingBox)2 COSName (com.tom_roush.pdfbox.cos.COSName)2 PDAppearanceContentStream (com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream)2 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)2 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)2 PDColor (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)2 PDAppearanceStream (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream)2 IOException (java.io.IOException)2 Paint (android.graphics.Paint)1