use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.
the class COSWriter method visitFromArray.
@Override
public Object visitFromArray(COSArray obj) throws IOException {
int count = 0;
getStandardOutput().write(ARRAY_OPEN);
for (Iterator<COSBase> i = obj.iterator(); i.hasNext(); ) {
COSBase current = i.next();
if (current instanceof COSDictionary) {
if (current.isDirect()) {
visitFromDictionary((COSDictionary) current);
} else {
addObjectToWrite(current);
writeReference(current);
}
} else if (current instanceof COSObject) {
COSBase subValue = ((COSObject) current).getObject();
if (willEncrypt || incrementalUpdate || subValue instanceof COSDictionary || subValue == null) {
// PDFBOX-4308: added willEncrypt to prevent an object
// that is referenced several times from being written
// direct and indirect, thus getting encrypted
// with wrong object number or getting encrypted twice
addObjectToWrite(current);
writeReference(current);
} else {
subValue.accept(this);
}
} else if (current == null) {
COSNull.NULL.accept(this);
} else {
current.accept(this);
}
count++;
if (i.hasNext()) {
if (count % 10 == 0) {
getStandardOutput().writeEOL();
} else {
getStandardOutput().write(SPACE);
}
}
}
getStandardOutput().write(ARRAY_CLOSE);
getStandardOutput().writeEOL();
return null;
}
use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.
the class COSWriter method doWriteBody.
/**
* This will write the body of the document.
*
* @param doc The document to write the body for.
*
* @throws IOException If there is an error writing the data.
*/
protected void doWriteBody(COSDocument doc) throws IOException {
COSDictionary trailer = doc.getTrailer();
COSDictionary root = trailer.getCOSDictionary(COSName.ROOT);
COSDictionary info = trailer.getCOSDictionary(COSName.INFO);
COSDictionary encrypt = trailer.getCOSDictionary(COSName.ENCRYPT);
if (root != null) {
addObjectToWrite(root);
}
if (info != null) {
addObjectToWrite(info);
}
doWriteObjects();
willEncrypt = false;
if (encrypt != null) {
addObjectToWrite(encrypt);
}
doWriteObjects();
}
use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.
the class COSWriter method visitFromDocument.
@Override
public Object visitFromDocument(COSDocument doc) throws IOException {
if (!incrementalUpdate) {
doWriteHeader(doc);
} else {
// Sometimes the original file will be missing a newline at the end
// In order to avoid having %%EOF the first object on the same line
// as the %%EOF, we put a newline here. If there's already one at
// the end of the file, an extra one won't hurt. PDFBOX-1051
getStandardOutput().writeCRLF();
}
doWriteBody(doc);
// get the previous trailer
COSDictionary trailer = doc.getTrailer();
long hybridPrev = -1;
if (trailer != null) {
hybridPrev = trailer.getLong(COSName.XREF_STM);
}
if (incrementalUpdate || doc.isXRefStream()) {
doWriteXRefInc(doc, hybridPrev);
} else {
doWriteXRefTable();
doWriteTrailer(doc);
}
// write endof
getStandardOutput().write(STARTXREF);
getStandardOutput().writeEOL();
getStandardOutput().write(String.valueOf(getStartxref()).getBytes(Charsets.ISO_8859_1));
getStandardOutput().writeEOL();
getStandardOutput().write(EOF);
getStandardOutput().writeEOL();
if (incrementalUpdate) {
if (signatureOffset == 0 || byteRangeOffset == 0) {
doWriteIncrement();
} else {
doWriteSignature();
}
}
return null;
}
use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.
the class COSWriter method doWriteTrailer.
/**
* This will write the trailer to the PDF document.
*
* @param doc The document to create the trailer for.
*
* @throws IOException If there is an IOError while writing the document.
*/
protected void doWriteTrailer(COSDocument doc) throws IOException {
getStandardOutput().write(TRAILER);
getStandardOutput().writeEOL();
COSDictionary trailer = doc.getTrailer();
// sort xref, needed only if object keys not regenerated
Collections.sort(getXRefEntries());
COSWriterXRefEntry lastEntry = getXRefEntries().get(getXRefEntries().size() - 1);
trailer.setLong(COSName.SIZE, lastEntry.getKey().getNumber() + 1);
// Only need to stay, if an incremental update will be performed
if (!incrementalUpdate) {
trailer.removeItem(COSName.PREV);
}
if (!doc.isXRefStream()) {
trailer.removeItem(COSName.XREF_STM);
}
// Remove a checksum if present
trailer.removeItem(COSName.DOC_CHECKSUM);
COSArray idArray = trailer.getCOSArray(COSName.ID);
if (idArray != null) {
idArray.setDirect(true);
}
trailer.accept(this);
}
use of com.tom_roush.pdfbox.cos.COSDictionary 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);
}
}
Aggregations