Search in sources :

Example 6 with PDPattern

use of org.apache.pdfbox.pdmodel.graphics.color.PDPattern in project pdfbox-graphics2d by rototor.

the class PdfBoxGraphics2D method applyShadingAsColor.

private void applyShadingAsColor(PDShading shading) throws IOException {
    /*
		 * If the paint has a shading we must create a tiling pattern and set that as
		 * stroke color...
		 */
    PDTilingPattern pattern = new PDTilingPattern();
    pattern.setPaintType(PDTilingPattern.PAINT_COLORED);
    pattern.setTilingType(PDTilingPattern.TILING_CONSTANT_SPACING_FASTER_TILING);
    PDRectangle anchorRect = bbox;
    pattern.setBBox(anchorRect);
    pattern.setXStep(anchorRect.getWidth());
    pattern.setYStep(anchorRect.getHeight());
    PDAppearanceStream appearance = new PDAppearanceStream(this.document);
    appearance.setResources(pattern.getResources());
    appearance.setBBox(pattern.getBBox());
    PDPageContentStream imageContentStream = new PDPageContentStream(document, appearance, ((COSStream) pattern.getCOSObject()).createOutputStream());
    imageContentStream.addRect(0, 0, anchorRect.getWidth(), anchorRect.getHeight());
    imageContentStream.clip();
    imageContentStream.shadingFill(shading);
    imageContentStream.close();
    PDColorSpace patternCS1 = new PDPattern(null);
    COSName tilingPatternName = xFormObject.getResources().add(pattern);
    PDColor patternColor = new PDColor(tilingPatternName, patternCS1);
    contentStream.setNonStrokingColor(patternColor);
    contentStream.setStrokingColor(patternColor);
}
Also used : PDTilingPattern(org.apache.pdfbox.pdmodel.graphics.pattern.PDTilingPattern) COSName(org.apache.pdfbox.cos.COSName) PDAppearanceStream(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream) PDPattern(org.apache.pdfbox.pdmodel.graphics.color.PDPattern) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) PDPageContentStream(org.apache.pdfbox.pdmodel.PDPageContentStream) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace) PDColor(org.apache.pdfbox.pdmodel.graphics.color.PDColor)

Example 7 with PDPattern

use of org.apache.pdfbox.pdmodel.graphics.color.PDPattern in project pdfbox by apache.

the class SCNToolTip method createMarkUp.

