org.apache.pdfbox.pdmodel.graphics.color.PDColor Java Examples

The following examples show how to use org.apache.pdfbox.pdmodel.graphics.color.PDColor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PDAppearanceCharacteristicsDictionary.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private PDColor getColor(COSName itemName)
{
    COSBase c = this.getCOSObject().getItem(itemName);
    if (c instanceof COSArray)
    {
        PDColorSpace colorSpace;
        switch (((COSArray) c).size())
        {
        case 1:
            colorSpace = PDDeviceGray.INSTANCE;
            break;
        case 3:
            colorSpace = PDDeviceRGB.INSTANCE;
            break;
        case 4:
            colorSpace = PDDeviceCMYK.INSTANCE;
            break;
        default:
            return null;
        }
        return new PDColor((COSArray) c, colorSpace);
    }
    return null;
}
 
Example #2
Source File: PageDrawer.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
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);
            }
        }
    }
}
 
Example #3
Source File: TilingPaintFactory.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
Paint create(PDTilingPattern pattern, PDColorSpace colorSpace,
        PDColor color, AffineTransform xform) throws IOException
{
    Paint paint = null;
    TilingPaintParameter tilingPaintParameter
            = new TilingPaintParameter(drawer.getInitialMatrix(), pattern.getCOSObject(), colorSpace, color, xform);
    WeakReference<Paint> weakRef = weakCache.get(tilingPaintParameter);
    if (weakRef != null)
    {
        // PDFBOX-4058: additional WeakReference makes gc work better
        paint = weakRef.get();
    }
    if (paint == null)
    {
        paint = new TilingPaint(drawer, pattern, colorSpace, color, xform);
        weakCache.put(tilingPaintParameter, new WeakReference<Paint>(paint));
    }
    return paint;
}
 
Example #4
Source File: SetColor.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@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));
}
 
Example #5
Source File: PDAnnotation.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
protected PDColor getColor(COSName itemName)
{
    COSBase c = this.getCOSObject().getItem(itemName);
    if (c instanceof COSArray)
    {
        PDColorSpace colorSpace = null;
        switch (((COSArray) c).size())
        {
        case 1:
            colorSpace = PDDeviceGray.INSTANCE;
            break;
        case 3:
            colorSpace = PDDeviceRGB.INSTANCE;
            break;
        case 4:
            colorSpace = PDDeviceCMYK.INSTANCE;
            break;
        default:
            break;
        }
        return new PDColor((COSArray) c, colorSpace);
    }
    return null;
}
 
Example #6
Source File: PDDefaultAppearanceString.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Process the font color operator.
 * 
 * This is assumed to be an RGB color.
 * 
 * @param operands the color components
 * @throws IOException in case of the color components not matching
 */
private void processSetFontColor(List<COSBase> operands) throws IOException
{
    PDColorSpace colorSpace;

    switch (operands.size())
    {
        case 1:
            colorSpace = PDDeviceGray.INSTANCE;
            break;
        case 3:
            colorSpace = PDDeviceRGB.INSTANCE;
            break;
        case 4:
            colorSpace = PDDeviceCMYK.INSTANCE;
            break;
        default:
            throw new IOException("Missing operands for set non stroking color operator " + Arrays.toString(operands.toArray()));
    }
    COSArray array = new COSArray();
    array.addAll(operands);
    setFontColor(new PDColor(array, colorSpace));
}
 
Example #7
Source File: TilingPaintFactory.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private TilingPaintParameter(Matrix matrix, COSDictionary patternDict, PDColorSpace colorSpace,
        PDColor color, AffineTransform xform)
{
    this.matrix = matrix.clone();
    this.patternDict = patternDict;
    this.colorSpace = colorSpace;
    this.color = color;
    this.xform = xform;
}
 
Example #8
Source File: PDPageContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Sets the non-stroking color and, if necessary, the non-stroking color space.
 *
 * @param color Color in a specific color space.
 * @throws IOException If an IO error occurs while writing to the stream.
 */
