|
private AnnotationContainer createFileEmbedLinkAnnotation( |
|
Element elem, String uri) { |
|
PDComplexFileSpecification fs = _embeddedFiles.get(uri); |
|
|
|
if (fs != null) { |
|
PDAnnotationFileAttachment annotationFileAttachment = new PDAnnotationFileAttachment(); |
|
|
|
annotationFileAttachment.setFile(fs); |
|
annotationFileAttachment.setAppearance(this._embeddedFileAppearance); |
|
|
|
return new AnnotationContainer.PDAnnotationFileAttachmentContainer(annotationFileAttachment); |
|
} |
|
|
|
byte[] file = _sharedContext.getUserAgentCallback().getBinaryResource(uri, ExternalResourceType.FILE_EMBED); |
|
|
|
if (file != null) { |
|
try { |
|
String contentType = elem.getAttribute("data-content-type").isEmpty() ? |
|
"application/octet-stream" : |
|
elem.getAttribute("data-content-type"); |
|
|
|
PDEmbeddedFile embeddedFile = new PDEmbeddedFile(_od.getWriter(), new ByteArrayInputStream(file)); |
|
embeddedFile.setSubtype(contentType); |
|
embeddedFile.setSize(file.length); |
|
|
|
// PDF/A3 requires a mod date for the file. |
|
if (elem.hasAttribute("relationship")) { |
|
// FIXME: Should we make this specifiable. |
|
embeddedFile.setModDate(Calendar.getInstance()); |
|
} |
|
|
|
String fileName = elem.getAttribute("download"); |
|
|
|
fs = new PDComplexFileSpecification(); |
|
fs.setEmbeddedFile(embeddedFile); |
|
fs.setFile(fileName); |
|
fs.setFileUnicode(fileName); |
|
|
|
// The PDF/A3 standard requires one to specify the relationship |
|
// this embedded file has to the link annotation. |
|
if (elem.hasAttribute("relationship") && |
|
Arrays.asList("Source", "Supplement", "Data", "Alternative", "Unspecified") |
|
.contains(elem.getAttribute("relationship"))) { |
|
fs.getCOSObject().setItem( |
|
COSName.getPDFName("AFRelationship"), |
|
COSName.getPDFName(elem.getAttribute("relationship"))); |
|
} |
|
|
|
if (elem.hasAttribute("title")) { |
|
fs.setFileDescription(elem.getAttribute("title")); |
|
} |
|
|
|
this._embeddedFiles.put(uri, fs); |
|
|
|
if (this._embeddedFileAppearance == null) { |
|
this._embeddedFileAppearance = createFileEmbedLinkAppearance(); |
|
} |
|
|
|
PDAnnotationFileAttachment annotationFileAttachment = new PDAnnotationFileAttachment(); |
|
|
|
annotationFileAttachment.setFile(fs); |
|
annotationFileAttachment.setAppearance(this._embeddedFileAppearance); |
|
|
|
// PDF/A3 requires we explicitly list this link as associated with file. |
|
if (elem.hasAttribute("relationship")) { |
|
COSArray fileRefArray = new COSArray(); |
|
fileRefArray.add(fs); |
|
|
|
annotationFileAttachment.getCOSObject().setItem(COSName.getPDFName("AF"), fileRefArray); |
|
} |
|
|
|
return new AnnotationContainer.PDAnnotationFileAttachmentContainer(annotationFileAttachment); |
|
} catch (IOException e) { |
|
XRLog.log(Level.WARNING, LogMessageId.LogMessageId1Param.EXCEPTION_COULD_NOT_LOAD_EMBEDDED_FILE, uri, e); |
|
} |
|
} else { |
|
XRLog.log(Level.WARNING, LogMessageId.LogMessageId1Param.LOAD_COULD_NOT_LOAD_EMBEDDED_FILE, uri); |
|
} |
|
|
|
return null; |
|
} |