コード例 #1
0
  /**
   * Unpacks a file attachment.
   *
   * @param reader The object that reads the PDF document
   * @param filespec The dictonary containing the file specifications
   * @throws IOException
   */
  protected static Object[] unpackFile(PdfReader reader, PdfDictionary filespec)
      throws IOException {
    Object arr[] = new Object[2]; // use to store name and file bytes
    if (filespec == null) {
      return null;
    }

    PdfName type = (PdfName) PdfReader.getPdfObject(filespec.get(PdfName.TYPE));
    if (!PdfName.F.equals(type) && !PdfName.FILESPEC.equals(type)) {
      return null;
    }

    PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(filespec.get(PdfName.EF));
    if (ef == null) {
      return null;
    }

    PdfString fn = (PdfString) PdfReader.getPdfObject(filespec.get(PdfName.F));
    if (fn == null) {
      return null;
    }

    File fLast = new File(fn.toUnicodeString());
    PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
    if (prs == null) {
      return null;
    }

    byte attachmentByte[] = PdfReader.getStreamBytes(prs);
    arr[0] = fLast.getName();
    arr[1] = attachmentByte;

    return arr;
  }