use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.
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(OperatorName.BEGIN_INLINE_IMAGE)) {
output.write(OperatorName.BEGIN_INLINE_IMAGE.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(OperatorName.BEGIN_INLINE_IMAGE_DATA.getBytes(Charsets.ISO_8859_1));
output.write(EOL);
output.write(op.getImageData());
output.write(EOL);
output.write(OperatorName.END_INLINE_IMAGE.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 com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.
the class PDFObjectStreamParser method parse.
/**
* This will parse the tokens in the stream. This will close the
* stream when it is finished parsing.
*
* @throws IOException If there is an error while parsing the stream.
*/
public void parse() throws IOException {
try {
Map<Integer, Long> offsets = readOffsets();
streamObjects = new ArrayList<COSObject>(offsets.size());
for (Entry<Integer, Long> offset : offsets.entrySet()) {
COSBase cosObject = parseObject(offset.getKey());
COSObject object = new COSObject(cosObject);
object.setGenerationNumber(0);
object.setObjectNumber(offset.getValue());
streamObjects.add(object);
if (PDFBoxConfig.isDebugEnabled()) {
Log.d("PdfBox-Android", "parsed=" + object);
}
}
} finally {
seqSource.close();
}
}
use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.
the class PDFStreamParser method parseNextToken.
/**
* This will parse the next token in the stream.
*
* @return The next token in the stream or null if there are no more tokens in the stream.
*
* @throws IOException If an io error occurs while parsing the stream.
*/
public Object parseNextToken() throws IOException {
Object retval;
skipSpaces();
int nextByte = seqSource.peek();
if (((byte) nextByte) == -1) {
return null;
}
char c = (char) nextByte;
switch(c) {
case '<':
{
// pull off first left bracket
int leftBracket = seqSource.read();
// check for second left bracket
c = (char) seqSource.peek();
// put back first bracket
seqSource.unread(leftBracket);
if (c == '<') {
retval = parseCOSDictionary();
} else {
retval = parseCOSString();
}
break;
}
case '[':
{
// array
retval = parseCOSArray();
break;
}
case '(':
// string
retval = parseCOSString();
break;
case '/':
// name
retval = parseCOSName();
break;
case 'n':
{
// null
String nullString = readString();
if (nullString.equals("null")) {
retval = COSNull.NULL;
} else {
retval = Operator.getOperator(nullString);
}
break;
}
case 't':
case 'f':
{
String next = readString();
if (next.equals("true")) {
retval = COSBoolean.TRUE;
break;
} else if (next.equals("false")) {
retval = COSBoolean.FALSE;
} else {
retval = Operator.getOperator(next);
}
break;
}
case 'R':
{
String line = readString();
if (line.equals("R")) {
retval = new COSObject(null);
} else {
retval = Operator.getOperator(line);
}
break;
}
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '-':
case '+':
case '.':
{
/* We will be filling buf with the rest of the number. Only
* allow 1 "." and "-" and "+" at start of number. */
StringBuilder buf = new StringBuilder();
buf.append(c);
seqSource.read();
// Ignore double negative (this is consistent with Adobe Reader)
if (c == '-' && seqSource.peek() == c) {
seqSource.read();
}
boolean dotNotRead = c != '.';
while (Character.isDigit(c = (char) seqSource.peek()) || dotNotRead && c == '.' || c == '-') {
if (c != '-') {
// PDFBOX-4064: ignore "-" in the middle of a number
buf.append(c);
}
seqSource.read();
if (dotNotRead && c == '.') {
dotNotRead = false;
}
}
retval = COSNumber.get(buf.toString());
break;
}
case 'B':
{
String next = readString();
retval = Operator.getOperator(next);
if (next.equals(OperatorName.BEGIN_INLINE_IMAGE)) {
Operator beginImageOP = (Operator) retval;
COSDictionary imageParams = new COSDictionary();
beginImageOP.setImageParameters(imageParams);
Object nextToken = null;
while ((nextToken = parseNextToken()) instanceof COSName) {
Object value = parseNextToken();
imageParams.setItem((COSName) nextToken, (COSBase) value);
}
// final token will be the image data, maybe??
if (nextToken instanceof Operator) {
Operator imageData = (Operator) nextToken;
if (imageData.getImageData() == null || imageData.getImageData().length == 0) {
Log.w("PdfBox-Android", "empty inline image at stream offset " + seqSource.getPosition());
}
beginImageOP.setImageData(imageData.getImageData());
}
}
break;
}
case 'I':
{
// Special case for ID operator
String id = Character.toString((char) seqSource.read()) + (char) seqSource.read();
if (!id.equals(OperatorName.BEGIN_INLINE_IMAGE_DATA)) {
throw new IOException("Error: Expected operator 'ID' actual='" + id + "' at stream offset " + seqSource.getPosition());
}
ByteArrayOutputStream imageData = new ByteArrayOutputStream();
if (isWhitespace()) {
// pull off the whitespace character
seqSource.read();
}
int lastByte = seqSource.read();
int currentByte = seqSource.read();
// Be aware not all kind of whitespaces are allowed here. see PDFBOX-1561
while (!(lastByte == 'E' && currentByte == 'I' && hasNextSpaceOrReturn() && hasNoFollowingBinData(seqSource)) && !seqSource.isEOF()) {
imageData.write(lastByte);
lastByte = currentByte;
currentByte = seqSource.read();
}
// the EI operator isn't unread, as it won't be processed anyway
retval = Operator.getOperator(OperatorName.BEGIN_INLINE_IMAGE_DATA);
// save the image data to the operator, so that it can be accessed later
((Operator) retval).setImageData(imageData.toByteArray());
break;
}
case ']':
{
// some ']' around without its previous '['
// this means a PDF is somewhat corrupt but we will continue to parse.
seqSource.read();
// must be a better solution than null...
retval = COSNull.NULL;
break;
}
default:
{
// we must be an operator
String operator = readOperator();
if (operator.trim().length() == 0) {
// we have a corrupt stream, stop reading here
retval = null;
} else {
retval = Operator.getOperator(operator);
}
}
}
return retval;
}
use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.
the class LayerUtility method wrapInSaveRestore.
/**
* Some applications may not wrap their page content in a save/restore (q/Q) pair which can
* lead to problems with coordinate system transformations when content is appended. This
* method lets you add a q/Q pair around the existing page's content.
* @param page the page
* @throws IOException if an I/O error occurs
*/
public void wrapInSaveRestore(PDPage page) throws IOException {
COSStream saveGraphicsStateStream = getDocument().getDocument().createCOSStream();
OutputStream saveStream = saveGraphicsStateStream.createOutputStream();
saveStream.write("q\n".getBytes("ISO-8859-1"));
saveStream.close();
COSStream restoreGraphicsStateStream = getDocument().getDocument().createCOSStream();
OutputStream restoreStream = restoreGraphicsStateStream.createOutputStream();
restoreStream.write("Q\n".getBytes("ISO-8859-1"));
restoreStream.close();
// Wrap the existing page's content in a save/restore pair (q/Q) to have a controlled
// environment to add additional content.
COSDictionary pageDictionary = page.getCOSObject();
COSBase contents = pageDictionary.getDictionaryObject(COSName.CONTENTS);
if (contents instanceof COSStream) {
COSStream contentsStream = (COSStream) contents;
COSArray array = new COSArray();
array.add(saveGraphicsStateStream);
array.add(contentsStream);
array.add(restoreGraphicsStateStream);
pageDictionary.setItem(COSName.CONTENTS, array);
} else if (contents instanceof COSArray) {
COSArray contentsArray = (COSArray) contents;
contentsArray.add(0, saveGraphicsStateStream);
contentsArray.add(restoreGraphicsStateStream);
} else {
throw new IOException("Contents are unknown type: " + contents.getClass().getName());
}
}
use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.
the class COSParser method parseDictionaryRecursive.
/**
* Resolves all not already parsed objects of a dictionary recursively.
*
* @param dictionaryObject dictionary to be parsed
* @throws IOException if something went wrong
*/
private void parseDictionaryRecursive(COSObject dictionaryObject) throws IOException {
parseObjectDynamically(dictionaryObject, true);
if (!(dictionaryObject.getObject() instanceof COSDictionary)) {
// to get the encryption directory
throw new IOException("Dictionary object expected at offset " + source.getPosition());
}
COSDictionary dictionary = (COSDictionary) dictionaryObject.getObject();
for (COSBase value : dictionary.getValues()) {
if (value instanceof COSObject) {
COSObject object = (COSObject) value;
if (object.getObject() == null) {
parseDictionaryRecursive(object);
}
}
}
}
Aggregations