コード例 #1
0
ファイル: PRAcroForm.java プロジェクト: zssure-thu/mirth
  /**
   * After reading, we index all of the fields. Recursive.
   *
   * @param fieldlist An array of fields
   * @param fieldDict the last field dictionary we encountered (recursively)
   * @param title the pathname of the field, up to this point or null
   */
  protected void iterateFields(PdfArray fieldlist, PRIndirectReference fieldDict, String title) {
    for (Iterator it = fieldlist.getArrayList().iterator(); it.hasNext(); ) {
      PRIndirectReference ref = (PRIndirectReference) it.next();
      PdfDictionary dict = (PdfDictionary) PdfReader.getPdfObjectRelease(ref);

      // if we are not a field dictionary, pass our parent's values
      PRIndirectReference myFieldDict = fieldDict;
      String myTitle = title;
      PdfString tField = (PdfString) dict.get(PdfName.T);
      boolean isFieldDict = tField != null;

      if (isFieldDict) {
        myFieldDict = ref;
        if (title == null) myTitle = tField.toString();
        else myTitle = title + '.' + tField.toString();
      }

      PdfArray kids = (PdfArray) dict.get(PdfName.KIDS);
      if (kids != null) {
        pushAttrib(dict);
        iterateFields(kids, myFieldDict, myTitle);
        stack.remove(stack.size() - 1); // pop
      } else { // leaf node
        if (myFieldDict != null) {
          PdfDictionary mergedDict = (PdfDictionary) stack.get(stack.size() - 1);
          if (isFieldDict) mergedDict = mergeAttrib(mergedDict, dict);

          mergedDict.put(PdfName.T, new PdfString(myTitle));
          FieldInformation fi = new FieldInformation(myTitle, mergedDict, myFieldDict);
          fields.add(fi);
          fieldByName.put(myTitle, fi);
        }
      }
    }
  }
コード例 #2
0
  /** extracts attachments from PDF File */
  @SuppressWarnings("unchecked")
  protected Map extractAttachments(PdfReader reader) throws IOException {
    Map fileMap = new HashMap();
    PdfDictionary catalog = reader.getCatalog();
    PdfDictionary names = (PdfDictionary) PdfReader.getPdfObject(catalog.get(PdfName.NAMES));
    if (names != null) {
      PdfDictionary embFiles =
          (PdfDictionary) PdfReader.getPdfObject(names.get(new PdfName("EmbeddedFiles")));
      if (embFiles != null) {
        HashMap embMap = PdfNameTree.readTree(embFiles);
        for (Iterator i = embMap.values().iterator(); i.hasNext(); ) {
          PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject((PdfObject) i.next());
          Object fileInfo[] = unpackFile(reader, filespec);
          if (fileMap.containsKey(fileInfo[0])) {
            throw new RuntimeException(DUPLICATE_FILE_NAMES);
          }
          fileMap.put(fileInfo[0], fileInfo[1]);
        }
      }
    }
    for (int k = 1; k <= reader.getNumberOfPages(); ++k) {
      PdfArray annots = (PdfArray) PdfReader.getPdfObject(reader.getPageN(k).get(PdfName.ANNOTS));
      if (annots == null) {
        continue;
      }
      for (Iterator i = annots.getArrayList().listIterator(); i.hasNext(); ) {
        PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject((PdfObject) i.next());
        PdfName subType = (PdfName) PdfReader.getPdfObject(annot.get(PdfName.SUBTYPE));
        if (!PdfName.FILEATTACHMENT.equals(subType)) {
          continue;
        }
        PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject(annot.get(PdfName.FS));
        Object fileInfo[] = unpackFile(reader, filespec);
        if (fileMap.containsKey(fileInfo[0])) {
          throw new RuntimeException(DUPLICATE_FILE_NAMES);
        }

        fileMap.put(fileInfo[0], fileInfo[1]);
      }
    }

    return fileMap;
  }