Skip to main content
Bradshaw_Ian_Michael
Participant
Feb 27, 2018 9:50 PM UTC
Question

Using Javascript to export a page level file attachment

Hello and thank you in advance for any light you can shine on this subject for me.

I'm creating a form in acrobat pro 10 that I want the users to be able to attach files to.

I don't know if the user will be filling out the form using Acrobat Pro or Reader so I'm using a logic statement in the document level Javascript to determine whether to use the "ImportDataObject" (Pro) or "AddAnot" (Reader) method.

The problem that I'm running into is that while I can export document level attachments using the "this.exportDataObject" function by referencing the name passed to the "ImportDataOjbect" function... I don't know how to export a page level file attachment added using the "AddAnot" function.

Really appreciate any help. I've written code in Visual Basic but Java Script is brand new to me, as is programming for Acrobat forms.

Document Level Java Script Added Below for Reference...

var oFAP = {x_init: -72, x: -72, y: -72}

function Attach(FileName) {

if (app.viewerType === "Reader")

    try {

        var annot = addAnnot({

            page: event.target.page,

            type: "FileAttachment",

            author: FileName,

            name: FileName,

            point: [oFAP.x, oFAP.y],

            contents: FileName,

            attachIcon: "Paperclip"

        });

        annot.cAttachmentPath; // Prompt user to select a file to attach

        oFAP.x += 18;  // Increment the x location for the icon

    } catch (e) {

        app.alert({

            cMsg: "Could not attach file.\r\rPlease report this error.",

            cTitle: "File attachment error",

            nIcon: 3,

            nType: 0

        });

    }

else

{this.importDataObject(FileName)}

}

This topic is closed to new replies. Start a new post to keep the conversation going.

6 replies

Participating Frequently
Sep 29, 2018 11:06 AM UTC

Is there any way we can do this...I tried but I am getting the below message.

TypeError: Invalid argument type.

Doc.setDataObjectContents:31:AcroForm:Button1:Annot1:MouseUp:Action1

===> Parameter cName.

The code I tried.. Any kind of help is appreciated...

var annot = this.addAnnot({


   page: 0,


   type: "FileAttachment",


   point: [400,500],


   author: "A. C. Robat",


   contents: "Call Smith to get help on this paragraph.",


   /*cAttachmentPath: "/d/a.pdf" ,*/


});


var attachObj = annot.attachment;

cName = attachObj.name;

console.println("Name: " + attachObj.name);


console.println("Type: " + attachObj.MIMEType);


console.println("Size: "+ attachObj.size);


console.println("==============Content=================");


var stm = attachObj.contentStream;


stringStream = util.stringFromStream(stm, "utf-8");

fileStream = util.streamFromString(stringStream, "utf-8");

this.setDataObjectContents(cName, fileStream);

Translate
try67
Community Expert
Community Expert
Sep 29, 2018 12:16 PM UTC

No, you can't do it. The attachment property is defined as Read-Only.

Translate
try67
Community Expert
Community Expert
Feb 27, 2018 10:12 PM UTC

AFAIK, it can't be done using JS.

Translate
Thom Parker
Community Expert
Community Expert
Feb 27, 2018 10:34 PM UTC

Actually you can do this, just not directly.

1. The Attachment annotation has an "attachment" property which is an "annotAttachment" object. Look it up in the JS reference.

2. This object has a contentStream property.  Use this property to create a regular attachment using the "doc.setDatObjectContents()" function.

3. From there you treat it as a regular file attachment.

4. Viola

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Bradshaw_Ian_Michael
Participant
Feb 28, 2018 3:39 PM UTC

Having a hard time trying to put this into practice. Any chance you could provide an example? Thank you so much!

Translate