Search in sources :

Example 1 with PDFStreamParser

use of org.apache.pdfbox.pdfparser.PDFStreamParser in project pdfbox by apache.

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<>();
    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(org.apache.pdfbox.contentstream.operator.Operator) PDFStreamParser(org.apache.pdfbox.pdfparser.PDFStreamParser) COSObject(org.apache.pdfbox.cos.COSObject) ArrayList(java.util.ArrayList) COSBase(org.apache.pdfbox.cos.COSBase) COSObject(org.apache.pdfbox.cos.COSObject) IOException(java.io.IOException)

Example 2 with PDFStreamParser

use of org.apache.pdfbox.pdfparser.PDFStreamParser in project pdfbox by apache.

the class PDDefaultAppearanceString method processAppearanceStringOperators.

/**
 * Processes the operators of the given content stream.
 *
 * @param content the content to parse.
 * @throws IOException if there is an error reading or parsing the content stream.
 */
private void processAppearanceStringOperators(byte[] content) throws IOException {
    List<COSBase> arguments = new ArrayList<>();
    PDFStreamParser parser = new PDFStreamParser(content);
    Object token = parser.parseNextToken();
    while (token != null) {
        if (token instanceof COSObject) {
            arguments.add(((COSObject) token).getObject());
        } else if (token instanceof Operator) {
            processOperator((Operator) token, arguments);
            arguments = new ArrayList<>();
        } else {
            arguments.add((COSBase) token);
        }
        token = parser.parseNextToken();
    }
}
Also used : Operator(org.apache.pdfbox.contentstream.operator.Operator) PDFStreamParser(org.apache.pdfbox.pdfparser.PDFStreamParser) COSObject(org.apache.pdfbox.cos.COSObject) ArrayList(java.util.ArrayList) COSBase(org.apache.pdfbox.cos.COSBase) COSObject(org.apache.pdfbox.cos.COSObject)

Example 3 with PDFStreamParser

use of org.apache.pdfbox.pdfparser.PDFStreamParser in project pdfbox by apache.

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(org.apache.pdfbox.pdfparser.PDFStreamParser)

Example 4 with PDFStreamParser

use of org.apache.pdfbox.pdfparser.PDFStreamParser in project pdfbox by apache.

the class TestPDPageContentStream method testSetCmykColors.

public void testSetCmykColors() throws IOException {
    try (PDDocument doc = new PDDocument()) {
        PDPage page = new PDPage();
        doc.addPage(page);
        try (PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, true)) {
            // pass a non-stroking color in CMYK color space
            contentStream.setNonStrokingColor(0.1f, 0.2f, 0.3f, 0.4f);
            contentStream.close();
        }
        // now read the PDF stream and verify that the CMYK values are correct
        PDFStreamParser parser = new PDFStreamParser(page);
        parser.parse();
        java.util.List<Object> pageTokens = parser.getTokens();
        // expected five tokens :
        // [0] = COSFloat{0.1}
        // [1] = COSFloat{0.2}
        // [2] = COSFloat{0.3}
        // [3] = COSFloat{0.4}
        // [4] = PDFOperator{"k"}
        assertEquals(0.1f, ((COSFloat) pageTokens.get(0)).floatValue());
        assertEquals(0.2f, ((COSFloat) pageTokens.get(1)).floatValue());
        assertEquals(0.3f, ((COSFloat) pageTokens.get(2)).floatValue());
        assertEquals(0.4f, ((COSFloat) pageTokens.get(3)).floatValue());
        assertEquals("k", ((Operator) pageTokens.get(4)).getName());
        // same as above but for PDPageContentStream#setStrokingColor
        page = new PDPage();
        doc.addPage(page);
        try (PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false)) {
            // pass a non-stroking color in CMYK color space
            contentStream.setStrokingColor(0.5f, 0.6f, 0.7f, 0.8f);
            contentStream.close();
        }
        // now read the PDF stream and verify that the CMYK values are correct
        parser = new PDFStreamParser(page);
        parser.parse();
        pageTokens = parser.getTokens();
        // expected five tokens  :
        // [0] = COSFloat{0.5}
        // [1] = COSFloat{0.6}
        // [2] = COSFloat{0.7}
        // [3] = COSFloat{0.8}
        // [4] = PDFOperator{"K"}
        assertEquals(0.5f, ((COSFloat) pageTokens.get(0)).floatValue());
        assertEquals(0.6f, ((COSFloat) pageTokens.get(1)).floatValue());
        assertEquals(0.7f, ((COSFloat) pageTokens.get(2)).floatValue());
        assertEquals(0.8f, ((COSFloat) pageTokens.get(3)).floatValue());
        assertEquals("K", ((Operator) pageTokens.get(4)).getName());
    }
}
Also used : PDFStreamParser(org.apache.pdfbox.pdfparser.PDFStreamParser)