public void setNonStrokingColor(PDColor color) throws IOException
{
    if (nonStrokingColorSpaceStack.isEmpty() ||
        nonStrokingColorSpaceStack.peek() != color.getColorSpace())
    {
        writeOperand(getName(color.getColorSpace()));
        writeOperator(OperatorName.NON_STROKING_COLORSPACE);
        setNonStrokingColorSpaceStack(color.getColorSpace());
    }

    for (float value : color.getComponents())
    {
        writeOperand(value);
    }

    if (color.getColorSpace() instanceof PDPattern)
    {
        writeOperand(color.getPatternName());
    }

    if (color.getColorSpace() instanceof PDPattern ||
        color.getColorSpace() instanceof PDSeparation ||
        color.getColorSpace() instanceof PDDeviceN ||
        color.getColorSpace() instanceof PDICCBased)
    {
        writeOperator(OperatorName.NON_STROKING_COLOR_N);
    }
    else
    {
        writeOperator(OperatorName.NON_STROKING_COLOR);
    }
}
 
Example #9
Source File: ExtractImageLocations.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
@Override
protected void processOperator(Operator operator, List<COSBase> operands) throws IOException {
    String operation = operator.getName();
    if (fillOperations.contains(operation)) {
        PDColor color = getGraphicsState().getNonStrokingColor();
        PDAbstractPattern pattern = getResources().getPattern(color.getPatternName());
        if (pattern instanceof PDTilingPattern) {
            processTilingPattern((PDTilingPattern) pattern, null, null);
        }
    }
    super.processOperator(operator, operands);
}
 
Example #10
Source File: SoftMask.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a new soft mask paint.
 *
 * @param paint underlying paint.
 * @param mask soft mask
 * @param bboxDevice bbox of the soft mask in the underlying Graphics2D device space
 * @param backdropColor the color to be used outside the transparency group’s bounding box; if
 * null, black will be used.
 * @param transferFunction the transfer function, may be null.
 */
SoftMask(Paint paint, BufferedImage mask, Rectangle2D bboxDevice, PDColor backdropColor, PDFunction transferFunction)
{
    this.paint = paint;
    this.mask = mask;
    this.bboxDevice = bboxDevice;
    if (transferFunction instanceof PDFunctionTypeIdentity)
    {
        this.transferFunction = null;
    }
    else
    {
        this.transferFunction = transferFunction;
    }
    if (backdropColor != null)
    {
        try
        {
            Color color = new Color(backdropColor.toRGB());
            // http://stackoverflow.com/a/25463098/535646
            bc = (299 * color.getRed() + 587 * color.getGreen() + 114 * color.getBlue()) / 1000;
        }
        catch (IOException ex)
        {
            // keep default
        }
    }
}
 
Example #11
Source File: PdfToTextInfoConverter.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
private Integer getCharacterBackgroundColor(TextPosition text) {
    Integer fillColorRgb = null;
    try {           
        for (Map.Entry<GeneralPath, PDColor> filledPath : filledPaths.entrySet()) {
            Vector center = getTextPositionCenterPoint(text);
            if (filledPath.getKey().contains(lowerLeftX + center.getX(), lowerLeftY + center.getY())) {
                fillColorRgb = filledPath.getValue().toRGB();                   
            }
        }
    } catch (IOException e) {
        logger.error("Could not convert color to RGB", e);
    }
    return fillColorRgb;
}
 
Example #12
Source File: PdfBoxFinder.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether the given color is black'ish.
 */
boolean isBlack(PDColor color) throws IOException {
    int value = color.toRGB();
    for (int i = 0; i < 2; i++) {
        int component = value & 0xff;
        if (component > 5)
            return false;
        value /= 256;
    }
    return true;
}
 
Example #13
Source File: PDAbstractContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Set the stroking color using an AWT color. Conversion uses the default sRGB color space.
 *
 * @param color The color to set.
 * @throws IOException If an IO error occurs while writing to the stream.
 */
