use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.
the class BaseParser method parseCOSArray.
/**
* This will parse a PDF array object.
*
* @return The parsed PDF array.
*
* @throws IOException If there is an error parsing the stream.
*/
protected COSArray parseCOSArray() throws IOException {
long startPosition = seqSource.getPosition();
readExpectedChar('[');
COSArray po = new COSArray();
COSBase pbo;
skipSpaces();
int i;
while (((i = seqSource.peek()) > 0) && ((char) i != ']')) {
pbo = parseDirObject();
if (pbo instanceof COSObject) {
// We have to check if the expected values are there or not PDFBOX-385
if (po.size() > 0 && po.get(po.size() - 1) instanceof COSInteger) {
COSInteger genNumber = (COSInteger) po.remove(po.size() - 1);
if (po.size() > 0 && po.get(po.size() - 1) instanceof COSInteger) {
COSInteger number = (COSInteger) po.remove(po.size() - 1);
COSObjectKey key = new COSObjectKey(number.longValue(), genNumber.intValue());
pbo = getObjectFromPool(key);
} else {
// the object reference is somehow wrong
pbo = null;
}
} else {
pbo = null;
}
}
if (pbo != null) {
po.add(pbo);
} else {
// it could be a bad object in the array which is just skipped
Log.w("PdfBox-Android", "Corrupt object reference at offset " + seqSource.getPosition() + ", start offset: " + startPosition);
// This could also be an "endobj" or "endstream" which means we can assume that
// the array has ended.
String isThisTheEnd = readString();
seqSource.unread(isThisTheEnd.getBytes(ISO_8859_1));
if (ENDOBJ_STRING.equals(isThisTheEnd) || ENDSTREAM_STRING.equals(isThisTheEnd)) {
return po;
}
}
skipSpaces();
}
// read ']'
seqSource.read();
skipSpaces();
return po;
}
use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.
the class BaseParser method parseDirObject.
/**
* This will parse a directory object from the stream.
*
* @return The parsed object.
*
* @throws IOException If there is an error during parsing.
*/
protected COSBase parseDirObject() throws IOException {
COSBase retval = null;
skipSpaces();
int nextByte = seqSource.peek();
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();
seqSource.unread(leftBracket);
if (c == '<') {
retval = parseCOSDictionary();
skipSpaces();
} else {
retval = parseCOSString();
}
break;
}
case '[':
{
// array
retval = parseCOSArray();
break;
}
case '(':
retval = parseCOSString();
break;
case '/':
// name
retval = parseCOSName();
break;
case 'n':
{
// null
readExpectedString(NULL);
retval = COSNull.NULL;
break;
}
case 't':
{
String trueString = new String(seqSource.readFully(4), ISO_8859_1);
if (trueString.equals(TRUE)) {
retval = COSBoolean.TRUE;
} else {
throw new IOException("expected true actual='" + trueString + "' " + seqSource + "' at offset " + seqSource.getPosition());
}
break;
}
case 'f':
{
String falseString = new String(seqSource.readFully(5), ISO_8859_1);
if (falseString.equals(FALSE)) {
retval = COSBoolean.FALSE;
} else {
throw new IOException("expected false actual='" + falseString + "' " + seqSource + "' at offset " + seqSource.getPosition());
}
break;
}
case 'R':
seqSource.read();
retval = new COSObject(null);
break;
case (char) -1:
return null;
default:
{
if (Character.isDigit(c) || c == '-' || c == '+' || c == '.') {
StringBuilder buf = new StringBuilder();
int ic = seqSource.read();
c = (char) ic;
while (Character.isDigit(c) || c == '-' || c == '+' || c == '.' || c == 'E' || c == 'e') {
buf.append(c);
ic = seqSource.read();
c = (char) ic;
}
if (ic != -1) {
seqSource.unread(ic);
}
retval = COSNumber.get(buf.toString());
} else {
// This is not suppose to happen, but we will allow for it
// so we are more compatible with POS writers that don't
// follow the spec
String badString = readString();
if (badString.isEmpty()) {
int peek = seqSource.peek();
// we can end up in an infinite loop otherwise
throw new IOException("Unknown dir object c='" + c + "' cInt=" + (int) c + " peek='" + (char) peek + "' peekInt=" + peek + " at offset " + seqSource.getPosition());
}
// if it's an endstream/endobj, we want to put it back so the caller will see it
if (ENDOBJ_STRING.equals(badString) || ENDSTREAM_STRING.equals(badString)) {
seqSource.unread(badString.getBytes(ISO_8859_1));
}
}
}
}
return retval;
}
use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.
the class Overlay method getLayoutPage.
private LayoutPage getLayoutPage(PDDocument doc) throws IOException {
PDPage page = doc.getPage(0);
COSBase contents = page.getCOSObject().getDictionaryObject(COSName.CONTENTS);
PDResources resources = page.getResources();
if (resources == null) {
resources = new PDResources();
}
return new LayoutPage(page.getMediaBox(), createCombinedContentStream(contents), resources.getCOSObject(), page.getRotation());
}
use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.
the class Overlay method processPages.
private void processPages(PDDocument document) throws IOException {
int pageCounter = 0;
for (PDPage page : document.getPages()) {
pageCounter++;
COSDictionary pageDictionary = page.getCOSObject();
COSBase originalContent = pageDictionary.getDictionaryObject(COSName.CONTENTS);
COSArray newContentArray = new COSArray();
LayoutPage layoutPage = getLayoutPage(pageCounter, document.getNumberOfPages());
if (layoutPage == null) {
continue;
}
switch(position) {
case FOREGROUND:
// save state
newContentArray.add(createStream("q\n"));
addOriginalContent(originalContent, newContentArray);
// restore state
newContentArray.add(createStream("Q\n"));
// overlay content last
overlayPage(page, layoutPage, newContentArray);
break;
case BACKGROUND:
// overlay content first
overlayPage(page, layoutPage, newContentArray);
addOriginalContent(originalContent, newContentArray);
break;
default:
throw new IOException("Unknown type of position:" + position);
}
pageDictionary.setItem(COSName.CONTENTS, newContentArray);
}
}
use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.
the class Overlay method getLayoutPages.
private Map<Integer, LayoutPage> getLayoutPages(PDDocument doc) throws IOException {
int numberOfPages = doc.getNumberOfPages();
Map<Integer, LayoutPage> layoutPages = new HashMap<Integer, Overlay.LayoutPage>(numberOfPages);
for (int i = 0; i < numberOfPages; i++) {
PDPage page = doc.getPage(i);
COSBase contents = page.getCOSObject().getDictionaryObject(COSName.CONTENTS);
PDResources resources = page.getResources();
if (resources == null) {
resources = new PDResources();
}
layoutPages.put(i, new LayoutPage(page.getMediaBox(), createCombinedContentStream(contents), resources.getCOSObject(), page.getRotation()));
}
return layoutPages;
}
Aggregations