Ejemplo n.º 1
0
 /**
  * This method scans the document for all WidgetAnnotation objects.
  *
  * <p>This is done because some writer do not create a correct list of all PDAcroFormField objects
  * in the AcroForm. In the case that the list of children is empty, we go and search ourselves for
  * candidates...
  *
  * @param doc The document to reconstruct.
  */
 protected COSArray reconstruct(PDDocument doc) {
   COSArray result = COSArray.create();
   if (doc == null) {
     return result;
   }
   PDPageTree pageTree = doc.getPageTree();
   if (pageTree == null) {
     return result;
   }
   boolean signatureExists = false;
   for (PDPage page = pageTree.getFirstPage(); page != null; page = page.getNextPage()) {
     List annotations = page.getAnnotations();
     if (annotations == null) {
       continue;
     }
     for (Iterator it = annotations.iterator(); it.hasNext(); ) {
       PDAnnotation annot = (PDAnnotation) it.next();
       if (annot.isWidgetAnnotation()) {
         COSDictionary cosAnnot = annot.cosGetDict();
         result.basicAddSilent(cosAnnot);
         cosAnnot.basicRemoveSilent(PDAcroFormField.DK_Parent);
         signatureExists |= cosAnnot.get(PDAcroFormField.DK_FT).equals(PDAcroFormField.CN_FT_Sig);
       }
     }
   }
   if (signatureExists) {
     int flags = getFieldInt(PDAcroForm.DK_SigFlags, 0);
     flags |= AcroFormSigFlags.Bit_AppendOnly | AcroFormSigFlags.Bit_SignatureExists;
     cosGetDict().basicPutSilent(PDAcroForm.DK_SigFlags, COSInteger.create(flags));
   }
   return result;
 }
Ejemplo n.º 2
0
 protected void do_def(CSOperation operation) {
   // define key / value association
   Iterator it = operation.getOperands();
   COSObject operand = COSNull.NULL;
   if (it.hasNext()) {
     operand = (COSObject) it.next();
   }
   COSDictionary dict = operand.asDictionary();
   if (dict == null) {
     COSName key = operand.asName();
     if (key == null) {
       return;
     }
     COSObject value = COSNull.NULL;
     if (it.hasNext()) {
       value = (COSObject) it.next();
     }
     addDefinition(key, value);
   } else {
     Iterator<Map.Entry<COSName, COSObject>> eit = dict.entryIterator();
     while (eit.hasNext()) {
       Map.Entry<COSName, COSObject> entry = eit.next();
       COSName key = entry.getKey();
       COSObject value = entry.getValue();
       addDefinition(key, value);
     }
   }
 }
 public static SystemSecurityHandler createFromSt(STDocument doc) throws COSSecurityException {
   SystemSecurityHandler systemSecurityHandler = null;
   COSDictionary dict = doc.cosGetTrailer().get(COSTrailer.DK_Encrypt).asDictionary();
   if (dict != null) {
     int version = 0;
     COSNumber cosVersion = dict.get(COSEncryption.DK_V).asNumber();
     if (cosVersion != null) {
       version = cosVersion.intValue();
     }
     if (version == 0) {
       systemSecurityHandler = new SystemSecurityHandlerV0(dict);
     } else if (version == 1) {
       systemSecurityHandler = new SystemSecurityHandlerV1(dict);
     } else if (version == 2) {
       systemSecurityHandler = new SystemSecurityHandlerV2(dict);
     } else if (version == 3) {
       systemSecurityHandler = new SystemSecurityHandlerV3(dict);
     } else if (version == 4) {
       systemSecurityHandler = new SystemSecurityHandlerV4(dict);
     } else {
       throw new COSSecurityException("unsupported security version " + version);
     }
     systemSecurityHandler.initialize(doc);
   }
   return systemSecurityHandler;
 }
 public static SystemSecurityHandler createNewV4() {
   COSDictionary dict = COSDictionary.create();
   dict.beIndirect();
   SystemSecurityHandler result = new SystemSecurityHandlerV4(dict);
   result.initializeFromScratch();
   return result;
 }
 public void attach(STDocument stDoc) throws COSSecurityException {
   this.stDoc = stDoc;
   currentCosTrailer = stDoc.cosGetTrailer();
   currentCosIDs = currentCosTrailer.get(COSTrailer.DK_ID).asArray();
   currentCosTrailer.put(COSTrailer.DK_Encrypt, cosGetEncryption());
   if (getSecurityHandler() != null) {
     getSecurityHandler().attach(stDoc);
   }
   forceFullWrite();
 }
 public COSCompositeObject popContextObject() {
   COSCompositeObject contextObject = contextStack[stackPtr--];
   // enable encryption when no longer in encryption dict of file id's
   if (contextObject == currentCosEncryption || contextObject == currentCosIDs) {
     enabled = true;
   }
   if (contextObject instanceof COSStream) {
     COSDictionary dict = contextObject.asStream().getDict();
     if (dict != null
         && dict.get(STStreamXRefSection.DK_Type).equals(STStreamXRefSection.CN_Type_XRef)) {
       // /XRef streams are not encrypted
       enabled = true;
     }
   }
   return contextObject;
 }