public void setStrokingColor(Color color) throws IOException
{
    float[] components = new float[] {
            color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f };
    PDColor pdColor = new PDColor(components, PDDeviceRGB.INSTANCE);
    setStrokingColor(pdColor);
}
 
Example #14
Source File: PDAbstractContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Sets the non-stroking color and, if necessary, the non-stroking color space.
 *
 * @param color Color in a specific color space.
 * @throws IOException If an IO error occurs while writing to the stream.
 */
public void setNonStrokingColor(PDColor color) throws IOException
{
    if (nonStrokingColorSpaceStack.isEmpty() ||
        nonStrokingColorSpaceStack.peek() != color.getColorSpace())
    {
        writeOperand(getName(color.getColorSpace()));
        writeOperator(OperatorName.NON_STROKING_COLORSPACE);
        setNonStrokingColorSpaceStack(color.getColorSpace());
    }

    for (float value : color.getComponents())
    {
        writeOperand(value);
    }

    if (color.getColorSpace() instanceof PDPattern)
    {
        writeOperand(color.getPatternName());
    }

    if (color.getColorSpace() instanceof PDPattern ||
        color.getColorSpace() instanceof PDSeparation ||
        color.getColorSpace() instanceof PDDeviceN ||
        color.getColorSpace() instanceof PDICCBased)
    {
        writeOperator(OperatorName.NON_STROKING_COLOR_N);
    }
    else
    {
        writeOperator(OperatorName.NON_STROKING_COLOR);
    }
}
 
Example #15
Source File: PDAbstractContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Set the non-stroking color using an AWT color. Conversion uses the default sRGB color space.
 *
 * @param color The color to set.
 * @throws IOException If an IO error occurs while writing to the stream.
 */
public void setNonStrokingColor(Color color) throws IOException
{
    float[] components = new float[] {
            color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f };
    PDColor pdColor = new PDColor(components, PDDeviceRGB.INSTANCE);
    setNonStrokingColor(pdColor);
}
 
Example #16
Source File: PDAbstractContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Sets the stroking color and, if necessary, the stroking color space.
 *
 * @param color Color in a specific color space.
 * @throws IOException If an IO error occurs while writing to the stream.
 */
public void setStrokingColor(PDColor color) throws IOException
{
    if (strokingColorSpaceStack.isEmpty() ||
        strokingColorSpaceStack.peek() != color.getColorSpace())
    {
        writeOperand(getName(color.getColorSpace()));
        writeOperator(OperatorName.STROKING_COLORSPACE);
        setStrokingColorSpaceStack(color.getColorSpace());
    }

    for (float value : color.getComponents())
    {
        writeOperand(value);
    }

    if (color.getColorSpace() instanceof PDPattern)
    {
        writeOperand(color.getPatternName());
    }

    if (color.getColorSpace() instanceof PDPattern ||
        color.getColorSpace() instanceof PDSeparation ||
        color.getColorSpace() instanceof PDDeviceN ||
        color.getColorSpace() instanceof PDICCBased)
    {
        writeOperator(OperatorName.STROKING_COLOR_N);
    }
    else
    {
        writeOperator(OperatorName.STROKING_COLOR);
    }
}
 
Example #17
Source File: PDPageContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Set the stroking color using an AWT color. Conversion uses the default sRGB color space.
 *
 * @param color The color to set.
 * @throws IOException If an IO error occurs while writing to the stream.
 */
public void setStrokingColor(Color color) throws IOException
{
    float[] components = new float[] {
            color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f };
    PDColor pdColor = new PDColor(components, PDDeviceRGB.INSTANCE);
    setStrokingColor(pdColor);
}
 
Example #18
Source File: PDPageContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Sets the stroking color and, if necessary, the stroking color space.
 *
 * @param color Color in a specific color space.
 * @throws IOException If an IO error occurs while writing to the stream.
 */
