简体   繁体   中英

Check if file exists using Acrobat javascript

I'm trying to determine if a specific .xml file exists on my desktop before running additional code. The following trusted script is in my Javascripts folder-level directory. The section commented as "First Part" below simply sets all fields of the form to be open/writable. The second part looks for a .xml file in which to import XFA data from.

My problem is that I'm trying to determine whether this .xml file exists in the first place. Right now if it doesn't exist, it displays a dialog window to browse for the file. Instead I'd like it to show an alert if the xml file does not exist, and never show the dialog at all . What am I doing wrong?

Any help is huge, thanks

CODE:

var myTrustFunctTwo = app.trustedFunction(function(doc)
{

//First Part
  for (var nPageCount = 0; nPageCount < doc.numPages; nPageCount++) {
        var oFields = doc.xfa.layout.pageContent(nPageCount, "field");
  //app.alert(oFields,0);
        var nNodesLength = oFields.length;
        // Set the field property.
        for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++) {
            oFields.item(nNodeCount).access = "open";
        }
    }

//Second Part
app.beginPriv();
doc.importXFAData({
          cPath:"/c/Users/ME/Desktop/Filled_In.xml"
                 });
app.endPriv();
});
app.trustedFunction(myTrustFunctTwo);

UPDATE CODE INCLUDING TRY/CATCH BLOCKS

var myTrustFunctTwo = app.trustedFunction(function(doc) {

    for (var nPageCount = 0; nPageCount < doc.numPages; nPageCount++) {
        var oFields = doc.xfa.layout.pageContent(nPageCount, "field");
        //app.alert(oFields,0);
        var nNodesLength = oFields.length;
        // Set the field property.
        for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++) {
            oFields.item(nNodeCount).access = "open";
        }
    }


    try {

        app.beginPriv();
        doc.importXFAData({
            cPath: "/c/Users/ME/Desktop/Filled_In.xml"
        });
        app.endPriv();

    } catch (e) {
        app.alert("No File Found", 1);
    }


});

app.trustedFunction(myTrustFunctTwo);

If the file you try to import does not exist, you will get an error message. Using try…catch , you can catch that message and exit the function, thus prevent the subsequent code from running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM