0
votes

I am developing .NET Application using pdfbox to extract metadata, content and attached file from PDF.I was able to extract metadata and content, but stuck while extracting attached/embedded files.

I have a pdf with embedded/attached doc file and want to retrieve that file. I have gone through the java example - http://svn.apache.org/repos/asf/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java . But while trying to use it in .Net, i got "non generic type 'java.util.Map' cannot be used with type arguments" in the following code snippet

java.util.Map<String, COSObjectable> names = efTree.getNames();

So, i will be grateful if anybody help me to extract the file from pdf.

Thanks in advance.

2

2 Answers

0
votes
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;

public class PDFAttachments {

    public PDFAttachments() {

    }

    public void extractAttachments(String src, String dest) throws IOException {
        PdfReader reader = new PdfReader(src);
        PdfArray array;
        PdfDictionary annot;
        PdfDictionary fs;
        PdfDictionary refs;
        String fName;
        try {
            for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                array = reader.getPageN(i).getAsArray(PdfName.ANNOTS);
                if (array == null)
                    continue;
                for (int j = 0; j < array.size(); j++) {
                    annot = array.getAsDict(j);
                    if (PdfName.FILEATTACHMENT.equals(annot
                            .getAsName(PdfName.SUBTYPE))) {
                        fs = annot.getAsDict(PdfName.FS);
                        refs = fs.getAsDict(PdfName.EF);
                        for (PdfName name : refs.getKeys()) {
                            fName = dest + fs.getAsString(name).toString();
                            /*
                             * FileOutputStream fos = new
                             * FileOutputStream(String.format(dest,
                             * fs.getAsString(name).toString()));
                             */
                            FileOutputStream fos = new FileOutputStream(fName);
                            fos.write(PdfReader.getStreamBytes((PRStream) refs
                                    .getAsStream(name)));
                            fos.flush();
                            fos.close();
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.err.println("exception " + e.getMessage());
        }
    }

}
}
0
votes

I have solved it by omitting the generics and trying something like this:

java.util.Map names = efTree.getNames();

Now i am able to extract the attached file located in the attachments tab but haven't been able to extract the attached file located in page. I am getting null efTree in this case.

PDDocumentNameDictionary namesDictionary = new PDDocumentNameDictionary(pdfDoc.getDocumentCatalog());
PDEmbeddedFilesNameTreeNode efTree=namesDictionary.getEmbeddedFiles();

So if anybody knows how to extract file attached/embedded in the page can help me to do it in .NET Application.