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");
}
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();
}
}
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();
}
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());
}
}
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);
}
Aggregations