Search in sources :

Example 16 with Matrix

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

the class PDPageContentStream method drawImage.

/**
 * Draw an image at the origin with the given transformation matrix.
 *
 * @param image The image to draw.
 * @param matrix The transformation matrix to apply to the image.
 *
 * @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(PDImageXObject image, Matrix matrix) throws IOException {
    if (inTextMode) {
        throw new IllegalStateException("Error: drawImage is not allowed within a text block.");
    }
    saveGraphicsState();
    AffineTransform transform = matrix.createAffineTransform();
    transform(new Matrix(transform));
    writeOperand(resources.add(image));
    writeOperator(OperatorName.DRAW_OBJECT);
    restoreGraphicsState();
}
Also used : Matrix(com.tom_roush.pdfbox.util.Matrix) AffineTransform(com.tom_roush.harmony.awt.geom.AffineTransform)

Example 17 with Matrix

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

the class PDPageContentStream method drawXObject.

/**
 * Draw an xobject(form or image) using the given {@link AffineTransform} to position
 * the xobject.
 *
 * @param xobject The xobject to draw.
 * @param transform the transformation matrix
 * @throws IOException If there is an error writing to the stream.
 * @throws IllegalStateException If the method was called within a text block.
 * @deprecated Use {@link #drawImage(PDImageXObject, Matrix) drawImage(PDImageXObject, Matrix)}
 * or {@link #drawForm(PDFormXObject) drawForm(PDFormXObject)} with
 * {@link #transform(Matrix) transform(Matrix)} instead.
 */
@Deprecated
public void drawXObject(PDXObject xobject, AffineTransform transform) throws IOException {
    if (inTextMode) {
        throw new IllegalStateException("Error: drawXObject is not allowed within a text block.");
    }
    String xObjectPrefix;
    if (xobject instanceof PDImageXObject) {
        xObjectPrefix = "Im";
    } else {
        xObjectPrefix = "Form";
    }
    COSName objMapping = resources.add(xobject, xObjectPrefix);
    saveGraphicsState();
    transform(new Matrix(transform));
    writeOperand(objMapping);
    writeOperator(OperatorName.DRAW_OBJECT);
    restoreGraphicsState();
}
Also used : PDImageXObject(com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject) Matrix(com.tom_roush.pdfbox.util.Matrix) COSName(com.tom_roush.pdfbox.cos.COSName)

Example 18 with Matrix

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

the class PDPageContentStream 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 19 with Matrix

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

the class AppearanceGeneratorHelper method prepareNormalAppearanceStream.

private PDAppearanceStream prepareNormalAppearanceStream(PDAnnotationWidget widget) {
    PDAppearanceStream appearanceStream = new PDAppearanceStream(field.getAcroForm().getDocument());
    // Calculate the entries for the bounding box and the transformation matrix
    // settings for the appearance stream
    int rotation = resolveRotation(widget);
    PDRectangle rect = widget.getRectangle();
    Matrix matrix = Matrix.getRotateInstance(Math.toRadians(rotation), 0, 0);
    PointF point2D = matrix.transformPoint(rect.getWidth(), rect.getHeight());
    PDRectangle bbox = new PDRectangle(Math.abs((float) point2D.x), Math.abs((float) point2D.y));
    appearanceStream.setBBox(bbox);
    AffineTransform at = calculateMatrix(bbox, rotation);
    if (!at.isIdentity()) {
        appearanceStream.setMatrix(at);
    }
    appearanceStream.setFormType(1);
    appearanceStream.setResources(new PDResources());
    return appearanceStream;
}
Also used : Matrix(com.tom_roush.pdfbox.util.Matrix) PDAppearanceStream(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream) PointF(android.graphics.PointF) AffineTransform(com.tom_roush.harmony.awt.geom.AffineTransform) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 20 with Matrix

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

the class TestLayerUtility method createOverlay1.

private File createOverlay1() throws IOException {
    File targetFile = new File(testResultsDir, "overlay1.pdf");
    PDDocument doc = new PDDocument();
    try {
        // Create new page
        PDPage page = new PDPage();
        doc.addPage(page);
        PDResources resources = page.getResources();
        if (resources == null) {
            resources = new PDResources();
            page.setResources(resources);
        }
        // Setup page content stream and paint background/title
        PDPageContentStream contentStream = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.OVERWRITE, false);
        PDFont font = PDType1Font.HELVETICA_BOLD;
        contentStream.setNonStrokingColor(AWTColor.LIGHT_GRAY);
        contentStream.beginText();
        float fontSize = 96;
        contentStream.setFont(font, fontSize);
        String text = "OVERLAY";
        // float sw = font.getStringWidth(text);
        // Too bad, base 14 fonts don't return character metrics.
        PDRectangle crop = page.getCropBox();
        float cx = crop.getWidth() / 2f;
        float cy = crop.getHeight() / 2f;
        Matrix transform = new Matrix();
        transform.translate(cx, cy);
        transform.rotate(Math.toRadians(45));
        transform.translate(-190, /* sw/2 */
        0);
        contentStream.setTextMatrix(transform);
        contentStream.showText(text);
        contentStream.endText();
        contentStream.close();
        doc.save(targetFile.getAbsolutePath());
    } finally {
        doc.close();
    }
    return targetFile;
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) Matrix(com.tom_roush.pdfbox.util.Matrix) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) File(java.io.File)

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