Automating SaveAs Word docx

Enthusiast ,
Jul 11, 2017

Copy link to clipboard

Copied

As part of a FrameMaker based publishing system that takes XML files then outputs PDF and optionally Word files, we have to improve the quality of the Word output. The previous process used FrameMaker's RTF output, but the results were not good enough without significant post processing. In an attempt to give the customer what they need I found that Acrobat's PDF output was good enough, so all we need to do is automate the process.

My developer created a folder level JavaScript that converts the PDF file which almost does what we need. However it does give an error dialog which may or may not prevent the export of the Word document. Here is the JavaScript:

/**

* trustedPrint function: Exercise the print function in the privileged context

*

* @param doc The Doc object of the target document

* @param pparam The PrintParams object containing print settings

*/

trustedSave = app.trustedFunction(

function(input) {

app.beginPriv();

try

{

var rs = util.readFileIntoStream(input);

var path = util.stringFromStream(rs);

var oDoc = app.openDoc({

cPath: path + ".pdf",

bHidden: true

});

oDoc.saveAs(path + ".docx", "com.adobe.acrobat.docx");

oDoc.saveAs(path + ".done.pdf");

oDoc.closeDoc();

}

catch(e)

{

console.println("[save2word] unable to process: " + input);

for (var i in e)

console.println( i + ": " + e);

}

app.endPriv();

}

);

trustedSave("/C/publiframe/api/pdf2word.txt");

The path to pdf2word.txt provides a way to introduce the path for the source PDF file.

When this is used we see an Acrobat dialog that tells us that a file cannot be found:

AcroDCerror.png

The JavaScript debugger tells us this, but what does it all mean? (The line numbers may not match the JavaScript above):

Invalid Location "/C/Users/itproudfoot/AppData/Local/Temp/CIMP000102231_EN.word.pdf". File or folder does not exist.Exception in line 23 of function trustedSave<, script Folder-Level:App:save2word.js

[save2word] unable to process: /C/publiframe/api/pdf2word.txt

name: NotAllowedError

message: Security settings prevent access to this property or method.

extMessage: NotAllowedError: Security settings prevent access to this property or method.

App.openDoc:23:Folder-Level:App:save2word.js

fileName: Folder-Level:App:save2word.js

lineNumber: 23

number: 11

columnNumber: 0

Despite the error message the Word document is created correctly on my system. On the client's system and our test system the output fails.

For business reasons this needs to work with AcrobatDC and Acrobat XI.

I hope this is a simple error, and that we're not trying to do something that is not permitted.

Thanks for any help.

Ian

TOPICS
Acrobat SDK and JavaScript, Windows

Views

915

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Automating SaveAs Word docx

Enthusiast ,
Jul 11, 2017

Copy link to clipboard

Copied

As part of a FrameMaker based publishing system that takes XML files then outputs PDF and optionally Word files, we have to improve the quality of the Word output. The previous process used FrameMaker's RTF output, but the results were not good enough without significant post processing. In an attempt to give the customer what they need I found that Acrobat's PDF output was good enough, so all we need to do is automate the process.

My developer created a folder level JavaScript that converts the PDF file which almost does what we need. However it does give an error dialog which may or may not prevent the export of the Word document. Here is the JavaScript:

/**

* trustedPrint function: Exercise the print function in the privileged context

*

* @param doc The Doc object of the target document

* @param pparam The PrintParams object containing print settings

*/

trustedSave = app.trustedFunction(

function(input) {

app.beginPriv();

try

{

var rs = util.readFileIntoStream(input);

var path = util.stringFromStream(rs);

var oDoc = app.openDoc({

cPath: path + ".pdf",

bHidden: true

});

oDoc.saveAs(path + ".docx", "com.adobe.acrobat.docx");

oDoc.saveAs(path + ".done.pdf");

oDoc.closeDoc();

}

catch(e)

{

console.println("[save2word] unable to process: " + input);

for (var i in e)

console.println( i + ": " + e);

}

app.endPriv();

}

);

trustedSave("/C/publiframe/api/pdf2word.txt");

The path to pdf2word.txt provides a way to introduce the path for the source PDF file.

When this is used we see an Acrobat dialog that tells us that a file cannot be found:

AcroDCerror.png

The JavaScript debugger tells us this, but what does it all mean? (The line numbers may not match the JavaScript above):

Invalid Location "/C/Users/itproudfoot/AppData/Local/Temp/CIMP000102231_EN.word.pdf". File or folder does not exist.Exception in line 23 of function trustedSave<, script Folder-Level:App:save2word.js

[save2word] unable to process: /C/publiframe/api/pdf2word.txt

name: NotAllowedError

message: Security settings prevent access to this property or method.

extMessage: NotAllowedError: Security settings prevent access to this property or method.

App.openDoc:23:Folder-Level:App:save2word.js

fileName: Folder-Level:App:save2word.js

lineNumber: 23

number: 11

columnNumber: 0

Despite the error message the Word document is created correctly on my system. On the client's system and our test system the output fails.

For business reasons this needs to work with AcrobatDC and Acrobat XI.

I hope this is a simple error, and that we're not trying to do something that is not permitted.

Thanks for any help.

Ian

TOPICS
Acrobat SDK and JavaScript, Windows

Views

