Exemplo n.º 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;
  }
Exemplo n.º 2
0
  /**
   * Unpacks a file attachment.
   *
   * @param reader The object that reads the PDF document
   * @param filespec The dictionary containing the file specifications
   * @param outPath The path where the attachment has to be written
   * @throws IOException
   */
  private static byte[] unpackFile(PdfReader reader, PdfDictionary filespec, String suffix)
      throws IOException {
    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());
    String filename = fLast.getName();

    if (!filename.endsWith(suffix)) {
      return null;
    }

    PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
    if (prs == null) {
      return null;
    }
    return PdfReader.getStreamBytes(prs);
  }
Exemplo n.º 3
0
 /**
  * Unpacks a file attachment.
  *
  * @param reader The object that reads the PDF document
  * @param filespec The dictionary containing the file specifications
  * @param outPath The path where the attachment has to be written
  * @throws IOException
  */
 public static void unpackFile(PdfReader reader, PdfDictionary filespec, String outPath)
     throws IOException {
   if (filespec == null) return;
   PdfName type = filespec.getAsName(PdfName.TYPE);
   if (!PdfName.F.equals(type) && !PdfName.FILESPEC.equals(type)) return;
   PdfDictionary ef = filespec.getAsDict(PdfName.EF);
   if (ef == null) return;
   PdfString fn = filespec.getAsString(PdfName.F);
   System.out.println("Unpacking file '" + fn + "' to " + outPath);
   if (fn == null) return;
   File fLast = new File(fn.toUnicodeString());
   File fullPath = new File(outPath, fLast.getName());
   if (fullPath.exists()) return;
   PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
   if (prs == null) return;
   byte b[] = PdfReader.getStreamBytes(prs);
   FileOutputStream fout = new FileOutputStream(fullPath);
   fout.write(b);
   fout.close();
 }