use of org.apache.pdfbox.contentstream.operator.Operator in project pdfbox by apache.
the class ContentStreamWriter method writeObject.
private void writeObject(Object o) throws IOException {
if (o instanceof COSString) {
COSWriter.writeString((COSString) o, output);
output.write(SPACE);
} else if (o instanceof COSFloat) {
((COSFloat) o).writePDF(output);
output.write(SPACE);
} else if (o instanceof COSInteger) {
((COSInteger) o).writePDF(output);
output.write(SPACE);
} else if (o instanceof COSBoolean) {
((COSBoolean) o).writePDF(output);
output.write(SPACE);
} else if (o instanceof COSName) {
((COSName) o).writePDF(output);
output.write(SPACE);
} else if (o instanceof COSArray) {
COSArray array = (COSArray) o;
output.write(COSWriter.ARRAY_OPEN);
for (int i = 0; i < array.size(); i++) {
writeObject(array.get(i));
output.write(SPACE);
}
output.write(COSWriter.ARRAY_CLOSE);
} else if (o instanceof COSDictionary) {
COSDictionary obj = (COSDictionary) o;
output.write(COSWriter.DICT_OPEN);
for (Map.Entry<COSName, COSBase> entry : obj.entrySet()) {
if (entry.getValue() != null) {
writeObject(entry.getKey());
output.write(SPACE);
writeObject(entry.getValue());
output.write(SPACE);
}
}
output.write(COSWriter.DICT_CLOSE);
output.write(SPACE);
} else if (o instanceof Operator) {
Operator op = (Operator) o;
if (op.getName().equals("BI")) {
output.write("BI".getBytes(Charsets.ISO_8859_1));
COSDictionary dic = op.getImageParameters();
for (COSName key : dic.keySet()) {
Object value = dic.getDictionaryObject(key);
key.writePDF(output);
output.write(SPACE);
writeObject(value);
output.write(EOL);
}
output.write("ID".getBytes(Charsets.ISO_8859_1));
output.write(EOL);
output.write(op.getImageData());
output.write(EOL);
output.write("EI".getBytes(Charsets.ISO_8859_1));
output.write(EOL);
} else {
output.write(op.getName().getBytes(Charsets.ISO_8859_1));
output.write(EOL);
}
} else {
throw new IOException("Error:Unknown type in content stream:" + o);
}
}
use of org.apache.pdfbox.contentstream.operator.Operator 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.contentstream.operator.Operator 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.contentstream.operator.Operator 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);
}
use of org.apache.pdfbox.contentstream.operator.Operator in project pdfbox by apache.
the class PDFStreamEngine method processOperator.
/**
* This is used to handle an operation.
*
* @param operation The operation to perform.
* @param arguments The list of arguments.
* @throws IOException If there is an error processing the operation.
*/
public void processOperator(String operation, List<COSBase> arguments) throws IOException {
Operator operator = Operator.getOperator(operation);
processOperator(operator, arguments);
}
Aggregations