Search in sources :

Example 6 with PDColor

use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor in project PdfBox-Android by TomRoush.

the class PDFreeTextAppearanceHandler method extractNonStrokingColor.

// get the last non stroking color from the /DA entry
private PDColor extractNonStrokingColor(PDAnnotationMarkup annotation) {
    // It could also work with a regular expression, but that should be written so that
    // "/LucidaConsole 13.94766 Tf .392 .585 .93 rg" does not produce "2 .585 .93 rg" as result
    // Another alternative might be to create a PDDocument and a PDPage with /DA content as /Content,
    // process the whole thing and then get the non stroking color.
    PDColor strokingColor = new PDColor(new float[] { 0 }, PDDeviceGray.INSTANCE);
    String defaultAppearance = annotation.getDefaultAppearance();
    if (defaultAppearance == null) {
        return strokingColor;
    }
    try {
        // not sure if charset is correct, but we only need numbers and simple characters
        PDFStreamParser parser = new PDFStreamParser(defaultAppearance.getBytes(Charsets.US_ASCII));
        COSArray arguments = new COSArray();
        COSArray colors = null;
        Operator graphicOp = null;
        for (Object token = parser.parseNextToken(); token != null; token = parser.parseNextToken()) {
            if (token instanceof COSObject) {
                arguments.add(((COSObject) token).getObject());
            } else if (token instanceof Operator) {
                Operator op = (Operator) token;
                String name = op.getName();
                if (OperatorName.NON_STROKING_GRAY.equals(name) || OperatorName.NON_STROKING_RGB.equals(name) || OperatorName.NON_STROKING_CMYK.equals(name)) {
                    graphicOp = op;
                    colors = arguments;
                }
                arguments = new COSArray();
            } else {
                arguments.add((COSBase) token);
            }
        }
        if (graphicOp != null) {
            String graphicOpName = graphicOp.getName();
            if (OperatorName.NON_STROKING_GRAY.equals(graphicOpName)) {
                strokingColor = new PDColor(colors, PDDeviceGray.INSTANCE);
            } else if (OperatorName.NON_STROKING_RGB.equals(graphicOpName)) {
                strokingColor = new PDColor(colors, PDDeviceRGB.INSTANCE);
            } else if (OperatorName.NON_STROKING_CMYK.equals(graphicOpName)) {
            // strokingColor = new PDColor(colors, PDDeviceCMYK.INSTANCE); TODO: PdfBox-Android
            }
        }
    } catch (IOException ex) {
        Log.w("PdfBox-Android", "Problem parsing /DA, will use default black", ex);
    }
    return strokingColor;
}
Also used : Operator(com.tom_roush.pdfbox.contentstream.operator.Operator) PDFStreamParser(com.tom_roush.pdfbox.pdfparser.PDFStreamParser) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSObject(com.tom_roush.pdfbox.cos.COSObject) IOException(java.io.IOException) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)

Example 7 with PDColor

use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor in project PdfBox-Android by TomRoush.

the class PDInkAppearanceHandler method generateNormalAppearance.

@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;
            // 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.e("PdfBox-Android", ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(cs);
    }
}
Also used : PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PDAnnotationMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup) IOException(java.io.IOException) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)

Example 8 with PDColor

use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor in project PdfBox-Android by TomRoush.

the class TestCheckBox method testCheckBoxNoAppearance.

/**
 * PDFBOX-4366: Create and test a checkbox with no /AP. The created file works with Adobe Reader!
 *
 * @throws IOException
 */