private void createMarkUp(PDResources resources, String colorSpaceName, String rowText) {
    PDColorSpace colorSpace = null;
    try {
        colorSpace = resources.getColorSpace(COSName.getPDFName(colorSpaceName));
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (colorSpace instanceof PDPattern) {
        setToolTipText("<html>Pattern</html>");
        return;
    }
    if (colorSpace != null) {
        try {
            float[] rgbValues = colorSpace.toRGB(extractColorValues(rowText));
            if (rgbValues != null) {
                Color color = new Color(rgbValues[0], rgbValues[1], rgbValues[2]);
                setToolTipText(getMarkUp(colorHexValue(color)));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : Color(java.awt.Color) PDPattern(org.apache.pdfbox.pdmodel.graphics.color.PDPattern) IOException(java.io.IOException) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 8 with PDPattern

use of org.apache.pdfbox.pdmodel.graphics.color.PDPattern in project pdfbox by apache.

the class SetColor method process.

@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException {
    PDColorSpace colorSpace = getColorSpace();
    if (!(colorSpace instanceof PDPattern)) {
        if (arguments.size() < colorSpace.getNumberOfComponents()) {
            throw new MissingOperandException(operator, arguments);
        }
        if (!checkArrayTypesClass(arguments, COSNumber.class)) {
            return;
        }
    }
    COSArray array = new COSArray();
    array.addAll(arguments);
    setColor(new PDColor(array, colorSpace));
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) MissingOperandException(org.apache.pdfbox.contentstream.operator.MissingOperandException) PDPattern(org.apache.pdfbox.pdmodel.graphics.color.PDPattern) COSNumber(org.apache.pdfbox.cos.COSNumber) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace) PDColor(org.apache.pdfbox.pdmodel.graphics.color.PDColor)

Example 9 with PDPattern

use of org.apache.pdfbox.pdmodel.graphics.color.PDPattern in project pdfbox by apache.

the class PageDrawer method intersectShadingBBox.

// checks whether this is a shading pattern and if yes,
// get the transformed BBox and intersect with current paint area
// need to do it here and not in shading getRaster() because it may have been rotated
private void intersectShadingBBox(PDColor color, Area area) throws IOException {
    if (color.getColorSpace() instanceof PDPattern) {
        PDColorSpace colorSpace = color.getColorSpace();
        PDAbstractPattern pat = ((PDPattern) colorSpace).getPattern(color);
        if (pat instanceof PDShadingPattern) {
            PDShading shading = ((PDShadingPattern) pat).getShading();
            PDRectangle bbox = shading.getBBox();
            if (bbox != null) {
                Matrix m = Matrix.concatenate(getInitialMatrix(), pat.getMatrix());
                Area bboxArea = new Area(bbox.transform(m));
                area.intersect(bboxArea);
            }
        }
    }
}
Also used : PDAbstractPattern(org.apache.pdfbox.pdmodel.graphics.pattern.PDAbstractPattern) Area(java.awt.geom.Area) PDShading(org.apache.pdfbox.pdmodel.graphics.shading.PDShading) Matrix(org.apache.pdfbox.util.Matrix) PDPattern(org.apache.pdfbox.pdmodel.graphics.color.PDPattern) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) PDShadingPattern(org.apache.pdfbox.pdmodel.graphics.pattern.PDShadingPattern) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 10 with PDPattern

use of org.apache.pdfbox.pdmodel.graphics.color.PDPattern in project pdfbox by apache.

the class PageDrawer method getPaint.

/**
 * Returns an AWT paint for the given PDColor.
 *
 * @param color The color to get a paint for. This can be an actual color or a pattern.
 * @throws IOException
 */
protected Paint getPaint(PDColor color) throws IOException {
    PDColorSpace colorSpace = color.getColorSpace();
    if (!(colorSpace instanceof PDPattern)) {
        float[] rgb = colorSpace.toRGB(color.getComponents());
        return new Color(clampColor(rgb[0]), clampColor(rgb[1]), clampColor(rgb[2]));
    } else {
        PDPattern patternSpace = (PDPattern) colorSpace;
        PDAbstractPattern pattern = patternSpace.getPattern(color);
        if (pattern instanceof PDTilingPattern) {
            PDTilingPattern tilingPattern = (PDTilingPattern) pattern;
            if (tilingPattern.getPaintType() == PDTilingPattern.PAINT_COLORED) {
                // colored tiling pattern
                return tilingPaintFactory.create(tilingPattern, null, null, xform);
            } else {
                // uncolored tiling pattern
                return tilingPaintFactory.create(tilingPattern, patternSpace.getUnderlyingColorSpace(), color, xform);
            }
        } else {
            PDShadingPattern shadingPattern = (PDShadingPattern) pattern;
            PDShading shading = shadingPattern.getShading();
            if (shading == null) {
                LOG.error("shadingPattern is null, will be filled with transparency");
                return new Color(0, 0, 0, 0);
            }
            return shading.toPaint(Matrix.concatenate(getInitialMatrix(), shadingPattern.getMatrix()));
        }
    }
}
Also used : PDAbstractPattern(org.apache.pdfbox.pdmodel.graphics.pattern.PDAbstractPattern) PDTilingPattern(org.apache.pdfbox.pdmodel.graphics.pattern.PDTilingPattern) PDShading(org.apache.pdfbox.pdmodel.graphics.shading.PDShading) Color(java.awt.Color) PDColor(org.apache.pdfbox.pdmodel.graphics.color.PDColor) PDPattern(org.apache.pdfbox.pdmodel.graphics.color.PDPattern) PDShadingPattern(org.apache.pdfbox.pdmodel.graphics.pattern.PDShadingPattern) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Aggregations

PDPattern (org.apache.pdfbox.pdmodel.graphics.color.PDPattern)10 PDColorSpace (org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)9 PDColor (org.apache.pdfbox.pdmodel.graphics.color.PDColor)6 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)5 PDTilingPattern (org.apache.pdfbox.pdmodel.graphics.pattern.PDTilingPattern)5 PDPageContentStream (org.apache.pdfbox.pdmodel.PDPageContentStream)4 AffineTransform (java.awt.geom.AffineTransform)3 Rectangle2D (java.awt.geom.Rectangle2D)3 PDAppearanceStream (org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream)3 Color (java.awt.Color)2 BufferedImage (java.awt.image.BufferedImage)2 IOException (java.io.IOException)2 COSName (org.apache.pdfbox.cos.COSName)2 PDImageXObject (org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject)2 PDAbstractPattern (org.apache.pdfbox.pdmodel.graphics.pattern.PDAbstractPattern)2 PDShadingPattern (org.apache.pdfbox.pdmodel.graphics.pattern.PDShadingPattern)2 PDShading (org.apache.pdfbox.pdmodel.graphics.shading.PDShading)2 Matrix (org.apache.pdfbox.util.Matrix)2 Graphics2D (java.awt.Graphics2D)1 Paint (java.awt.Paint)1