Search in sources :

Example 1 with PDFStreamParser

use of com.tom_roush.pdfbox.pdfparser.PDFStreamParser 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 2 with PDFStreamParser

use of com.tom_roush.pdfbox.pdfparser.PDFStreamParser in project PdfBox-Android by TomRoush.

the class TestPDPageContentStream method testSetRGBandGColors.

public void testSetRGBandGColors() throws IOException {
    PDDocument doc = new PDDocument();
    PDPage page = new PDPage();
    doc.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, true);
    // pass a non-stroking color in RGB and Gray color space
    contentStream.setNonStrokingColor(0.1f, 0.2f, 0.3f);
    contentStream.setNonStrokingColor(1, 2, 3);
    contentStream.setNonStrokingColor(0.8f);
    contentStream.setNonStrokingColor(8);
    contentStream.close();
    // now read the PDF stream and verify that the values are correct
    PDFStreamParser parser = new PDFStreamParser(page);
    parser.parse();
    List<Object> pageTokens = parser.getTokens();
    assertEquals(0.1f, ((COSNumber) pageTokens.get(0)).floatValue());
    assertEquals(0.2f, ((COSNumber) pageTokens.get(1)).floatValue());
    assertEquals(0.3f, ((COSNumber) pageTokens.get(2)).floatValue());
    assertEquals(OperatorName.NON_STROKING_RGB, ((Operator) pageTokens.get(3)).getName());
    assertEquals(1 / 255f, ((COSNumber) pageTokens.get(4)).floatValue(), 0.00001d);
    assertEquals(2 / 255f, ((COSNumber) pageTokens.get(5)).floatValue(), 0.00001d);
    assertEquals(3 / 255f, ((COSNumber) pageTokens.get(6)).floatValue(), 0.00001d);
    assertEquals(OperatorName.NON_STROKING_RGB, ((Operator) pageTokens.get(7)).getName());
    assertEquals(0.8f, ((COSNumber) pageTokens.get(8)).floatValue());
    assertEquals(OperatorName.NON_STROKING_GRAY, ((Operator) pageTokens.get(9)).getName());
    assertEquals(8 / 255f, ((COSNumber) pageTokens.get(10)).floatValue(), 0.00001d);
    assertEquals(OperatorName.NON_STROKING_GRAY, ((Operator) pageTokens.get(11)).getName());
    // same as above but for PDPageContentStream#setStrokingColor
    page = new PDPage();
    doc.addPage(page);
    contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
    // pass a non-stroking color in RGB and Gray color space
    contentStream.setStrokingColor(0.5f, 0.6f, 0.7f);
    contentStream.setStrokingColor(5, 6, 7);
    contentStream.setStrokingColor(0.8f);
    contentStream.setStrokingColor(8);
    contentStream.close();
    // now read the PDF stream and verify that the values are correct
    parser = new PDFStreamParser(page);
    parser.parse();
    pageTokens = parser.getTokens();
    assertEquals(0.5f, ((COSNumber) pageTokens.get(0)).floatValue());
    assertEquals(0.6f, ((COSNumber) pageTokens.get(1)).floatValue());
    assertEquals(0.7f, ((COSNumber) pageTokens.get(2)).floatValue());
    assertEquals(OperatorName.STROKING_COLOR_RGB, ((Operator) pageTokens.get(3)).getName());
    assertEquals(5 / 255f, ((COSNumber) pageTokens.get(4)).floatValue(), 0.00001d);
    assertEquals(6 / 255f, ((COSNumber) pageTokens.get(5)).floatValue(), 0.00001d);
    assertEquals(7 / 255f, ((COSNumber) pageTokens.get(6)).floatValue(), 0.00001d);
    assertEquals(OperatorName.STROKING_COLOR_RGB, ((Operator) pageTokens.get(7)).getName());
    assertEquals(0.8f, ((COSNumber) pageTokens.get(8)).floatValue());
    assertEquals(OperatorName.STROKING_COLOR_GRAY, ((Operator) pageTokens.get(9)).getName());
    assertEquals(8 / 255f, ((COSNumber) pageTokens.get(10)).floatValue(), 0.00001d);
    assertEquals(OperatorName.STROKING_COLOR_GRAY, ((Operator) pageTokens.get(11)).getName());
    doc.close();
}
Also used : PDFStreamParser(com.tom_roush.pdfbox.pdfparser.PDFStreamParser)

Example 3 with PDFStreamParser

