title: Extracting the embedded attachments in PDF
body: <pre><code>import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  
  import com.itextpdf.text.pdf.PRStream;
  import com.itextpdf.text.pdf.PdfArray;
  import com.itextpdf.text.pdf.PdfDictionary;
  import com.itextpdf.text.pdf.PdfName;
  import com.itextpdf.text.pdf.PdfReader;
  import com.itextpdf.text.pdf.PdfString;
  
  public class extractAttachments{
  
  public extractAttachments(String src, String dir) throws IOException {
  
      File folder = new File(dir);
         folder.mkdirs();
         PdfReader reader = new PdfReader(src);
         PdfDictionary root = reader.getCatalog();
         PdfDictionary names = root.getAsDict(PdfName.NAMES);
         PdfDictionary embedded = names.getAsDict(PdfName.EMBEDDEDFILES);
         PdfArray filespecs = embedded.getAsArray(PdfName.NAMES);
         for (int i = 0; i &lt; filespecs.size(); ) {
           extractAttachment(reader, folder, filespecs.getAsString(i++),
           filespecs.getAsDict(i++));
         }
       }
  
      protected void extractAttachment(PdfReader reader, File dir, PdfString name, PdfDictionary filespec)
         throws IOException {
         PRStream stream;
         FileOutputStream fos;
         String filename;
         PdfDictionary refs = filespec.getAsDict(PdfName.EF);
         for (PdfName key : refs.getKeys()) {
           stream = (PRStream)PdfReader.getPdfObject(refs.getAsIndirectObject(key));
           filename = filespec.getAsString(key).toString();
           fos = new FileOutputStream(new File(dir, filename));
           fos.write(PdfReader.getStreamBytes(stream));
           fos.flush();
           fos.close();
         }
       }
    }
  </code></pre>
  
  <p>I have tried the above code for extracting the embedded attachment from the PDF.but it is not working for the PDF that is having wordDocument. I am using itext5.It is working for the PDF having text document.  What can be the issue?</p>