use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.
the class PDDocument method assignAcroFormDefaultResource.
private void assignAcroFormDefaultResource(PDAcroForm acroForm, COSDictionary newDict) {
// read and set/update AcroForm default resource dictionary /DR if available
COSBase newBase = newDict.getDictionaryObject(COSName.DR);
if (newBase instanceof COSDictionary) {
COSDictionary newDR = (COSDictionary) newBase;
PDResources defaultResources = acroForm.getDefaultResources();
if (defaultResources == null) {
acroForm.getCOSObject().setItem(COSName.DR, newDR);
newDR.setDirect(true);
newDR.setNeedToBeUpdated(true);
} else {
COSDictionary oldDR = defaultResources.getCOSObject();
COSBase newXObjectBase = newDR.getItem(COSName.XOBJECT);
COSBase oldXObjectBase = oldDR.getItem(COSName.XOBJECT);
if (newXObjectBase instanceof COSDictionary && oldXObjectBase instanceof COSDictionary) {
((COSDictionary) oldXObjectBase).addAll((COSDictionary) newXObjectBase);
oldDR.setNeedToBeUpdated(true);
}
}
}
}
use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.
the class PDDocument method prepareVisibleSignature.
private void prepareVisibleSignature(PDSignatureField signatureField, PDAcroForm acroForm, COSDocument visualSignature) {
// Obtain visual signature object
boolean annotNotFound = true;
boolean sigFieldNotFound = true;
for (COSObject cosObject : visualSignature.getObjects()) {
if (!annotNotFound && !sigFieldNotFound) {
break;
}
COSBase base = cosObject.getObject();
if (base instanceof COSDictionary) {
COSDictionary cosBaseDict = (COSDictionary) base;
// Search for signature annotation
COSBase type = cosBaseDict.getDictionaryObject(COSName.TYPE);
if (annotNotFound && COSName.ANNOT.equals(type)) {
assignSignatureRectangle(signatureField, cosBaseDict);
annotNotFound = false;
}
// Search for signature field
COSBase fieldType = cosBaseDict.getDictionaryObject(COSName.FT);
COSBase apDict = cosBaseDict.getDictionaryObject(COSName.AP);
if (sigFieldNotFound && COSName.SIG.equals(fieldType) && apDict instanceof COSDictionary) {
assignAppearanceDictionary(signatureField, (COSDictionary) apDict);
assignAcroFormDefaultResource(acroForm, cosBaseDict);
sigFieldNotFound = false;
}
}
}
if (annotNotFound || sigFieldNotFound) {
throw new IllegalArgumentException("Template is missing required objects");
}
}
use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.
the class PDDocument method addSignatureField.
/**
* This will add a list of signature fields to the document.
*
* @param sigFields are the PDSignatureFields that should be added to the document
* @param signatureInterface is an interface whose implementation provides
* signing capabilities. Can be null if external signing if used.
* @param options signature options
* @throws IOException if there is an error creating required fields
* @deprecated The method is misleading, because only one signature may be
* added in a document. The method will be removed in the future.
*/
@Deprecated
public void addSignatureField(List<PDSignatureField> sigFields, SignatureInterface signatureInterface, SignatureOptions options) throws IOException {
PDDocumentCatalog catalog = getDocumentCatalog();
catalog.getCOSObject().setNeedToBeUpdated(true);
PDAcroForm acroForm = catalog.getAcroForm();
if (acroForm == null) {
acroForm = new PDAcroForm(this);
catalog.setAcroForm(acroForm);
}
COSDictionary acroFormDict = acroForm.getCOSObject();
acroFormDict.setDirect(true);
acroFormDict.setNeedToBeUpdated(true);
if (!acroForm.isSignaturesExist()) {
// 1 if at least one signature field is available
acroForm.setSignaturesExist(true);
}
List<PDField> acroformFields = acroForm.getFields();
for (PDSignatureField sigField : sigFields) {
sigField.getCOSObject().setNeedToBeUpdated(true);
// Check if the field already exists
boolean checkSignatureField = checkSignatureField(acroForm.getFieldIterator(), sigField);
if (checkSignatureField) {
sigField.getCOSObject().setNeedToBeUpdated(true);
} else {
acroformFields.add(sigField);
}
// Check if we need to add a signature
if (sigField.getSignature() != null) {
sigField.getCOSObject().setNeedToBeUpdated(true);
if (options == null) {
// TODO ??
}
addSignature(sigField.getSignature(), signatureInterface, options);
}
}
}
use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.
the class PDDocument method getDocumentCatalog.
/**
* This will get the document CATALOG. This is guaranteed to not return null.
*
* @return The documents /Root dictionary
*/
public PDDocumentCatalog getDocumentCatalog() {
if (documentCatalog == null) {
COSDictionary trailer = document.getTrailer();
COSBase dictionary = trailer.getDictionaryObject(COSName.ROOT);
if (dictionary instanceof COSDictionary) {
documentCatalog = new PDDocumentCatalog(this, (COSDictionary) dictionary);
} else {
documentCatalog = new PDDocumentCatalog(this);
}
}
return documentCatalog;
}
use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.
the class PDFMergerUtility method mergeIDTree.
private void mergeIDTree(PDFCloneUtility cloner, PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree) throws IOException {
PDNameTreeNode<PDStructureElement> srcIDTree = srcStructTree.getIDTree();
PDNameTreeNode<PDStructureElement> destIDTree = destStructTree.getIDTree();
if (srcIDTree == null) {
return;
}
if (destIDTree == null) {
destIDTree = new PDStructureElementNameTreeNode();
}
Map<String, PDStructureElement> srcNames = getIDTreeAsMap(srcIDTree);
Map<String, PDStructureElement> destNames = getIDTreeAsMap(destIDTree);
for (Map.Entry<String, PDStructureElement> entry : srcNames.entrySet()) {
if (destNames.containsKey(entry.getKey())) {
Log.w("PdfBox-Android", "key " + entry.getKey() + " already exists in destination IDTree");
} else {
destNames.put(entry.getKey(), new PDStructureElement((COSDictionary) cloner.cloneForNewDocument(entry.getValue().getCOSObject())));
}
}
destIDTree = new PDStructureElementNameTreeNode();
destIDTree.setNames(destNames);
destStructTree.setIDTree(destIDTree);
// Note that all elements are stored flatly. This could become a problem for large files
// when these are opened in a viewer that uses the tagging information.
// If this happens, then PDNameTreeNode should be improved with a convenience method that
// stores the map into a B+Tree, see https://en.wikipedia.org/wiki/B+_tree
}
Aggregations