public void setStrokingColor(PDColor color) throws IOException
{
    if (strokingColorSpaceStack.isEmpty() ||
        strokingColorSpaceStack.peek() != color.getColorSpace())
    {
        writeOperand(getName(color.getColorSpace()));
        writeOperator(OperatorName.STROKING_COLORSPACE);
        setStrokingColorSpaceStack(color.getColorSpace());
    }

    for (float value : color.getComponents())
    {
        writeOperand(value);
    }

    if (color.getColorSpace() instanceof PDPattern)
    {
        writeOperand(color.getPatternName());
    }

    if (color.getColorSpace() instanceof PDPattern ||
        color.getColorSpace() instanceof PDSeparation ||
        color.getColorSpace() instanceof PDDeviceN ||
        color.getColorSpace() instanceof PDICCBased)
    {
        writeOperator(OperatorName.STROKING_COLOR_N);
    }
    else
    {
        writeOperator(OperatorName.STROKING_COLOR);
    }
}
 
Example #19
Source File: PDPageContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Set the non-stroking color using an AWT color. Conversion uses the default sRGB color space.
 *
 * @param color The color to set.
 * @throws IOException If an IO error occurs while writing to the stream.
 */
public void setNonStrokingColor(Color color) throws IOException
{
    float[] components = new float[] {
            color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f };
    PDColor pdColor = new PDColor(components, PDDeviceRGB.INSTANCE);
    setNonStrokingColor(pdColor);
}
 
Example #20
Source File: PDBoxStyle.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Set the color space instance for this box style.  This must be a
 * PDDeviceRGB!
 *
 * @param color The new colorspace value.
 */
public void setGuideLineColor( PDColor color )
{
    COSArray values = null;
    if( color != null )
    {
        values = color.toCOSArray();
    }
    dictionary.setItem(COSName.C, values);
}
 
