Some test text!
Web / Guides / File Attachment
File attachment annotations are annotations that represent an attached file in the document. Users can annotate their documents with files that can also be downloaded.
File attachment are created with NoZoom
and NoRotate
set to true
by default. This means they will remain the same size at all zoom levels and will not rotate with the view/page. Stroke color is also used as the primary color of the icons as opposed to fill color.
In the PDF specification, there are no specifications for file restrictions or attachment size limits. WebViewer has provided options to tweaks these from the tool that creates file attachment annotations.
WebViewer(...)
.then(instance => {
const { documentViewer, annotationManager, Annotations } = instance.Core;
documentViewer.addEventListener('annotationsLoaded', async () => {
const annot = new Annotations.FileAttachmentAnnotation({
PageNumber: 1,
X: 100,
Y: 50,
Icon: 'Paperclip',
StrokeColor: new Annotations.Color(0, 255, 0, 1),
});
const res = await fetch('https://myserver.com/files/test.pdf');
const buffer = await res.arrayBuffer();
await annot.setFileData(buffer, 'application/pdf', 'test.pdf');
annotationManager.addAnnotation(annot);
annotationManager.redrawAnnotation(annot);
});
});
Element name: fileattachment
<fileattachment page="0" rect="302.410,609.110,316.410,643.110" color="#000000" flags="print,nozoom,norotate" name="c93af20d-a823-b04b-6cae-de26e3202304" title="Guest" subject="File Attachment" date="D:20220721151359-07'00'" creationdate="D:20220721151349-07'00'" icon="Paperclip" mimetype="application/pdf" file="test.pdf">
<data mode="raw" filter="FlateDecode" encoding="hex" length="4561">...</data>
</fileattachment>
Gets or sets the page number of a document that the annotation appears on.
Gets or sets the annotation's x-axis position.
Gets or sets the annotation's y-axis position.
For the full list of properties, please visit the annotation's API docs.
The name of the icon to use for this file attachment annotation. Possible default icon types:
The author of the annotation.
Gets or sets the annotation's stroke color.
Gets or sets whether the annotation is hidden.
Gets or sets whether the annotation is invisible, only if it is an unknown annotation type. Generally for hiding annotations you should use "Hidden".
Gets or sets whether any parts of the annotation drawn outside of the rect are clickable.
Gets or sets whether the annotation should be listed in annotation lists. If set to false, the annotation will also become unselectable.
Gets or sets whether the annotation is locked or not. If it's locked it can't be edited or deleted, but the note can be edited.
Gets or sets whether the annotation contents are locked or not. If the contents are locked then note can't be edited but the annotation can be edited or deleted.
Gets or sets if this annotation can be deleted.
Gets or sets whether or not the annotation can be moved.
Gets or sets if this annotation can be resized by the user.
Gets or sets if this annotation can be rotated.
Gets or sets whether the annotation is visible on the screen. Differs from Hidden in that it can still be printed if the print flag is set.
Gets or sets if this annotation scales with the page.
Gets or sets the opacity of the annotation.
Gets or sets whether the annotation should be displayed when printing the page.
Gets or sets whether the annotation is readonly or not. If it's readonly both the annotation itself and its note can't be edited or deleted.
Gets or sets the color of the annotation's stroke.
Gets or sets whether the ToggleNoView flag is set on the annotation.
To get the file data from an attachment annotation, you can use getFileData
which will asynchronously return the data as a blob. This can be used to trigger an automatic download of an attachment.
WebViewer(...)
.then(instance => {
const { documentViewer, annotationManager, Annotations } = instance.Core;
documentViewer.addEventListener('annotationsLoaded', () => {
const annotList = annotationManager.getAnnotationsList();
annotList.forEach(async annot => {
if (annot instanceof Annotations.FileAttachmentAnnotation) {
const fileData = await annot.getFileData();
const url = URL.createObjectURL(fileData);
window.open(url);
}
});
});
});
Setting the file data on a file attachment annotation is recommended after construction using setFileData
. This requires slightly more information other than just the file data, as you will need to provide the web mimetype of the attachment and filename. The filename can be different and will be used when downloaded.
If you need to understand the type of file that is stored in the attachment, you use getFileMetadata
to get the mimetype and name of the file from when it was originally attached.
WebViewer(...)
.then(instance => {
const { documentViewer, annotationManager, Annotations } = instance.Core;
documentViewer.addEventListener('annotationsLoaded', () => {
const annotList = annotationManager.getAnnotationsList();
annotList.forEach(async annot => {
if (annot instanceof Annotations.FileAttachmentAnnotation) {
const metadata = annot.getFileMetadata();
console.log(metadata.filename, metadata.mimeType);
}
});
});
});
Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales