• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Trying to use app.execMenuItem("Save"); to save a file and having problems

New Here ,
Nov 11, 2022 Nov 11, 2022

Copy link to clipboard

Copied

I am trying to write a script that will create a tool button to save the current file and then flatten it. This is a workaround to a bug in my PDF-creation routine in my CAD software. So here is the script so far:

 

app.addToolButton({cName: "MyFlattenButton",
cLabel: "Flatten",
cTooltext: "Flatten all pages",
cEnable: "event.rc = (app.doc != null);",
cExec: "saveandflatten();"
});

 

function saveandflatten() {
// app.beginPriv();
app.execMenuItem("Save");
// app.endPriv();
this.flattenPages();
return 0;
}

 

I tried it first without the beginPriv and endPriv calls, then tried with those un-commented. Does not work. I imagine this is something simple. Basically I'm just trying to save the current file.

 

Thanks!

TOPICS
Acrobat SDK and JavaScript , Windows

Views

158

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
community guidelines
Community Expert ,
Nov 11, 2022 Nov 11, 2022

Copy link to clipboard

Copied

As I think you realize, these are priveleged operation. However, your code is missing the "trustedFunction" definition. You can find the details you need about trust and privelege in these articles:

https://www.pdfscripting.com/public/Trust-and-Privilege-in-Acrobat-Scripts.cfm

https://www.pdfscripting.com/public/Using-Trusted-Functions.cfm

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
community guidelines
New Here ,
Nov 12, 2022 Nov 12, 2022

Copy link to clipboard

Copied

Thank you for that guidance. Copying and pasting from the page you linked to, I changed my code to this:

 

app.addToolButton({cName: "MyFlattenButton",
cLabel: "Flatten",
cTooltext: "Flatten all pages",
cEnable: "event.rc = (app.doc != null);",
cExec: "saveandflatten();"
});

 

var Trusted_MyDocSave = app.trustedFunction(function(oDoc)
{
app.beginPriv();
oDoc.saveAs(oDoc.path);
app.endPriv();
});

function saveandflatten() {
Trusted_MyDocSave(this);
this.flattenPages();
return 0;
}

 

This seems to operate correctly now. I also added the Acrobat Javascripts folder as a trusted location in the Enhanced Security preferences window (was that necessary?). 

 

But now a new question -- sort of a long explanation but wondering why this occurs:

 

- My original need to add the save feature to this script is due to some bug in either Acrobat or my CAD software. When I create a PDF directly from my CAD software (Bricscad) it is faster to do it using the Bricscad-supplied create PDF feature rather than printing to Acrobat. But then if I open the file in Acrobat and add a stamp (which I often do) and then flatten the stamp (for security) it messes up all the fonts in the PDF. The workaround is to add the stamp, save the PDF, then flatten. That works fine for some reason.

 

I reported this bug to Adobe and to Bricscad but neither could solve it. So I thought why not just incorporate the save into my flatten routine. But when I do that and run the code above (which appears to operate correctly) the fonts get messed up as if the save did not happen. I tried commenting out the flatten code in the script to be sure the save was happening and it appears that it was being saved. 

 

Is something different happening in the script than when I do this manually? I tried manually using Save As before flattening and that works fine (no messed up fonts). 

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
community guidelines
New Here ,
Nov 13, 2022 Nov 13, 2022

Copy link to clipboard

Copied

Fixed it -- for some reason, changing from the oDoc.saveAs(oDoc.path) function to app.execMenuItem("Save") makes it work. So for reference, here is the working routine. This saves the file before flattening and then again after flattening, just for convenience.

 

app.addToolButton({cName: "MyFlattenButton",
cLabel: "Flatten",
cTooltext: "Flatten all pages",
cEnable: "event.rc = (app.doc != null);",
cExec: "saveandflatten();"
});

 

var Trusted_MyDocSave = app.trustedFunction(function()
{
app.beginPriv();
app.execMenuItem("Save");
app.endPriv();
});

 

function saveandflatten() {
Trusted_MyDocSave();
this.flattenPages();
Trusted_MyDocSave();
return 0;
}

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
community guidelines
Community Expert ,
Nov 14, 2022 Nov 14, 2022

Copy link to clipboard

Copied

In Acrobat there is a significant difference between Save and SaveAs, that has to do with how PDFs are organized at the binary level. I believe this difference is the root of all your issues.  And it might still bite you in the future. 

 

A PDF is essentially an organized set of object definitions, that are accessed through a reference table. When a PDF is just "saved", the changed objects and a new reference table are appended to the end of the file.  The original file is left intact.  But a "SaveAs" causes Acrobat to recreate the PDF by merging all change sections into the original to create a completely new PDF, one that is organized they way Acrobat wants it. This merging seems to be causing the problem. Acrobat is trying to re-organize the PDF in a way that is probably different from how the CAD tool organized it in the first place.  I don't know, but I  suspect that Acrobat is unhappy with the fonts, so it replaces them.  Maybe the cad software is creating multiple subsets of fonts not available to Acrobat.  If this is the case, Acrobat wouldn't know how to merge the subsets. 

 

This is just idle speculation.  A thorough examination of the document would be necessary to get a real handle on the issue. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
community guidelines
New Here ,
Nov 15, 2022 Nov 15, 2022

Copy link to clipboard

Copied

LATEST

Thank you for the insight on that. It is an odd problem. Because when I'm doing it manually, I can either Save or Save As after applying the stamp and then flatten works fine. But when doing it with the script, the Save As does not solve the problem with flatten but the Save does. 

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
community guidelines