Example #21
Source File: PdfBoxFinder.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * Processes the path elements currently in the {@link #path} list and
 * eventually clears the list.
 * </p>
 * <p>
 * Currently only elements are considered which 
 * </p>
 * <ul>
 * <li>are {@link Rectangle} instances;
 * <li>are filled fairly black;
 * <li>have a thin and long form; and
 * <li>have sides fairly parallel to the coordinate axis.
 * </ul>
 */
void processPath() throws IOException {
    PDColor color = getGraphicsState().getNonStrokingColor();
    if (!isBlack(color)) {
        logger.debug("Dropped path due to non-black fill-color.");
        return;
    }

    for (PathElement pathElement : path) {
        if (pathElement instanceof Rectangle) {
            Rectangle rectangle = (Rectangle) pathElement;

            double p0p1 = rectangle.p0.distance(rectangle.p1);
            double p1p2 = rectangle.p1.distance(rectangle.p2);
            boolean p0p1small = p0p1 < 3;
            boolean p1p2small = p1p2 < 3;

            if (p0p1small) {
                if (p1p2small) {
                    logger.debug("Dropped rectangle too small on both sides.");
                } else {
                    processThinRectangle(rectangle.p0, rectangle.p1, rectangle.p2, rectangle.p3);
                }
            } else if (p1p2small) {
                processThinRectangle(rectangle.p1, rectangle.p2, rectangle.p3, rectangle.p0);
            } else {
                logger.debug("Dropped rectangle too large on both sides.");
            }
        }
    }
    path.clear();
}
 
Example #22
Source File: PdfTextStyleTest.java    From cat-boot with Apache License 2.0 5 votes vote down vote up
@Test
public void testTextWidth() {
    float fontSize = 0.5f;
    PDColor black = new PDColor(new float[] {0.0f, 0.0f, 0.0f}, PDDeviceRGB.INSTANCE);
    PdfTextStyle config = new PdfTextStyle(fontSize, PdfFont.COURIER, black, "bold");

    Float textWidth = PdfBoxHelper.getTextWidth(config.getCurrentFontStyle(), config.getFontSize(), "Some text");

    Assert.assertTrue(textWidth > 0);
}
 
Example #23
Source File: PdfTextStyleTest.java    From cat-boot with Apache License 2.0 5 votes vote down vote up
@Test
public void testFloatSize() {
    float fontSize = 10.5f;
    PDColor red = new PDColor(new float[] {1.0f, 0.0f, 0.0f}, PDDeviceRGB.INSTANCE);
    PdfTextStyle config = new PdfTextStyle(fontSize, PdfFont.TIMES_ROMAN, red, "regular");

    Assert.assertEquals(fontSize, config.getFontSize(), delta);
}
 
Example #24
Source File: PdfTextStyleTest.java    From cat-boot with Apache License 2.0 5 votes vote down vote up
@Test
public void fromConstructor() {
    PdfTextStyle config = new PdfTextStyle("10.5,Times-Roman,#000000");

    PDColor black = new PDColor(new float[] {0.0f, 0.0f, 0.0f}, PDDeviceRGB.INSTANCE);

    Assert.assertEquals(10.5, config.getFontSize(), delta);
    Assert.assertEquals("Times-Roman", config.getFont().getBasename());
    Assert.assertEquals(black.getColorSpace(), config.getColor().getColorSpace());
}
 
Example #25
Source File: PdfTextStyle.java    From cat-boot with Apache License 2.0 5 votes vote down vote up
/**
 * This constructor is used by spring when creating a font from properties.
 *
 * @param config e.g. 10,Times-Roman,#000000
 */
public PdfTextStyle(String config) {
    Assert.hasText(config);
    String[] split = config.split(",");
    Assert.isTrue(split.length == 3, "config must look like: 10,Times-Roman,#000000");
    fontSize = Float.parseFloat(split[0]);
    font = PdfFont.getFont(split[1]);
    Color tempColor = new Color(Integer.valueOf(split[2].substring(1), 16));
    float[] components = {tempColor.getRed(), tempColor.getGreen(), tempColor.getBlue()};
    color = new PDColor(components, PDDeviceRGB.INSTANCE);
}
 
Example #26
Source File: PDAppearanceContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Set the non stroking color.
 * 
 * <p>
 * The command is only emitted if the color is not null and the number of
 * components is &gt; 0.
 * 
 * @param color The colorspace to write.
 * @throws IOException If there is an error writing to the content stream.
 * @see PDAbstractContentStream#setNonStrokingColor(PDColor)
 */
public boolean setNonStrokingColorOnDemand(PDColor color) throws IOException
{
    if (color != null)
    {
        float[] components = color.getComponents();
        if (components.length > 0)
        {
            setNonStrokingColor(components);
            return true;
        }
    }
    return false;
}
 
Example #27
Source File: PDAppearanceContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Set the stroking color.
 * 
 * <p>
 * The command is only emitted if the color is not null and the number of
 * components is &gt; 0.
 * 
 * @param color The colorspace to write.
 * @throws IOException If there is an error writing to the content stream.
 * @see PDAbstractContentStream#setStrokingColor(PDColor)
 */
public boolean setStrokingColorOnDemand(PDColor color) throws IOException
{
    if (color != null)
    {
        float[] components = color.getComponents();
        if (components.length > 0)
        {
            setStrokingColor(components);
            return true;
        }
    }
    return false;
}
 
Example #28
Source File: SetNonStrokingColor.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Sets the non-stroking color.
 * @param color The new non-stroking color.
 */
@Override
protected void setColor(PDColor color)
{
    context.getGraphicsState().setNonStrokingColor(color);
}
 
Example #29
Source File: PathDrawer.java    From Pdf2Dom with GNU Lesser General Public License v3.0 4 votes vote down vote up
private Color pdfColorToColor(PDColor color) throws IOException
{
    float[] rgb = color.getColorSpace().toRGB(color.getComponents());

    return new Color(rgb[0], rgb[1], rgb[2]);
}
 
Example #30
Source File: SetNonStrokingColor.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Returns the non-stroking color.
 * @return The non-stroking color.
 */
@Override
protected PDColor getColor()
{
    return context.getGraphicsState().getNonStrokingColor();
}