Java Code Examples for org.apache.pdfbox.pdmodel.graphics.color.PDColor#getComponents()

The following examples show how to use org.apache.pdfbox.pdmodel.graphics.color.PDColor#getComponents() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
Source File: PDInkAppearanceHandler.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public void generateNormalAppearance()
{
    PDAnnotationMarkup ink = (PDAnnotationMarkup) getAnnotation();
    // PDF spec does not mention /Border for ink annotations, but it is used if /BS is not available
    AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(ink, ink.getBorderStyle());
    PDColor color = ink.getColor();
    if (color == null || color.getComponents().length == 0 || Float.compare(ab.width, 0) == 0)
    {
        return;
    }

    PDAppearanceContentStream cs = null;

    try
    {
        cs = getNormalAppearanceAsContentStream();

        setOpacity(cs, ink.getConstantOpacity());

        cs.setStrokingColor(color);
        if (ab.dashArray != null)
        {
            cs.setLineDashPattern(ab.dashArray, 0);
        }
        cs.setLineWidth(ab.width);

        for (float[] pathArray : ink.getInkList())
        {
            int nPoints = pathArray.length / 2;

            // "When drawn, the points shall be connected by straight lines or curves 
            // in an implementation-dependent way" - we do lines.
            for (int i = 0; i < nPoints; ++i)
            {
                float x = pathArray[i * 2];
                float y = pathArray[i * 2 + 1];

                if (i == 0)
                {
                    cs.moveTo(x, y);
                }
                else
                {
                    cs.lineTo(x, y);
                }
            }
            cs.stroke();
        }
    }
    catch (IOException ex)
    {
        LOG.error(ex);
    }
    finally
    {
        IOUtils.closeQuietly(cs);
    }
}
 
Example 8
Source File: PDUnderlineAppearanceHandler.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
@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);
        if (ab.dashArray != null)
        {
            cs.setLineDashPattern(ab.dashArray, 0);
        }
        cs.setLineWidth(ab.width);

        // spec is incorrect
        // https://stackoverflow.com/questions/9855814/pdf-spec-vs-acrobat-creation-quadpoints
        for (int i = 0; i < pathsArray.length / 8; ++i)
        {
            // Adobe doesn't use the lower coordinate for the line, it uses lower + delta / 7.
            // do the math for diagonal annotations with this weird old trick:
            // https://stackoverflow.com/questions/7740507/extend-a-line-segment-a-specific-distance
            float len0 = (float) (Math.sqrt(Math.pow(pathsArray[i * 8] - pathsArray[i * 8 + 4], 2) + 
                                  Math.pow(pathsArray[i * 8 + 1] - pathsArray[i * 8 + 5], 2)));
            float x0 = pathsArray[i * 8 + 4];
            float y0 = pathsArray[i * 8 + 5];
            if (Float.compare(len0, 0) != 0)
            {
                // only if both coordinates are not identical to avoid divide by zero
                x0 += (pathsArray[i * 8] - pathsArray[i * 8 + 4]) / len0 * len0 / 7;
                y0 += (pathsArray[i * 8 + 1] - pathsArray[i * 8 + 5]) / len0 * (len0 / 7);
            }
            float len1 = (float) (Math.sqrt(Math.pow(pathsArray[i * 8 + 2] - pathsArray[i * 8 + 6], 2) + 
                                  Math.pow(pathsArray[i * 8 + 3] - pathsArray[i * 8 + 7], 2)));
            float x1 = pathsArray[i * 8 + 6];
            float y1 = pathsArray[i * 8 + 7];
            if (Float.compare(len1, 0) != 0)
            {
                // only if both coordinates are not identical to avoid divide by zero
                x1 += (pathsArray[i * 8 + 2] - pathsArray[i * 8 + 6]) / len1 * len1 / 7;
                y1 += (pathsArray[i * 8 + 3] - pathsArray[i * 8 + 7]) / len1 * len1 / 7;
            }
            cs.moveTo(x0, y0);
            cs.lineTo(x1, y1);
        }
        cs.stroke();
    }
    catch (IOException ex)
    {
        LOG.error(ex);
    }
    finally
    {
        IOUtils.closeQuietly(cs);
    }
}