简体   繁体   中英

How to write a text file in Acrobat Javascript

I am using acrobat XI I have tried output a text file like this

var cMyC = "abc";
var doc = this.createDataObject({cName: "test.txt", cValue: cMyC});
this.exportDataObject({cName: "test.txt", nLaunch:0});

This is working , but I would like to provided a fixed path and no dialog is popup to request the user choose a saving path

Are there any way to fix the problem? thanks

All Acrobat JavaScript functions that write a file to the user's local disk pose a security risk, so there are some restrictions placed on their use. These functions include doc.saveAs() and all of the data export functions, like doc.exportAsFDF() .

As you can read here :

Acrobat provides us with two modes of operation for these functions--with a path and without a path. If no path parameter is provided to the function, Acrobat displays a file-browser dialog. The file browser dialog gives users control over how data is saved to their systems. If a path is provided to the function, then no dialog is displayed and the operation is handled silently, ie, the user is not necessarily aware that data has been saved to their hard drive. This is a security problem, so to use one of these functions in silent mode, the function must be executed from a privileged context. This means the code must reside in a trusted location. For example, code executed from the Console Window, a Batch Process or a certified PDF is privileged. When any of these functions are used with a path parameter and executed in a non-privileged context, Acrobat will throw an exception. The reasoning behind this restriction is, if the code can't be trusted, then the user has to specifically select the file location.

Another restriction on saving data to the user's system is that the path specification must be a Safe Path. A safe path is one that doesn't point to a restricted location on the user's hard drive or one that might pose a security risk. Examples of these restricted locations are the system folder and the root folder of any hard drive. Other folders that might be restricted are dependent on the operating system and the sensibilities of the Acrobat developers. Neither is well documented, so it's best to use these functions carefully.

About "Safe Paths", the Acrobat JS API doc.saveAS method documentation states:

Acrobat 6.0 introduced the concept of a safe path for JavaScript methods that write data to the local hard drive based on a path passed to it by one of its parameters. A path cannot point to a system critical folder, for example a root, windows or system directory. A path is also subject to other unspecified tests. For many methods, the file name must have an extension appropriate to the type of data that is to be saved. Some methods may have a no-overwrite restriction. These additional restrictions are noted in the documentation. Generally, when a path is judged to be not safe, a NotAllowedError exception is thrown (see Error object) and the method fails.

For sure you can't do it with the exportDataObject method, since it has no path parameter, as you can also read here :

The "cName" parameter is a required input and specifies the specific file attachment that will be exported. Notice there is no path parameter. There is in fact a "cPath" input to this function, but it is no longer valid. If you try to use a path in this function, it will fail and throw an exception. It doesn't matter what context the function is called from because the "cPath" parameter was removed from all usage.

Further references:

Here's a way to output to a fixed path text file using doc.exportAsText :

// set up output text
var TEMP_FIELD_NAME = "testHeader"
var textValue = "test";
// add a temporary text field
var f = this.addField(TEMP_FIELD_NAME, "text", 0, [30,30,100,20]);
f.value = textValue;
// export field name and value to defined file
this.exportAsText({aFields: TEMP_FIELD_NAME, cPath: "test-text.txt"});
// remove text field
this.removeField(TEMP_FIELD_NAME);

The resulting text file will have two lines:

testHeader

test

Impossible. For security reasons, saving a file automatically is not allowed.

As stated in the SDK:

Beginning with Acrobat 6.0, if the parameter cDIPath is non-null, a NotAllowedError  exception is thrown and the method fails.
If cDIPath is not passed to this method, a file selection dialog box opens to allow the user to select a save path for the embedded data object.

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