Search in sources :

Example 1 with PDTransparencyGroup

use of org.apache.pdfbox.pdmodel.graphics.form.PDTransparencyGroup in project pdfbox by apache.

the class PDXObject method createXObject.

/**
 * Creates a new XObject instance of the appropriate type for the COS stream.
 *
 * @param base The stream which is wrapped by this XObject.
 * @param resources
 * @return A new XObject instance.
 * @throws java.io.IOException if there is an error creating the XObject.
 */
public static PDXObject createXObject(COSBase base, PDResources resources) throws IOException {
    if (base == null) {
        // TODO throw an exception?
        return null;
    }
    if (!(base instanceof COSStream)) {
        throw new IOException("Unexpected object type: " + base.getClass().getName());
    }
    COSStream stream = (COSStream) base;
    String subtype = stream.getNameAsString(COSName.SUBTYPE);
    if (COSName.IMAGE.getName().equals(subtype)) {
        return new PDImageXObject(new PDStream(stream), resources);
    } else if (COSName.FORM.getName().equals(subtype)) {
        ResourceCache cache = resources != null ? resources.getResourceCache() : null;
        COSDictionary group = (COSDictionary) stream.getDictionaryObject(COSName.GROUP);
        if (group != null && COSName.TRANSPARENCY.equals(group.getCOSName(COSName.S))) {
            return new PDTransparencyGroup(stream, cache);
        }
        return new PDFormXObject(stream, cache);
    } else if (COSName.PS.getName().equals(subtype)) {
        return new PDPostScriptXObject(stream);
    } else {
        throw new IOException("Invalid XObject Subtype: " + subtype);
    }
}
Also used : PDTransparencyGroup(org.apache.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) COSStream(org.apache.pdfbox.cos.COSStream) PDImageXObject(org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject) COSDictionary(org.apache.pdfbox.cos.COSDictionary) PDFormXObject(org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject) IOException(java.io.IOException) PDStream(org.apache.pdfbox.pdmodel.common.PDStream) ResourceCache(org.apache.pdfbox.pdmodel.ResourceCache)

Example 2 with PDTransparencyGroup

use of org.apache.pdfbox.pdmodel.graphics.form.PDTransparencyGroup in project pdfbox by apache.

the class DrawObject method process.

@Override
public void process(Operator operator, List<COSBase> operands) throws IOException {
    if (operands.size() < 1) {
        throw new MissingOperandException(operator, operands);
    }
    COSBase base0 = operands.get(0);
    if (!(base0 instanceof COSName)) {
        return;
    }
    COSName objectName = (COSName) base0;
    PDXObject xobject = context.getResources().getXObject(objectName);
    if (xobject == null) {
        throw new MissingResourceException("Missing XObject: " + objectName.getName());
    } else if (xobject instanceof PDImageXObject) {
        PDImageXObject image = (PDImageXObject) xobject;
        context.drawImage(image);
    } else if (xobject instanceof PDTransparencyGroup) {
        getContext().showTransparencyGroup((PDTransparencyGroup) xobject);
    } else if (xobject instanceof PDFormXObject) {
        getContext().showForm((PDFormXObject) xobject);
    }
}
Also used : PDTransparencyGroup(org.apache.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) PDImageXObject(org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject) COSName(org.apache.pdfbox.cos.COSName) MissingOperandException(org.apache.pdfbox.contentstream.operator.MissingOperandException) MissingResourceException(org.apache.pdfbox.pdmodel.MissingResourceException) COSBase(org.apache.pdfbox.cos.COSBase) PDFormXObject(org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject) PDXObject(org.apache.pdfbox.pdmodel.graphics.PDXObject)

Example 3 with PDTransparencyGroup

use of org.apache.pdfbox.pdmodel.graphics.form.PDTransparencyGroup in project pdfbox by apache.

the class DrawObject method process.

@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException {
    if (arguments.size() < 1) {
        throw new MissingOperandException(operator, arguments);
    }
    COSBase base0 = arguments.get(0);
    if (!(base0 instanceof COSName)) {
        return;
    }
    COSName name = (COSName) base0;
    if (context.getResources().isImageXObject(name)) {
        // we're done here, don't decode images when doing text extraction
        return;
    }
    PDXObject xobject = context.getResources().getXObject(name);
    if (xobject instanceof PDTransparencyGroup) {
        context.showTransparencyGroup((PDTransparencyGroup) xobject);
    } else if (xobject instanceof PDFormXObject) {
        PDFormXObject form = (PDFormXObject) xobject;
        context.showForm(form);
    }
}
Also used : PDTransparencyGroup(org.apache.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) COSName(org.apache.pdfbox.cos.COSName) COSBase(org.apache.pdfbox.cos.COSBase) PDFormXObject(org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject) PDXObject(org.apache.pdfbox.pdmodel.graphics.PDXObject)

Example 4 with PDTransparencyGroup

use of org.apache.pdfbox.pdmodel.graphics.form.PDTransparencyGroup in project pdfbox by apache.

the class DrawObject method process.

