28 Replies Latest reply on Jun 24, 2010 12:14 PM by gkaiseril

    saveas ... yet again

    TCGSUPP Level 1

      using Acrobat Pro v9, visual studio 2003 VB.Net

       

      I am struggling to find a working solution to use the saveas method to save a file in a different format.

      In this case, I am trying to export PDF to TIF programatically, but might want to adapt to other export type.

      I have read the portion in the SDK with v7 and up that .saveas is a privileged feature and must be called through trustedFunction().

       

      I've read numerous posts here and have adapted and changed code several times, but in all versions of re-writing the javascript portion I continue to get the "NotAllowedError: Security settings prevent access to this property or method. "

      I cannot figure out what I am doing wrong to get this to work right.

       

      We have VL support and I called Adobe support, but they would not help as this was a scripting issue and said to post here.

       

      Any help here?

       

      Thanks in advance

       

       

      Code as I have it today.

      ----------------

      Folder Level script

      in c:\program files\adobe\acrobat 9.0\acrobat\javascripts\mysaveas.js

       

      mySaveAs = app.trustPropagatorFunction(function(path, type)
      {
      app.beginPriv();
      doc.saveAs(path, type); // <== This is the line that throws the error
      app.endPriv();
      });

      mySaveDoc = app.trustedFunction( function (path, type)
      {
      // privileged and/or non-privileged code here
      app.beginPriv();
      mySaveAs(doc, path, type);
      app.endPriv();
      // privileged and/or non-privileged code here
      });


      exportpdf2tif = function(path,type)
      {
      mySaveDoc(this,path,type)
      };

       

       

      VB.Net Code

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

      Private Sub savePDFtoTIF(ByVal fullPathPDF As String, ByVal fullPathTIF As String)
              Dim PDFApp As Acrobat.AcroApp
              Dim PDDoc As Acrobat.CAcroPDDoc
              Dim AVDoc As Acrobat.CAcroAVDoc
              Dim JSObj As Object

              ' Create Acrobat Application object
              PDFApp = CreateObject("AcroExch.App")

              ' Create Acrobat Document object
              PDDoc = CreateObject("AcroExch.PDDoc")

              ' Open PDF file
              PDDoc.Open(fullPathPDF)

              ' Create AV doc from PDDoc object
              AVDoc = PDDoc.OpenAVDoc("TempPDF")

              ' Hide Acrobat application so everything is done in silent mode
              PDFApp.Hide()

              ' Create Javascript bridge object
              JSObj = PDDoc.GetJSObject()

              ' Attempt to save PDF to TIF image file.
              ' SaveAs method syntax .SaveAs( strFilePath, cConvID )
              ' For TIFF output the correct cConvid is
              ' cCovid MUST BE ALL LOWERCASE.
              JSObj.mySaveDoc(fullPathTIF, "com.adobe.acrobat.tiff") '<=== this is the line that throws the error


              PDDoc.Close()
              PDFApp.CloseAllDocs()

              ' Clean up
              System.Runtime.InteropServices.Marshal.ReleaseComObject(JSObj)
              JSObj = Nothing

              System.Runtime.InteropServices.Marshal.ReleaseComObject(PDFApp)
              PDFApp = Nothing

              System.Runtime.InteropServices.Marshal.ReleaseComObject(PDDoc)
              PDDoc = Nothing

              System.Runtime.InteropServices.Marshal.ReleaseComObject(AVDoc)
              AVDoc = Nothing

          End Sub

       

       

       

        • 1. Re: saveas ... yet again
          try67 MVP & Adobe Community Professional

          Not sure if it will solve it, but you should pass doc as a parameter to mySaveAs as well as path and type.

          • 2. Re: saveas ... yet again
            TCGSUPP Level 1

            thanks.

            tried doing that already and could never get the syntax correct. everytime i passed in 'doc', i got a "type mismatch" error.

            I logically thought passing in PDDoc would do it. Nope.

             

            which object is the correct to pass into the javascript function?

            • 3. Re: saveas ... yet again
              TCGSUPP Level 1

              if i pass in PDDoc i get "type mismatch"

              if i pass in PDFApp.getactivedoc i get "type mismatch"

              if i pass in the path of file, i get "doc.saveAs is not a function"

               

              this is what the code looks like with 3 parameters as you suggested.

               

              VB.NET

               

                  Private Sub savePDFtoTIF(ByVal fullPathPDF As String, ByVal fullPathTIF As String)
                      Dim PDFApp As Acrobat.AcroApp
                      Dim PDDoc As Acrobat.CAcroPDDoc
                      Dim AVDoc As Acrobat.CAcroAVDoc
                      Dim JSObj As Object
                      PDFApp = CreateObject("AcroExch.App")
                      PDDoc = CreateObject("AcroExch.PDDoc")
                      PDDoc.Open(fullPathPDF)
                      AVDoc = PDDoc.OpenAVDoc("TempPDF")
                      PDFApp.Hide()
                      JSObj = PDDoc.GetJSObject()
                      JSObj.mySaveDoc(fullPathPDF, fullPathTIF, "com.adobe.acrobat.tiff")
                      PDDoc.Close()
                      PDFApp.CloseAllDocs()
                      System.Runtime.InteropServices.Marshal.ReleaseComObject(JSObj)
                      JSObj = Nothing
                      System.Runtime.InteropServices.Marshal.ReleaseComObject(PDFApp)
                      PDFApp = Nothing
                      System.Runtime.InteropServices.Marshal.ReleaseComObject(PDDoc)
                      PDDoc = Nothing
                      System.Runtime.InteropServices.Marshal.ReleaseComObject(AVDoc)
                      AVDoc = Nothing
                  End Sub

               

              Javascript

               

              mySaveAs = app.trustPropagatorFunction(function(doc, path, type)
              {
              app.beginPriv();
              doc.saveAs(path, type);
              app.endPriv();
              });

              mySaveDoc = app.trustedFunction( function (doc, path, type)
              {
              // privileged and/or non-privileged code here
              app.beginPriv();
              mySaveAs(doc, path, type);
              app.endPriv();
              // privileged and/or non-privileged code here
              });

              • 4. Re: saveas ... yet again
                Patrick Leckey Level 3

                You can't pass the VB.NET PDDoc class to JavaScript as a Doc object.  The JavaScript Doc object is NOT a PDDoc, it is a higher-level abstraction than PDDoc.  Pass your VB.NET JSObject variable, that is what Acrobat JavaScript will treat as your JS Doc object.

                • 5. Re: saveas ... yet again
                  lqn Level 1

                  Sorry to reopen this issue, but I've tried to follow the last post advice by writing:

                   

                          JSObj = PDDoc.GetJSObject()
                          JSObj.mySaveDoc(JSObj, fullPathTIF, "com.adobe.acrobat.tiff")

                   

                  and the code still raises the "NotAllowError".

                   

                  Any help would be appreciated.

                  • 6. Re: saveas ... yet again
                    lqn Level 1

                    Maybe it is relevant that Acrobat Pro indicates the document has no protection, and that everything in the restriction list appears as authorized.

                     

                    I have also read the chapter 13 "Rights-Enabled PDF Files" in the Acrobat JavaScript Scripting Guide, which has not helped me. It seems it deals essentially with forms, but my document has no form.

                     

                    I've also read what seemed relevant in LifeCycle extensions, but did not find a solution to my problem. That sounds very complex for a task that I would have hoped simple: doc.saveAs("targetName.xml","com.acrobat.adobe.xml-1-00");

                     

                    Any idea ?

                    • 7. Re: saveas ... yet again
                      Bernd Alheit Adobe Community Professional & MVP

                      Read about safe path in the Javascript API Reference.

                      • 8. Re: saveas ... yet again
                        lqn Level 1

                        Thanks for your help.

                         

                        I've tried to save to "C:\Documents and Settings\<myusername>\Mes documents\out.xml". I guess it is a safe path, although "However, this is not the only requirement for a path to be safe; a path is also subject to certain, unspecified tests as well." is not clear.

                         

                        It still fails with the same NotAllowedError about security settings preventing access to a property or method.

                        • 9. Re: saveas ... yet again
                          Vaibhav Padlikar Level 2

                          this.saveAs("/c/Documents and Settings/temp/targetName.xml","com.adobe.acrobat.xml-1-00");   works for me from the console. (Do ctrl+J to get the console and try this)

                           

                           

                           

                          The error you are getting, suggests the cPath is not getting passed correctly. Can you please tell us the value that you are passing to fullPathTIF variable.

                           

                           

                           

                          -vaibhav

                          • 10. Re: saveas ... yet again
                            lqn Level 1

                            Thanks. I have just tried this with Acrobat Pro 7. I have opened the console and simply typed "this.saveAs("/c/Documents and  Settings//temp/targetName.xml","com.adobe.acrobat.xml-1-00"); ". A document (SysML 1.1 pdf specification)  was opened in Acrobat.

                             

                            It fails differently: a dialog says that opening the file has failed. (The message is in French ).

                            • 11. Re: saveas ... yet again
                              lqn Level 1

                              Here is the code hopefully with the cPath answer. I am not sure why "TempPDF"  is used.

                               

                              Option Strict Off
                              Option Explicit On
                              Module EntryPoint
                                 
                                  ' ADOBE SYSTEMS INCORPORATED
                                  ' Copyright (C) 1994-2003 Adobe Systems Incorporated
                                  ' All rights reserved.
                                  '
                                  ' NOTICE: Adobe permits you to use, modify, and distribute this file
                                  ' in accordance with the terms of the Adobe license agreement
                                  ' accompanying it. If you have received this file from a source other
                                  ' than Adobe, then your use, modification, or distribution of it
                                  ' requires the prior written permission of Adobe.
                                  '
                                  ' -------------------------------------------------------------------
                                  'BasicIacVBJso
                                  '- This simple VB sample demonstrates how to use Acrobat IAC JavaScript Object
                                  'in VB applications. It includes the minimum code to create IAC object CAcroPDDoc
                                  'and AFormApp, and use their properties and methods. The program simply opens a
                                  'PDF file ( IAC\TestFiles\TestForm.pdf ), and get some information about the document
                                  '( number of pages, number of words, and number of form fields ) as well as Acrobat Viewer.
                                  '----------------------------------------------------------------------
                                  'The purpose of the sample is to provide simple code for VB developers
                                  'to get started with Acrobat JavaScript Programming in VB. Based on this project, you
                                  'should be easily add more code to improve and extend the capability, or to use
                                  'other properties and methods of the Acrobat IAC objects.
                                  '----------------------------------------------------------------------
                                  'The way to set up using Acrobat IAC in the project is from the menu
                                  ' Project -> Preferences ... to select Acrobat and AcroFormAut type libraries.
                                  '------------------------------------------------------------------------
                                 
                                  ' global variables
                                  Public jsObj As Object

                               


                                  Public Sub Main()

                               

                                      Dim SampleFilePath As String = "c:\doc.pdf" 'System.Windows.Forms.Application.StartupPath & "\..\..\..\..\TestFiles\TestForm.pdf"
                                      Dim targetPath As String = "C:\Documents and Settings\<myusername>Mes documents\out.xml"
                                      MsgBox("Saving ...")
                                      savePDFtoTIF(SampleFilePath, targetPath)
                                      MsgBox("Saved.")

                               

                                  End Sub

                               

                                      Private Sub savePDFtoTIF(ByVal fullPathPDF As String, ByVal fullPathTIF As String)
                                      Dim PDFApp As Acrobat.AcroApp
                                      Dim PDDoc As Acrobat.CAcroPDDoc
                                      Dim AVDoc As Acrobat.CAcroAVDoc
                                      Dim JSObj As Object
                                      PDFApp = CreateObject("AcroExch.App")
                                      PDDoc = CreateObject("AcroExch.PDDoc")
                                      PDDoc.Open(fullPathPDF)
                                      AVDoc = PDDoc.OpenAVDoc("TempPDF")
                                      'PDFApp.Hide()
                                      JSObj = PDDoc.GetJSObject()
                                      JSObj.mySaveDoc(JSObj, fullPathTIF, "com.adobe.acrobat.tiff")
                                      PDDoc.Close()
                                      PDFApp.CloseAllDocs()
                                      System.Runtime.InteropServices.Marshal.ReleaseComObject(JSObj)
                                      JSObj = Nothing
                                      System.Runtime.InteropServices.Marshal.ReleaseComObject(PDFApp)
                                      PDFApp = Nothing
                                      System.Runtime.InteropServices.Marshal.ReleaseComObject(PDDoc)
                                      PDDoc = Nothing
                                      System.Runtime.InteropServices.Marshal.ReleaseComObject(AVDoc)
                                      AVDoc = Nothing
                                  End Sub
                              End Module

                              • 12. Re: saveas ... yet again
                                Vaibhav Padlikar Level 2

                                >> Dim targetPath As String = "C:\Documents and Settings\/Mes documents/out.xml"

                                 

                                Try Changing this to;

                                 

                                Dim targetPath As String = “/c/Documents and  Settings/<myusername>/Mes documents/out.xml”

                                 

                                -vaibhav

                                 

                                 

                                 

                                -vaibhav

                                • 13. Re: saveas ... yet again
                                  lqn Level 1

                                  NotAllowedError again ...Same detail about security settings preventing access to property or method.

                                  • 14. Re: saveas ... yet again
                                    Vaibhav Padlikar Level 2

                                    On console can you try out ,

                                     


                                    this.saveAs("/c/Documents and Settings/<myusername>/Mes documents/out.xml","com.adobe.acrobat.xml-1-00");

                                     

                                     


                                    -vaibhav

                                    • 15. Re: saveas ... yet again
                                      lqn Level 1

                                      Thanks, it has worked !

                                       

                                      this.saveAs("/c/Documents and Settings/<myusername>/Mes documents/out.xml","com.adobe.acrobat.xml-1-00");

                                      in the console.

                                       

                                      Why is still unclear, but I guess that I'll end up understanding it.

                                       

                                      Thanks very much for your help. I even doubted it was possible without complex lifecycle extensions or something like that.

                                      • 16. Re: saveas ... yet again
                                        lqn Level 1

                                        The trouble now is that I want to invoke this an external application, so I understand it that I may use VB, but the code provided in the previous comment with Dim targetPath As String = "c:\Documents and Settings\<username>/Mes documents/out.xml" does not work.

                                         

                                        Another question for me is why saveAs works from the console outside beginPriv/endPriv ?

                                        • 17. Re: saveas ... yet again
                                          Vaibhav Padlikar Level 2

                                          Its actually not that complex, the save rights are required if you wish to allow uers to save that pdf even with Adobe Reader.  In Acrobat, by default any pdf can be saved, and hence you don’t need to give save rights from lifecycle server. Morever, the saving rights can be given from Acrobat itself, by going to Advanced -> Extend Features in Adobe Reader...

                                           

                                          Anyhow , this information is only in case you want to use the code from adobe reader.

                                           

                                          -vaibhav

                                          • 18. Re: saveas ... yet again
                                            Vaibhav Padlikar Level 2

                                            >> Dim targetPath As String = "c:\Documents and Settings\<username>/Mes documents/out.xml"

                                             

                                            change it to

                                             

                                            Dim targetPath As String = "/c/Documents and Settings/<username>/Mes documents/out.xml"

                                             

                                            Some typo perhaps in my previous post on this. Ensure to change <username> else you would get the error again.

                                             

                                             

                                            >> why saveAs works from the console outside beginPriv/endPriv ?

                                             

                                            JS API reference says console is a privileged context.

                                             

                                            -vaibhav

                                            • 19. Re: saveas ... yet again
                                              lqn Level 1

                                              "c/Documents and Settings/XX/Mes documents/out.xml" where XX is replaced by my username.

                                               

                                              This still raises NotAllowedError.

                                               

                                              "JS API reference says console is a privileged context."

                                              -> I hadn't noticed, thanks for the tip.

                                              • 20. Re: saveas ... yet again
                                                lqn Level 1

                                                "Anyhow , this information is only in case you want to use the code from  adobe reader."

                                                 

                                                This is what I intend to do. I have both Acrobat Pro 7 and Reader 9 with the following script in their (app) Javascript folder:

                                                 

                                                mySaveAs = app.trustPropagatorFunction(function(doc, path, type)
                                                {
                                                app.beginPriv();
                                                doc.saveAs(path, type);
                                                app.endPriv();
                                                });

                                                 

                                                mySaveDoc = app.trustedFunction( function (doc, path, type)
                                                {
                                                // privileged and/or non-privileged code here
                                                app.beginPriv();
                                                mySaveAs(doc, path, type);
                                                app.endPriv();
                                                // privileged and/or non-privileged code here
                                                });

                                                • 21. Re: saveas ... yet again
                                                  try67 MVP & Adobe Community Professional

                                                  You should pay more attention to the correct path syntax. It's not:

                                                  c/Documents and Settings/XX/Mes documents/out.xml

                                                  But:

                                                  /c/Documents and Settings/XX/Mes documents/out.xml

                                                  (notice the first forward slash)

                                                  • 22. Re: saveas ... yet again
                                                    lqn Level 1

                                                    Thanks for the impressive collective effort ...

                                                     

                                                    The typo was in my last post. The line I try is

                                                    Dim targetPath As String = "/c/Documents and Settings/XXX/Mes documents/out.xml",


                                                    but the outcome is still NotAllowedError.

                                                    • 23. Re: saveas ... yet again
                                                      Vaibhav Padlikar Level 2

                                                      Are you trying it from Adobe Rader or from Acrobat . If you are trying on Adobe Reader , as I had mentioned in my previous post , you will need extended rights for the pdf; those rights you can even give form within Acrobat for the pdf on which you are trying this.

                                                       

                                                      If you are trying on Acrobat, I can't think of other reason , for it to work from console and not from code in Acrobat.  (In case there are no typos in the code)  Perhaps others might help.

                                                       

                                                      -vaibhav

                                                      • 24. Re: saveas ... yet again
                                                        lqn Level 1

                                                        OK, everything works now with Acrobat 7 Pro, so thank you all for your help. The problem was with tiff format, not with xml and I remember I read something in the docs about matching the file content to its extension.

                                                         

                                                        What is responsible for choosing Acrobat 7 Pro instead of Reader 9 ? I 'd like to target Reader 9 as the execution platform.

                                                        • 25. Re: saveas ... yet again
                                                          Vaibhav Padlikar Level 2

                                                          OK great. 

                                                           

                                                          >> What is responsible for choosing Acrobat 7 Pro instead of Reader 9 ? I 'd like to target Reader 9 as the execution platform.

                                                           

                                                          Not all documents  have save rights by default for reader.  If  the documents have save usage rights , you can use this even with reader. So from scripting perspective nothing more needs to be done. 

                                                           

                                                          If you need more help on this and since it's not a scripting related issue , feel free to email your queries to  "vaibhavp at adobe dot com".

                                                           

                                                          -vaibhav

                                                          • 26. Re: saveas ... yet again
                                                            gkaiseril MVP & Adobe Community Professional

                                                            Adobe constantly updates certain actions because of security concerns. The 'saveAs' method has been restricted to prevent the form from being saved without knowledge of the user by a 3rd party. And to allow for a silent save, the user needs to install a special file in an application level folder.

                                                             

                                                            Reader is just that a "READER". So anything beyond reading or printing requires special rights that are applied by Acrobat Pro for up to 500 distributed copies of a form or LiveCycle ES server for forms being distributed to more than 500 users.

                                                             

                                                            More information is contained in the Acrobat JS API Reference and in Adobe's Acrobat Developers' site.

                                                            • 27. Re: saveas ... yet again
                                                              lqn Level 1

                                                              On "Reader is just that a "READER".", from previous message.

                                                               

                                                              I understand that there are security issues at stake. I also appreciate the huge efforts to provide documentation to the user (although hundreds of pages are sometimes hard to assimilate in a few days). I also and finally thank the persons who spend time to answer in this forum when answers are also written somewhere in the docs.

                                                               

                                                              But I think the SDK should give the user total control over HIS/HER documents. Converting to another format in Reader, to my eyes, should be possible if the user accepts to install a 'plugin/javascript/VB" app maybe digitally signed.

                                                               

                                                              In the case that interests me, I want to write a program that extracts the structure (bookmarks) and other information based on regexps from any document the end user wishes to analyse. The end user is generally not the author of the document (ex. a standard). There are no forms in the document. When I read the numerous Adobe docs, I can see that security is mostly (?) thought of in the context of forms/workflow. I also find it sometimes difficult to see what applies to Acrobat only, and why (for example, in SDK samples guide).

                                                               

                                                              "And to allow for a silent save, the user needs to install a special file  in an application level folder": could you please give me a pointer on this ? I have looked at many documents, but I haven't noticed a remark on this ?

                                                               

                                                              Thanks in advance.

                                                               

                                                              Loïc

                                                              • 28. Re: saveas ... yet again
                                                                gkaiseril MVP & Adobe Community Professional

                                                                And to allow for a silent save, the user needs to install a special file  in an application level folder": could you please give me a pointer on this ? I have looked at many documents, but I haven't noticed a remark on this ?

                                                                There is a lot of information in the Acrobat JS API Reference.How to save a PDF with Acrobat JavaScript by Thom Parker.

                                                                 

                                                                 

                                                                When I read the numerous Adobe docs, I can see that security is mostly (?) thought of in the context of forms/workflow.

                                                                That is because Identity Theft is easier  trough official looking forms. There are also the same security restrictions on silent printing. Non-form documents are covered by national and international copyright laws and Adobe allows creators to specify security restrictions and use digital rights management to control the documents.

                                                                 

                                                                I also find it sometimes difficult to see what applies to Acrobat only, and why (for example, in SDK samples guide).

                                                                Maybe Adobe has given out the free Reader in hopes that the user of the free program will purchase the full product. I would expect that Adobe does not write software for free and I  would expect that you do not work for free.