Example 5 with PDFStreamParser

use of org.apache.pdfbox.pdfparser.PDFStreamParser in project pdfbox by apache.

the class AppearanceGenerationTest method rectangleFullStrokeNoFill.

// Test currently disabled as the content stream differs
@Test
public void rectangleFullStrokeNoFill() throws IOException {
    PDPage page = document.getPage(0);
    PDAnnotation annotation = page.getAnnotations().get(0);
    // get the tokens of the content stream generated by Adobe
    PDAppearanceStream appearanceContentStream = annotation.getNormalAppearanceStream();
    PDFStreamParser streamParser = new PDFStreamParser(appearanceContentStream);
    streamParser.parse();
    List<Object> tokensForOriginal = streamParser.getTokens();
    // get the tokens for the content stream generated by PDFBox
    annotation.getCOSObject().removeItem(COSName.AP);
    annotation.constructAppearances();
    appearanceContentStream = annotation.getNormalAppearanceStream();
    streamParser = new PDFStreamParser(appearanceContentStream);
    streamParser.parse();
    List<Object> tokensForPdfbox = streamParser.getTokens();
    assertEquals("The number of tokens in the content stream should be the same", tokensForOriginal.size(), tokensForPdfbox.size());
    int actualToken = 0;
    for (Object tokenForOriginal : tokensForOriginal) {
        Object tokenForPdfbox = tokensForPdfbox.get(actualToken);
        assertEquals("The tokens should have the same type", tokenForOriginal.getClass().getName(), tokenForPdfbox.getClass().getName());
        if (tokenForOriginal instanceof Operator) {
            assertEquals("The operator generated by PDFBox should be the same Operator", ((Operator) tokenForOriginal).getName(), ((Operator) tokenForPdfbox).getName());
        } else if (tokenForOriginal instanceof COSFloat) {
            assertTrue("The difference between the numbers should be smaller than " + DELTA, (Math.abs(((COSFloat) tokenForOriginal).floatValue() - ((COSFloat) tokenForPdfbox).floatValue()) < DELTA));
        }
        actualToken++;
    }
    // Save the file for manual comparison for now
    File file = new File(OUT_DIR, NAME_OF_PDF + "-newAP.pdf");
    document.save(file);
}
Also used : Operator(org.apache.pdfbox.contentstream.operator.Operator) PDPage(org.apache.pdfbox.pdmodel.PDPage) PDFStreamParser(org.apache.pdfbox.pdfparser.PDFStreamParser) File(java.io.File) ScratchFile(org.apache.pdfbox.io.ScratchFile) COSFloat(org.apache.pdfbox.cos.COSFloat) Test(org.junit.Test)

Aggregations

PDFStreamParser (org.apache.pdfbox.pdfparser.PDFStreamParser)11 ArrayList (java.util.ArrayList)6 Operator (org.apache.pdfbox.contentstream.operator.Operator)6 COSBase (org.apache.pdfbox.cos.COSBase)4 COSObject (org.apache.pdfbox.cos.COSObject)4 File (java.io.File)2 PDPage (org.apache.pdfbox.pdmodel.PDPage)2 PDFormXObject (org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 COSFloat (org.apache.pdfbox.cos.COSFloat)1 COSNumber (org.apache.pdfbox.cos.COSNumber)1 COSString (org.apache.pdfbox.cos.COSString)1 ScratchFile (org.apache.pdfbox.io.ScratchFile)1 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)1 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)1 PDStream (org.apache.pdfbox.pdmodel.common.PDStream)1 PDXObject (org.apache.pdfbox.pdmodel.graphics.PDXObject)1 PDAnnotationWidget (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)1 Matrix (org.apache.pdfbox.util.Matrix)1