@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException {
    if (arguments.size() < 1) {
        throw new MissingOperandException(operator, arguments);
    }
    COSBase base0 = arguments.get(0);
    if (!(base0 instanceof COSName)) {
        return;
    }
    COSName name = (COSName) base0;
    PDXObject xobject = context.getResources().getXObject(name);
    ((PDFMarkedContentExtractor) context).xobject(xobject);
    if (xobject instanceof PDTransparencyGroup) {
        context.showTransparencyGroup((PDTransparencyGroup) xobject);
    } else if (xobject instanceof PDFormXObject) {
        PDFormXObject form = (PDFormXObject) xobject;
        context.showForm(form);
    }
}
Also used : PDTransparencyGroup(org.apache.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) COSName(org.apache.pdfbox.cos.COSName) MissingOperandException(org.apache.pdfbox.contentstream.operator.MissingOperandException) PDFMarkedContentExtractor(org.apache.pdfbox.text.PDFMarkedContentExtractor) COSBase(org.apache.pdfbox.cos.COSBase) PDFormXObject(org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject) PDXObject(org.apache.pdfbox.pdmodel.graphics.PDXObject)

Example 5 with PDTransparencyGroup

use of org.apache.pdfbox.pdmodel.graphics.form.PDTransparencyGroup in project pdfbox by apache.

the class PageDrawer method showTransparencyGroup.

@Override
public void showTransparencyGroup(PDTransparencyGroup form) throws IOException {
    TransparencyGroup group = new TransparencyGroup(form, false, getGraphicsState().getCurrentTransformationMatrix(), null);
    BufferedImage image = group.getImage();
    if (image == null) {
        // image is empty, don't bother
        return;
    }
    graphics.setComposite(getGraphicsState().getNonStrokingJavaComposite());
    setClip();
    // both the DPI xform and the CTM were already applied to the group, so all we do
    // here is draw it directly onto the Graphics2D device at the appropriate position
    PDRectangle bbox = group.getBBox();
    AffineTransform prev = graphics.getTransform();
    Matrix m = new Matrix(xform);
    float xScale = Math.abs(m.getScalingFactorX());
    float yScale = Math.abs(m.getScalingFactorY());
    // adjust the initial translation (includes the translation used to "help" the rotation)
    graphics.setTransform(AffineTransform.getTranslateInstance(xform.getTranslateX(), xform.getTranslateY()));
    graphics.rotate(Math.toRadians(pageRotation));
    // adjust bbox (x,y) position at the initial scale + cropbox
    float x = bbox.getLowerLeftX() - pageSize.getLowerLeftX();
    float y = pageSize.getUpperRightY() - bbox.getUpperRightY();
    if (flipTG) {
        graphics.translate(0, image.getHeight());
        graphics.scale(1, -1);
    } else {
        graphics.translate(x * xScale, y * yScale);
    }
    PDSoftMask softMask = getGraphicsState().getSoftMask();
    if (softMask != null) {
        Paint awtPaint = new TexturePaint(image, new Rectangle2D.Float(0, 0, image.getWidth(), image.getHeight()));
        awtPaint = applySoftMaskToPaint(awtPaint, softMask);
        graphics.setPaint(awtPaint);
        graphics.fill(new Rectangle2D.Float(0, 0, bbox.getWidth() * xScale, bbox.getHeight() * yScale));
    } else {
        graphics.drawImage(image, null, null);
    }
    graphics.setTransform(prev);
}
Also used : PDSoftMask(org.apache.pdfbox.pdmodel.graphics.state.PDSoftMask) TexturePaint(java.awt.TexturePaint) PDTransparencyGroup(org.apache.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) Matrix(org.apache.pdfbox.util.Matrix) Rectangle2D(java.awt.geom.Rectangle2D) AffineTransform(java.awt.geom.AffineTransform) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) Paint(java.awt.Paint) TexturePaint(java.awt.TexturePaint) BufferedImage(java.awt.image.BufferedImage)

Aggregations

PDTransparencyGroup (org.apache.pdfbox.pdmodel.graphics.form.PDTransparencyGroup)5 PDFormXObject (org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject)4 COSBase (org.apache.pdfbox.cos.COSBase)3 COSName (org.apache.pdfbox.cos.COSName)3 PDXObject (org.apache.pdfbox.pdmodel.graphics.PDXObject)3 MissingOperandException (org.apache.pdfbox.contentstream.operator.MissingOperandException)2 PDImageXObject (org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject)2 Paint (java.awt.Paint)1 TexturePaint (java.awt.TexturePaint)1 AffineTransform (java.awt.geom.AffineTransform)1 Rectangle2D (java.awt.geom.Rectangle2D)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 COSDictionary (org.apache.pdfbox.cos.COSDictionary)1 COSStream (org.apache.pdfbox.cos.COSStream)1 MissingResourceException (org.apache.pdfbox.pdmodel.MissingResourceException)1 ResourceCache (org.apache.pdfbox.pdmodel.ResourceCache)1 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)1 PDStream (org.apache.pdfbox.pdmodel.common.PDStream)1 PDSoftMask (org.apache.pdfbox.pdmodel.graphics.state.PDSoftMask)1