* @throws IOException if an I/O error occurs
*/
public void addEmbeddedFile(PDFEmbeddedFileExtensionAttachment embeddedFile)
throws IOException {
this.pdfDoc.getProfile().verifyEmbeddedFilesAllowed();
PDFNames names = this.pdfDoc.getRoot().getNames();
if (names == null) {
//Add Names if not already present
names = this.pdfDoc.getFactory().makeNames();
this.pdfDoc.getRoot().setNames(names);
}
//Create embedded file
PDFEmbeddedFile file = new PDFEmbeddedFile();
this.pdfDoc.registerObject(file);
Source src = getUserAgent().resolveURI(embeddedFile.getSrc());
InputStream in = ImageUtil.getInputStream(src);
if (in == null) {
throw new FileNotFoundException(embeddedFile.getSrc());
}
try {
OutputStream out = file.getBufferOutputStream();
IOUtils.copyLarge(in, out);
} finally {
IOUtils.closeQuietly(in);
}
PDFDictionary dict = new PDFDictionary();
dict.put("F", file);
String filename = PDFText.toPDFString(embeddedFile.getFilename(), '_');
PDFFileSpec fileSpec = new PDFFileSpec(filename);
fileSpec.setEmbeddedFile(dict);
if (embeddedFile.getDesc() != null) {
fileSpec.setDescription(embeddedFile.getDesc());
}
this.pdfDoc.registerObject(fileSpec);
//Make sure there is an EmbeddedFiles in the Names dictionary
PDFEmbeddedFiles embeddedFiles = names.getEmbeddedFiles();
if (embeddedFiles == null) {
embeddedFiles = new PDFEmbeddedFiles();
this.pdfDoc.assignObjectNumber(embeddedFiles);
this.pdfDoc.addTrailerObject(embeddedFiles);
names.setEmbeddedFiles(embeddedFiles);
}
//Add to EmbeddedFiles in the Names dictionary
PDFArray nameArray = embeddedFiles.getNames();
if (nameArray == null) {