use of com.tom_roush.pdfbox.cos.COSDocument in project PdfBox-Android by TomRoush.
the class TestPDFParser method executeParserTest.
private void executeParserTest(RandomAccessRead source, MemoryUsageSetting memUsageSetting) throws IOException {
ScratchFile scratchFile = new ScratchFile(memUsageSetting);
PDFParser pdfParser = new PDFParser(source, scratchFile);
pdfParser.parse();
COSDocument doc = pdfParser.getDocument();
assertNotNull(doc);
doc.close();
source.close();
// number tmp file must be the same
assertEquals(numberOfTmpFiles, getNumberOfTempFile());
}
use of com.tom_roush.pdfbox.cos.COSDocument in project PdfBox-Android by TomRoush.
the class COSWriter method write.
/**
* This will write the pdf document. If signature should be created externally,
* {@link #writeExternalSignature(byte[])} should be invoked to set signature after calling this
* method.
*
* @param doc The document to write.
* @param signInterface class to be used for signing; {@code null} if external signing would be
* performed or there will be no signing at all
*
* @throws IOException If an error occurs while generating the data.
* @throws IllegalStateException If the document has an encryption dictionary but no protection
* policy.
*/
public void write(PDDocument doc, SignatureInterface signInterface) throws IOException {
Long idTime = doc.getDocumentId() == null ? System.currentTimeMillis() : doc.getDocumentId();
pdDocument = doc;
signatureInterface = signInterface;
if (incrementalUpdate) {
prepareIncrement(doc);
}
// if the document says we should remove encryption, then we shouldn't encrypt
if (doc.isAllSecurityToBeRemoved()) {
willEncrypt = false;
// also need to get rid of the "Encrypt" in the trailer so readers
// don't try to decrypt a document which is not encrypted
COSDocument cosDoc = doc.getDocument();
COSDictionary trailer = cosDoc.getTrailer();
trailer.removeItem(COSName.ENCRYPT);
} else {
if (pdDocument.getEncryption() != null) {
if (!incrementalUpdate) {
SecurityHandler securityHandler = pdDocument.getEncryption().getSecurityHandler();
if (!securityHandler.hasProtectionPolicy()) {
throw new IllegalStateException("PDF contains an encryption dictionary, please remove it with " + "setAllSecurityToBeRemoved() or set a protection policy with protect()");
}
securityHandler.prepareDocumentForEncryption(pdDocument);
}
willEncrypt = true;
} else {
willEncrypt = false;
}
}
COSDocument cosDoc = pdDocument.getDocument();
COSDictionary trailer = cosDoc.getTrailer();
COSArray idArray = null;
boolean missingID = true;
COSBase base = trailer.getDictionaryObject(COSName.ID);
if (base instanceof COSArray) {
idArray = (COSArray) base;
if (idArray.size() == 2) {
missingID = false;
}
}
// check for an existing documentID
if (idArray != null && idArray.size() == 2) {
missingID = false;
}
if (missingID || incrementalUpdate) {
MessageDigest md5;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
// should never happen
throw new RuntimeException(e);
}
// algorithm says to use time/path/size/values in doc to generate the id.
// we don't have path or size, so do the best we can
md5.update(Long.toString(idTime).getBytes(Charsets.ISO_8859_1));
COSDictionary info = trailer.getCOSDictionary(COSName.INFO);
if (info != null) {
for (COSBase cosBase : info.getValues()) {
md5.update(cosBase.toString().getBytes(Charsets.ISO_8859_1));
}
}
// reuse origin documentID if available as first value
COSString firstID = missingID ? new COSString(md5.digest()) : (COSString) idArray.get(0);
// it's ok to use the same ID for the second part if the ID is created for the first time
COSString secondID = missingID ? firstID : new COSString(md5.digest());
idArray = new COSArray();
idArray.add(firstID);
idArray.add(secondID);
trailer.setItem(COSName.ID, idArray);
}
cosDoc.accept(this);
}
use of com.tom_roush.pdfbox.cos.COSDocument in project PdfBox-Android by TomRoush.
the class PDFParser method init.
private void init(ScratchFile scratchFile) {
String eofLookupRangeStr = System.getProperty(SYSPROP_EOFLOOKUPRANGE);
if (eofLookupRangeStr != null) {
try {
setEOFLookupRange(Integer.parseInt(eofLookupRangeStr));
} catch (NumberFormatException nfe) {
Log.w("PdfBox-Android", "System property " + SYSPROP_EOFLOOKUPRANGE + " does not contain an integer value, but: '" + eofLookupRangeStr + "'");
}
}
document = new COSDocument(scratchFile);
}
Aggregations