public void testCheckBoxNoAppearance() throws IOException {
    PDDocument doc = new PDDocument();
    PDPage page = new PDPage();
    doc.addPage(page);
    PDAcroForm acroForm = new PDAcroForm(doc);
    // need this or it won't appear on Adobe Reader
    acroForm.setNeedAppearances(true);
    doc.getDocumentCatalog().setAcroForm(acroForm);
    List<PDField> fields = new ArrayList<PDField>();
    PDCheckBox checkBox = new PDCheckBox(acroForm);
    checkBox.setPartialName("checkbox");
    PDAnnotationWidget widget = checkBox.getWidgets().get(0);
    widget.setRectangle(new PDRectangle(50, 600, 100, 100));
    PDBorderStyleDictionary bs = new PDBorderStyleDictionary();
    bs.setStyle(PDBorderStyleDictionary.STYLE_SOLID);
    bs.setWidth(1);
    COSDictionary acd = new COSDictionary();
    PDAppearanceCharacteristicsDictionary ac = new PDAppearanceCharacteristicsDictionary(acd);
    ac.setBackground(new PDColor(new float[] { 1, 1, 0 }, PDDeviceRGB.INSTANCE));
    ac.setBorderColour(new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE));
    // 4 is checkmark, 8 is cross
    ac.setNormalCaption("4");
    widget.setAppearanceCharacteristics(ac);
    widget.setBorderStyle(bs);
    checkBox.setValue("Off");
    fields.add(checkBox);
    page.getAnnotations().add(widget);
    acroForm.setFields(fields);
    assertEquals("Off", checkBox.getValue());
    doc.close();
}
Also used : PDAppearanceCharacteristicsDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) ArrayList(java.util.ArrayList) PDBorderStyleDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDAnnotationWidget(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 9 with PDColor

use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor in project PdfBox-Android by TomRoush.

the class PDPageContentStream method setStrokingColor.

/**
 * 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(AWTColor 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);
}
Also used : PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)

Example 10 with PDColor

use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor in project PdfBox-Android by TomRoush.

the class PDFStreamEngine method processTilingPattern.

/**
 * Process the given tiling pattern. Allows the pattern matrix to be overridden for custom
 * rendering.
 *
 * @param tilingPattern the tiling pattern
 * @param color color to use, if this is an uncoloured pattern, otherwise null.
 * @param colorSpace color space to use, if this is an uncoloured pattern, otherwise null.
 * @param patternMatrix the pattern matrix, may be overridden for custom rendering.
 * @throws IOException if there is an error reading or parsing the tiling pattern content stream.
 */
protected final void processTilingPattern(PDTilingPattern tilingPattern, PDColor color, PDColorSpace colorSpace, Matrix patternMatrix) throws IOException {
    PDResources parent = pushResources(tilingPattern);
    Matrix parentMatrix = initialMatrix;
    initialMatrix = Matrix.concatenate(initialMatrix, patternMatrix);
    // save the original graphics state
    Deque<PDGraphicsState> savedStack = saveGraphicsStack();
    // save a clean state (new clipping path, line path, etc.)
    RectF bbox = new RectF();
    tilingPattern.getBBox().transform(patternMatrix).computeBounds(bbox, true);
    PDRectangle rect = new PDRectangle((float) bbox.left, (float) bbox.top, (float) bbox.width(), (float) bbox.height());
    graphicsStack.push(new PDGraphicsState(rect));
    // non-colored patterns have to be given a color
    if (colorSpace != null) {
        color = new PDColor(color.getComponents(), colorSpace);
        getGraphicsState().setNonStrokingColorSpace(colorSpace);
        getGraphicsState().setNonStrokingColor(color);
        getGraphicsState().setStrokingColorSpace(colorSpace);
        getGraphicsState().setStrokingColor(color);
    }
    // transform the CTM using the stream's matrix
    getGraphicsState().getCurrentTransformationMatrix().concatenate(patternMatrix);
    // clip to bounding box
    clipToRect(tilingPattern.getBBox());
    processStreamOperators(tilingPattern);
    initialMatrix = parentMatrix;
    restoreGraphicsStack(savedStack);
    popResources(parent);
}
Also used : RectF(android.graphics.RectF) Matrix(com.tom_roush.pdfbox.util.Matrix) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) PDGraphicsState(com.tom_roush.pdfbox.pdmodel.graphics.state.PDGraphicsState) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)

Aggregations

PDColor (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)22 IOException (java.io.IOException)12 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)11 PDAppearanceContentStream (com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream)10 COSArray (com.tom_roush.pdfbox.cos.COSArray)5 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)4 PDAnnotationTextMarkup (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup)4 PDAnnotationMarkup (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup)3 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)2 PDFormContentStream (com.tom_roush.pdfbox.pdmodel.PDFormContentStream)2 PDColorSpace (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace)2 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)2 PDAppearanceCharacteristicsDictionary (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary)2 PDBorderStyleDictionary (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)2 Matrix (com.tom_roush.pdfbox.util.Matrix)2 RectF (android.graphics.RectF)1 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)1 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)1 COSBase (com.tom_roush.pdfbox.cos.COSBase)1 COSFloat (com.tom_roush.pdfbox.cos.COSFloat)1