use of com.tom_roush.pdfbox.pdfparser.PDFStreamParser in project PdfBox-Android by TomRoush.

the class PDType3CharProc method getWidth.

/**
 * Get the width from a type3 charproc stream.
 *
 * @return the glyph width.
 * @throws IOException if the stream could not be read, or did not have d0 or d1 as first
 * operator, or if their first argument was not a number.
 */
public float getWidth() throws IOException {
    List<COSBase> arguments = new ArrayList<COSBase>();
    PDFStreamParser parser = new PDFStreamParser(this);
    Object token = parser.parseNextToken();
    while (token != null) {
        if (token instanceof COSObject) {
            arguments.add(((COSObject) token).getObject());
        } else if (token instanceof Operator) {
            return parseWidth((Operator) token, arguments);
        } else {
            arguments.add((COSBase) token);
        }
        token = parser.parseNextToken();
    }
    throw new IOException("Unexpected end of stream");
}
Also used : Operator(com.tom_roush.pdfbox.contentstream.operator.Operator) PDFStreamParser(com.tom_roush.pdfbox.pdfparser.PDFStreamParser) COSObject(com.tom_roush.pdfbox.cos.COSObject) ArrayList(java.util.ArrayList) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSObject(com.tom_roush.pdfbox.cos.COSObject) IOException(java.io.IOException)

Example 4 with PDFStreamParser

use of com.tom_roush.pdfbox.pdfparser.PDFStreamParser in project PdfBox-Android by TomRoush.

the class PDType3CharProc method getGlyphBBox.

/**
 * Calculate the bounding box of this glyph. This will work only if the first operator in the
 * stream is d1.
 *
 * @return the bounding box of this glyph, or null if the first operator is not d1.
 * @throws IOException If an io error occurs while parsing the stream.
 */
public PDRectangle getGlyphBBox() throws IOException {
    List<COSBase> arguments = new ArrayList<COSBase>();
    PDFStreamParser parser = new PDFStreamParser(this);
    Object token = parser.parseNextToken();
    while (token != null) {
        if (token instanceof COSObject) {
            arguments.add(((COSObject) token).getObject());
        } else if (token instanceof Operator) {
            if (((Operator) token).getName().equals("d1") && arguments.size() == 6) {
                for (int i = 0; i < 6; ++i) {
                    if (!(arguments.get(i) instanceof COSNumber)) {
                        return null;
                    }
                }
                return new PDRectangle(((COSNumber) arguments.get(2)).floatValue(), ((COSNumber) arguments.get(3)).floatValue(), ((COSNumber) arguments.get(4)).floatValue() - ((COSNumber) arguments.get(2)).floatValue(), ((COSNumber) arguments.get(5)).floatValue() - ((COSNumber) arguments.get(3)).floatValue());
            } else {
                return null;
            }
        } else {
            arguments.add((COSBase) token);
        }
        token = parser.parseNextToken();
    }
    return null;
}
Also used : Operator(com.tom_roush.pdfbox.contentstream.operator.Operator) PDFStreamParser(com.tom_roush.pdfbox.pdfparser.PDFStreamParser) COSObject(com.tom_roush.pdfbox.cos.COSObject) ArrayList(java.util.ArrayList) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSObject(com.tom_roush.pdfbox.cos.COSObject) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 5 with PDFStreamParser

use of com.tom_roush.pdfbox.pdfparser.PDFStreamParser in project PdfBox-Android by TomRoush.

the class AppearanceGeneratorHelper method tokenize.

/**
 * Parses an appearance stream into tokens.
 */
private List<Object> tokenize(PDAppearanceStream appearanceStream) throws IOException {
    PDFStreamParser parser = new PDFStreamParser(appearanceStream);
    parser.parse();
    return parser.getTokens();
}
Also used : PDFStreamParser(com.tom_roush.pdfbox.pdfparser.PDFStreamParser)

Aggregations

PDFStreamParser (com.tom_roush.pdfbox.pdfparser.PDFStreamParser)11 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)6 COSBase (com.tom_roush.pdfbox.cos.COSBase)6 COSObject (com.tom_roush.pdfbox.cos.COSObject)6 ArrayList (java.util.ArrayList)5 IOException (java.io.IOException)3 COSArray (com.tom_roush.pdfbox.cos.COSArray)2 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)2 COSName (com.tom_roush.pdfbox.cos.COSName)1 COSString (com.tom_roush.pdfbox.cos.COSString)1 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)1 PDColor (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)1 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)1 PDAnnotationWidget (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)1