916

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 11, 2017 0
Most Valuable Participant ,
Jul 11, 2017

Copy link to clipboard

Copied

Not sure if that's the issue, but try renaming the "path" variable. There's already a property of the Document object with that name and it could cause conflicts...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 11, 2017 0
Enthusiast ,
Jul 11, 2017

Copy link to clipboard

Copied

Thanks for the idea try67. Unfortunately it made no difference when I used a different name for the variable.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 11, 2017 0
Most Valuable Participant ,
Jul 11, 2017

Copy link to clipboard

Copied

It's possible the folder you're trying to save the file to is considered "unsafe". Try hard-coding it to save under some other folder (like C:\Temp\), and see if that helps...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 11, 2017 0
Enthusiast ,
Jul 11, 2017

Copy link to clipboard

Copied

Thanks for the suggestion Try67. Still no luck, but maybe I shouldn't be surprised? I just noticed this in the documentation for openDoc():

Note:The app.openDoc API cannot be called from a doc open script. A script which is executed on the doc open action is known as doc open script. In such cases, Viewer raises a NotAllowedError exception, which escript passes to the user. However, it can be called successfully from other triggers, such as a page open action.

Does this mean a folder level doc open script? Or am I getting my terminology confused?

Of course if this an illegal action, why is the Word document being correctly created?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 11, 2017 0
Most Valuable Participant ,
Jul 11, 2017

Copy link to clipboard

Copied

No, it means a doc-level script, ie a script that is executed each time the file is opened. Is that when you're calling this code? If not, when is it triggered?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 11, 2017 0
Enthusiast ,
Jul 11, 2017

Copy link to clipboard

Copied

This script is triggered as a folder level script, so only when Acrobat is opened. It should then open the required PDF file convert it to Word then exit.

I found this old thread which seems to cover the same problem: More about app.openDoc However that thread fades away without a real resolution. Looks like I may be trying to do something that can't be done?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 11, 2017 0
Most Valuable Participant ,
Jul 11, 2017

Copy link to clipboard

Copied

That's surely too early to open a document.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 11, 2017 0
Enthusiast ,
Jul 11, 2017

Copy link to clipboard

Copied

I did wonder about that, but it's not clear what the alternative could be. Any ideas?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 11, 2017 0
Most Valuable Participant ,
Jul 12, 2017

Copy link to clipboard

Copied

Folder level scripts are to set global variables, functions (including trusted functions). You can add a tool button. Once Acrobat has started the user can click it.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 12, 2017 0
Most Valuable Participant ,
Jul 12, 2017

Copy link to clipboard

Copied

Besides, your strategy (typo fixed) overlooks that Acrobat is a general resource for the user, who might already have a different document open.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 12, 2017 0
Enthusiast ,
Jul 12, 2017

Copy link to clipboard

Copied

I agree that in normal circumstances it would be wrong to hijack Acrobat, but in this case there will never be another user running Acrobat on this computer. It has only been installed for its Word output abilities.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 12, 2017 0
Enthusiast ,
Jul 12, 2017

Copy link to clipboard

Copied

But this is a trusted function...
In the documentation there are examples or suggestions of how folder level scripts can be used, but no specific statement that it can't do what we are attempting...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 12, 2017 0
Enthusiast ,
Jul 12, 2017

Copy link to clipboard

Copied

I am willing to look at this from a different direction if what we are trying is impossible. We have a Launcher application that manages a publishing job. Its normal role is to compile a series of XML files into a FrameMaker book then create a PDF which is sent to an output folder. Optionally a Word docx file can be created with slightly different formatting. This is saved as another PDF file in the users temp folder. It's this temporary file that needs to be converted to Word and then moved to the output folder.

Why did I choose Acrobat to create the Word output? Because I could find nothing else that retained the look, feel and functionality of the original document. The customer was very happy with the manually exported Word documents.

Thanks for all of the comments so far. I would love to get some ideas for how to move forward.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 12, 2017 0
Most Valuable Participant ,
Jul 12, 2017

Copy link to clipboard

Copied

What you're doing is indeed not conventional, but I don't see a reason it shouldn't work.
In fact, I tried your exact code on my machine and it worked just fine. It's very difficult to help you without seeing the actual error in action.

Did you get the same error message when you converted a file from the C:\Temp\ folder, as I've suggested?

What is the full error message text?

Also, you might have a synchronization issue. Saving a PDF as DOCX can take some time. It's possible that while this process is running the script continues to the next lines of codes, which eventually close the original file you're converting. That could cause an issue...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 12, 2017 1
Enthusiast ,
Jul 12, 2017

Copy link to clipboard

Copied

Try67, Yes the same error message popped-up when I used C:\Temp. The full error message is shown in my original post and I get the same error message except for the path.

Thanks for the other suggestions too I'll see if I can wait before the doc is closed.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 12, 2017 0
Most Valuable Participant ,
Jul 12, 2017

Copy link to clipboard

Copied

Folder level scripts are for defining trusted functions, but not generally for calling them. You'd use external automation to run the function. Be sure, by the way, not to use Acrobat as part of an automatic solution handling work from multiple users/machines; if that's the aim look at LiveCycle PDF Generator (despite the name). Acrobat is an interactive tool for a single (human) user.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 12, 2017 0