Ejemplo n.º 7
0
    /*
     * (non-Javadoc)
     *
     * @see
     * de.intarsys.pdf.cos.COSBasedObject.MetaClass#doDetermineClass(de.
     * intarsys.pdf.cos.COSObject)
     */
    @Override
    protected COSBasedObject.MetaClass doDetermineClass(COSObject object) {
      COSDictionary dict;

      dict = object.asDictionary();
      if (dict == null) {
        throw new IllegalArgumentException("font object is not a COSDictionary as required");
      }
      COSName type = dict.get(DK_Type).asName();
      if (type == null) {
        throw new IllegalArgumentException("Dictionary has no type");
      }
      if (!type.equals(CN_Type_Font)) {
        throw new IllegalArgumentException("type <" + type + "> is not a valid font type");
      }
      COSName subtype = dict.get(DK_Subtype).asName();
      if (subtype == null) {
        throw new IllegalArgumentException("font not identified by subtype");
      }
      if (subtype.equals(CN_Subtype_Type1)) {
        return PDFontType1.META;
      } else if (subtype.equals(CN_Subtype_TrueType)) {
        if (dict.get(DK_FontDescriptor).isNull()) {
          /*
           * treat as if Type1 was specified, because that's probably
           * what the creator meant; further processing would yield
           * wrong results anyway as FontDescriptor is a required
           * entry for TrueType fonts
           */
          return PDFontType1.META;
        }
        return PDFontTrueType.META;
      } else if (subtype.equals(CN_Subtype_MMType1)) {
        return PDFontMMType1.META;
      } else if (subtype.equals(CN_Subtype_Type0)) {
        return PDFontType0.META;
      } else if (subtype.equals(CN_Subtype_Type3)) {
        return PDFontType3.META;
      } else if (subtype.equals(CN_Subtype_CIDFontType0)) {
        return CIDFontType0.META;
      } else if (subtype.equals(CN_Subtype_CIDFontType2)) {
        return CIDFontType2.META;
      }
      throw new IllegalArgumentException("font subtype <" + subtype + "> not supported");
    }
 public void pushContextObject(COSCompositeObject contextObject) {
   stackPtr++;
   if (stackPtr >= contextStack.length) {
     COSCompositeObject[] tempStack = new COSCompositeObject[contextStack.length + 5];
     System.arraycopy(contextStack, 0, tempStack, 0, contextStack.length);
     contextStack = tempStack;
   }
   contextStack[stackPtr] = contextObject;
   // do not encrypt within encryption dict and file id's
   if (contextObject == currentCosEncryption || contextObject == currentCosIDs) {
     enabled = false;
   }
   if (contextObject instanceof COSStream) {
     COSDictionary dict = contextObject.asStream().getDict();
     if (dict != null
         && dict.get(STStreamXRefSection.DK_Type).equals(STStreamXRefSection.CN_Type_XRef)) {
       // /XRef streams are not encrypted
       enabled = false;
     }
   }
 }
 public void updateTrailer(COSDictionary trailer) {
   this.currentCosTrailer = trailer;
   this.currentCosIDs = currentCosTrailer.get(COSTrailer.DK_ID).asArray();
   this.currentCosEncryption = trailer.get(COSTrailer.DK_Encrypt).asDictionary();
 }