use of com.tom_roush.pdfbox.cos.COSNumber in project PdfBox-Android by TomRoush.
the class COSWriter method prepareIncrement.
private void prepareIncrement(PDDocument doc) {
try {
if (doc != null) {
COSDocument cosDoc = doc.getDocument();
Map<COSObjectKey, Long> xrefTable = cosDoc.getXrefTable();
Set<COSObjectKey> keySet = xrefTable.keySet();
long highestNumber = doc.getDocument().getHighestXRefObjectNumber();
for (COSObjectKey cosObjectKey : keySet) {
COSBase object = cosDoc.getObjectFromPool(cosObjectKey).getObject();
if (object != null && cosObjectKey != null && !(object instanceof COSNumber)) {
objectKeys.put(object, cosObjectKey);
keyObject.put(cosObjectKey, object);
}
if (cosObjectKey != null) {
long num = cosObjectKey.getNumber();
if (num > highestNumber) {
highestNumber = num;
}
}
}
setNumber(highestNumber);
}
} catch (IOException e) {
Log.e("PdfBox-Android", e.getMessage(), e);
}
}
use of com.tom_roush.pdfbox.cos.COSNumber in project PdfBox-Android by TomRoush.
the class COSParser method parseCOSStream.
/**
* This will read a COSStream from the input stream using length attribute within dictionary. If
* length attribute is a indirect reference it is first resolved to get the stream length. This
* means we copy stream data without testing for 'endstream' or 'endobj' and thus it is no
* problem if these keywords occur within stream. We require 'endstream' to be found after
* stream data is read.
*
* @param dic dictionary that goes with this stream.
*
* @return parsed pdf stream.
*
* @throws IOException if an error occurred reading the stream, like problems with reading
* length attribute, stream does not end with 'endstream' after data read, stream too short etc.
*/
protected COSStream parseCOSStream(COSDictionary dic) throws IOException {
COSStream stream = document.createCOSStream(dic);
// read 'stream'; this was already tested in parseObjectsDynamically()
readString();
skipWhiteSpaces();
/*
* This needs to be dic.getItem because when we are parsing, the underlying object might still be null.
*/
COSNumber streamLengthObj = getLength(dic.getItem(COSName.LENGTH), dic.getCOSName(COSName.TYPE));
if (streamLengthObj == null) {
if (isLenient) {
Log.w("PdfBox-Android", "The stream doesn't provide any stream length, using fallback readUntilEnd, at offset " + source.getPosition());
} else {
throw new IOException("Missing length for stream.");
}
}
// get output stream to copy data to
if (streamLengthObj != null && validateStreamLength(streamLengthObj.longValue())) {
OutputStream out = stream.createRawOutputStream();
try {
readValidStream(out, streamLengthObj);
} finally {
out.close();
// restore original (possibly incorrect) length
stream.setItem(COSName.LENGTH, streamLengthObj);
}
} else {
OutputStream out = stream.createRawOutputStream();
try {
readUntilEndStream(new EndstreamOutputStream(out));
} finally {
out.close();
// restore original (possibly incorrect) length
if (streamLengthObj != null) {
stream.setItem(COSName.LENGTH, streamLengthObj);
}
}
}
String endStream = readString();
if (endStream.equals("endobj") && isLenient) {
Log.w("PdfBox-Android", "stream ends with 'endobj' instead of 'endstream' at offset " + source.getPosition());
// avoid follow-up warning about missing endobj
source.rewind(ENDOBJ.length);
} else if (endStream.length() > 9 && isLenient && endStream.substring(0, 9).equals(ENDSTREAM_STRING)) {
Log.w("PdfBox-Android", "stream ends with '" + endStream + "' instead of 'endstream' at offset " + source.getPosition());
// unread the "extra" bytes
source.rewind(endStream.substring(9).getBytes(ISO_8859_1).length);
} else if (!endStream.equals(ENDSTREAM_STRING)) {
throw new IOException("Error reading stream, expected='endstream' actual='" + endStream + "' at offset " + source.getPosition());
}
return stream;
}
use of com.tom_roush.pdfbox.cos.COSNumber in project PdfBox-Android by TomRoush.
the class PDPolygonAppearanceHandler method getLineWidth.
/**
* Get the line with of the border.
*
* Get the width of the line used to draw a border around the annotation.
* This may either be specified by the annotation dictionaries Border
* setting or by the W entry in the BS border style dictionary. If both are
* missing the default width is 1.
*
* @return the line width
*/
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth() {
PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();
PDBorderStyleDictionary bs = annotation.getBorderStyle();
if (bs != null) {
return bs.getWidth();
}
COSArray borderCharacteristics = annotation.getBorder();
if (borderCharacteristics.size() >= 3) {
COSBase base = borderCharacteristics.getObject(2);
if (base instanceof COSNumber) {
return ((COSNumber) base).floatValue();
}
}
return 1;
}
use of com.tom_roush.pdfbox.cos.COSNumber in project PdfBox-Android by TomRoush.
the class PDPolylineAppearanceHandler method getLineWidth.
// TODO DRY, this code is from polygonAppearanceHandler so it's double
/**
* Get the line with of the border.
*
* Get the width of the line used to draw a border around the annotation.
* This may either be specified by the annotation dictionaries Border
* setting or by the W entry in the BS border style dictionary. If both are
* missing the default width is 1.
*
* @return the line width
*/
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth() {
PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();
PDBorderStyleDictionary bs = annotation.getBorderStyle();
if (bs != null) {
return bs.getWidth();
}
COSArray borderCharacteristics = annotation.getBorder();
if (borderCharacteristics.size() >= 3) {
COSBase base = borderCharacteristics.getObject(2);
if (base instanceof COSNumber) {
return ((COSNumber) base).floatValue();
}
}
return 1;
}
use of com.tom_roush.pdfbox.cos.COSNumber in project PdfBox-Android by TomRoush.
the class PDSquareAppearanceHandler method getLineWidth.
/**
* Get the line with of the border.
*
* Get the width of the line used to draw a border around the annotation.
* This may either be specified by the annotation dictionaries Border
* setting or by the W entry in the BS border style dictionary. If both are
* missing the default width is 1.
*
* @return the line width
*/
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth() {
PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();
PDBorderStyleDictionary bs = annotation.getBorderStyle();
if (bs != null) {
return bs.getWidth();
}
COSArray borderCharacteristics = annotation.getBorder();
if (borderCharacteristics.size() >= 3) {
COSBase base = borderCharacteristics.getObject(2);
if (base instanceof COSNumber) {
return ((COSNumber) base).floatValue();
}
}
return 1;
}
Aggregations