Our Engineers can Write as well
How to attach a restricted size image file into Adobe PDF using JavaScript
In continuation of our blog series on Acrobat, we bring you the next one helping you effectively attach an image with restricted size and format into an Acrobat PDF file using JavaScript.
These are quick steps taking you there:
Scenario:

Figure(a) explains the scenario in which you may need to attach an image file to pdf with restricted size and/or format
Here’s how you should go about to make it happen.
Step-1:
In Prepare form, edit view section, the button field is available and place the field where it is required in the pdf form.

Figure(b) highlighting the button field
Step-2:
Insert the following script given in the Code Explanation Section of this blog into the document JavaScript which is under the More dropdown, in Prepare Form -> Edit view on right side pane.

Figure(c) guides us how to add the script
Step-3:
To call the methods into the field, we have set properties.

Figure(d) explains the set properties to call methods
Step-4:
In properties, we have set two actions. Mouse down and On Focus as represented in the below image.

Step-5:
On Mouse down, set the following code.
Code Below:
// tool support
if (app.viewerVersion < 11) {
app.alert(“Your Editor Does not support the File Attachment”);
} else {
import_11();
}
Step-6:
On Focus, set the following code.
Code Below:
//function to check the field missing
checkAlreadyHaveAttachment(“You are missing this field”);
Coding Explanation:
Main Script in document JavaScript.
Code Below:
//Initial location of file attachment icon
//The x value is incremented below
var oFAP = {x_init: -72, x: -72, y: -30}; // Below lower-left corner of the page
//remove the existing file from the attachment.
function removeAllAtachments(isRemoveRecentAttachment){
this.syncAnnotScan();
var annots = this.getAnnots({nSortBy:ANSB_ModDate});
if (annots!=null && annots.length!=0){
if(isRemoveRecentAttachment){
annots[0].destroy();
}
if(!isRemoveRecentAttachment && annots.length>1)
annots[annots.length – 1].destroy();
}
}
/*
* Perform attach functionality
* @returns
*/
function attachFile(){
// addAnnot is object which capture the file as an object.
// we store them to the local variable.
var annot = addAnnot({
page: event.target.page,
type: “FileAttachment”,
author: “Form user”,
name: “File Attachment”,
point: [oFAP.x, oFAP.y],
contents: “File attachment on: ” + util.printd(“yyyy/mm/dd HH:MM:ss”, new Date()),
attachIcon: “Paperclip”
});
//condition check for the format match
var attachmentObj = annot.attachment;
if (attachmentObj !== null) {
if(attachmentObj.MIMEType == null || (attachmentObj.MIMEType.toUpperCase() != “image/tiff”.toUpperCase() && attachmentObj.MIMEType.toUpperCase() != “image/x-png”.toUpperCase() && attachmentObj.MIMEType.toUpperCase() != “image/png”.toUpperCase() && attachmentObj.MIMEType.toUpperCase() != “image/jpg”.toUpperCase() && attachmentObj.MIMEType.toUpperCase() != “image/jpeg”.toUpperCase())){
app.alert(“Your Image file doesn’t fit the specified format.”);
removeAllAtachments(true);
// condition for the size restriction.
}else if(attachmentObj.size > 307200 || attachmentObj.size < 10240 ){
app.alert(“Your Image File does’nt fit within 10kb-30kb.”);
removeAllAtachments(true);
}else{
app.alert(“Image File successfully uploaded.”);
this.getField(“button”).borderColor = color.black;
}
removeAllAtachments(false);
}
oFAP.x += 18;
}
/**
* Check if image is already attached
* @param alertMessage
* @returns
*/
function checkAlreadyHaveAttachment(alertMessage){
var isHavingAttachment = false;
try{
this.syncAnnotScan();
annots_2 = this.getAnnots();
if(annots_2 && annots_2 != null && annots_2 != “” && annots_2.length > 0){
isHavingAttachment = true;
}
}catch(e){
isHavingAttachment = true;
}
if(!isHavingAttachment){
app.alert(alertMessage);
this.getField(“button”).setFocus();
this.getField(“button”).borderColor = color.red;
}else{
this.getField(“button”).borderColor = color.transparent;
}
return isHavingAttachment;
}
//attach the file to annot object
function import_11() {
try {
var annots_1 = “”;
try{
this.syncAnnotScan();
annots_1 = this.getAnnots();
}catch(e){
}
// condition check for the file is already attached.
if(annots_1 && annots_1 != null && annots_1 != “” && annots_1.length > 0){
var retVal = app.alert(“Do you really want to overwrite the Image File.”, 1, 2, “FFi”);
if(retVal == 4){
attachFile();
}
}else{
attachFile();
}
} catch (e) {
}
}
So, next time you wanted to insert or import a restricted size and format image into an Adobe Acrobat file, these steps hopefully comes for your rescue.
For more such insightful tit-bytes, keep watching this space.

Contributed by:
Ramkumar
Software Engineer