Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to Filter the Various Documents Attached to PDF?

Avatar

Level 4

Hello Folks,

                 I am wondering whether we can restrict some documents attaching to a PDF or not. For example, i can add only Microsoft Word documents to PDF and ignore reset of all other documents. Please let me know how should i generate this type of functionality.

Thanks in Advance

Rajesh

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi Rajesh,

If you are using a button for the user to add an attachment then you can use some JavaScript code like this in the click event;

var attachmentName = "SupportDocument"

var validTypes = 'doc,docx,xls,xlsx'

var pdf = event.target;

if (pdf.importDataObject(attachmentName))

{

    var attachment = pdf.getDataObject(attachmentName);

    var filetype = attachment.path.replace(/(.*)\.([a-zA-Z]+)$/, '$2')

    if (validTypes.split(',').indexOf(filetype) === -1)

    {

        pdf.removeDataObject(attachmentName);

        app.alert('invalid file type');

    }  

}

You will still have to check when they submit that they haven't added an attachment via the builtin Adobe attachment pane.  Something like;

var pdf = event.target;

if (pdf.dataObjects !== null)

{

   for (var i = 0; i < pdf.dataObjects.length; i++)

    {

        if (pdf.dataObjects[i].name === pdf.dataObjects[i].path)

        {

            app.alert("you have an invalid attachment");

                        break;

        }

    }

}

This relies on the name property being set to the path if using the attachment pane or the arguement to the importDataObject function if using code.  You may want to keep you own record of the attachments added via script as this behaviour could be changed by Adobe in a future release.

I don't want to make it sound like I know what Adobe might do, just that the name property used to be set to Untitled when using the attachment pane.

Regards

Bruce

View solution in original post

2 Replies

Avatar

Correct answer by
Level 10

Hi Rajesh,

If you are using a button for the user to add an attachment then you can use some JavaScript code like this in the click event;

var attachmentName = "SupportDocument"

var validTypes = 'doc,docx,xls,xlsx'

var pdf = event.target;

if (pdf.importDataObject(attachmentName))

{

    var attachment = pdf.getDataObject(attachmentName);

    var filetype = attachment.path.replace(/(.*)\.([a-zA-Z]+)$/, '$2')

    if (validTypes.split(',').indexOf(filetype) === -1)

    {

        pdf.removeDataObject(attachmentName);

        app.alert('invalid file type');

    }  

}

You will still have to check when they submit that they haven't added an attachment via the builtin Adobe attachment pane.  Something like;

var pdf = event.target;

if (pdf.dataObjects !== null)

{

   for (var i = 0; i < pdf.dataObjects.length; i++)

    {

        if (pdf.dataObjects[i].name === pdf.dataObjects[i].path)

        {

            app.alert("you have an invalid attachment");

                        break;

        }

    }

}

This relies on the name property being set to the path if using the attachment pane or the arguement to the importDataObject function if using code.  You may want to keep you own record of the attachments added via script as this behaviour could be changed by Adobe in a future release.

I don't want to make it sound like I know what Adobe might do, just that the name property used to be set to Untitled when using the attachment pane.

Regards

Bruce

Avatar

Level 4

Thank you, very much. it is very helpful.

I have created a sample which works file