I'm trying to use the iText 5.5
library to manipulate information within a PDF. I want to scan a PDF for attachments and if it has attachments make physical copies of them (without removing/editting the original file). I'm running into an issue when there is a PDF with a .joboptions file attached.
I'm using the following 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 < 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();
}
}
}
Once it gets to PdfArray filespecs = embedded.getAsArray(PdfName.NAMES);
null is returned. I don't care if the .joboptions file is copied, however I do want the other attachments (if there are any) to be copied. Any ideas how I can get around this?
Also, if you want to create a PDF with said .joboptions file open a PDF document, go to the print menu and change the Printer to "Adobe PDF". Now select Properties, click OK and in the main print menu click Print. This will prompt you to select a location to save the document and the new document will have a .joboptions as an attachment.