Skip navigation
Droptix
Currently Being Moderated

PDDocColorConvertPage - Convert color pages to gray

Jun 24, 2012 11:49 PM

Tags: #com #python #pddoccolorconvertpage #pdcolorconvertparams

I want to convert a specified color page to gray programmatically using `PDDocColorConvertPage` with Dot Gain 15% as I would do it in Acrobat 9 Pro manually. In the API reference the syntax is explained but I don't understand it, especially `PDColorConvertParams`.

 

Is there anyone who can post a short example of code?

 

I'm trying to achieve this via COM-programming and Python. An example in VB, JavaScript or similar is also great.

 

Thanks in advance!

 
Replies
  • Currently Being Moderated
    Jun 25, 2012 8:14 AM   in reply to Droptix

    PDDocColorConvertPage and PDColorConvertParams are C/C++ methods and data structures – you can get to them from COM or Python.

     

    In order to convert the page from COM/Python, you will need to use the JavaScript method for color conversion.  Details in the docs.

     

    FYI: Acrobat can't be used on a server….

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 26, 2012 8:29 AM   in reply to Droptix

    The Acrobat SDK consists of different parts:

    - plug-in and PDF library API - these are functions you can call from

    C/C++, but only within a plug-in or an application that uses the PDFL. The

    API function yo are trying to use is part of this interface and not

    available via COM.

    - Inter Application Commuication (IAC) interface - these are e.g. COM

    methods that you can call from your Python program

    - JavaScript - an API to automate Acrobat with JavaScript programs that are

    either embedded in PDF files, or stored on your computer.

     

    In general, you cannot cross API boundaries, but there is a way to execute

    JavaScript in an application that uses the COM interface. Take a look at

    the documentation that is part of the SDK, this is all documented, and

    there are sample programs that come with the SDK. You will however not find

    any Python programs in the SDK - you need to map the use of the COM

    interface from VB to Python.

     

    Here is a link to a blog post I wrote a few years ago about how to use

    JavaScript from within a VB program:

    http://www.khk.net/wordpress/2009/03/11/acrobat-javascript-and-vb-walk -into-a-bar/

     

     

    Karl Heinz Kremer

    PDF Acrobatics Without a Net

     

    khk@khk.net

    http://www.khkonsulting.com

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 29, 2012 11:46 AM   in reply to Droptix

    I don't have a Windows version of Acrobat 9 handy, I was only able to test

    with Acrobat X, and the menu item does show up (after changing "Document"

    to "File" because AX does not have a Document menu anymore).

     

    The "Document" name is a language independent name, so  you have to use

    that even with the German version of Acrobat. Are you sure that your path

    is correct? You can easily test that by running a JavaScript command (take

    a look at this blog post:

    http://khkonsulting.com/2010/12/acrobat-javascripts-where-do-they-go/ )

     

    Acrobat will automatically load anything that has a .js extension if it's

    stored in either the user or the application level JavaScript directory.

     

    The different APIs are not compatible with each other - you can only call

    JavaScript methods from within JavaScript (with the exception of using the

    JS bridge), you can call COM methods only when using COM, and you can call

    the Acrobat/PDFL functions only when writing an Acrobat plug-in or a PDFL

    based program.  You could write your own COM server in form of a plug-in,

    and make other functions available via a COM interface, but the standard

    IAC API does not provide anything besides what is documented in the SDK IAC

    documentation.

     

    If you have a license for the PDFL, then you can certainly call this

    function (and any of the other functions that are part of the Acrobat/PDFL

    API) from your own application.

     

    Karl Heinz Kremer

    PDF Acrobatics Without a Net

     

    khk@khk.net

    http://www.khkonsulting.com

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 1, 2012 10:05 AM   in reply to Droptix

    You are, STILL, trying to use the C++ APIs from JavaScript.

     

    You want the JavaScript API at http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/wwhelp/w whimpl/js/html/wwhelp.htm?href=JS_API_AcroJS.88.1.html#1515776&accessi ble=true

     

    Look at the doc.colorConvertPage() API.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 2, 2012 6:39 AM   in reply to lrosenth

    Just because you find a function somewhere in the Acrobat SDK documentation

    does not mean you can use it in your program: As I said before, you cannot

    cross API boundaries, if you have a function that is described in the

    plug-in API, you can only use it in an Acrobat plug-in (or in most cased,

    in an application that uses the PDFL). You need to stick to the API that

    your application can use (e.g. as lrosenth pointed out, a function from the

    JavaScript API when you try to either write a JavaScript program, or if you

    want to use the JS bridge).

     

    This is not a question of the level of experience somebody has, it's just

    not possible to do that.

     

    Karl Heinz Kremer

    PDF Acrobatics Without a Net

     

    khk@khk.net

    http://www.khkonsulting.com

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 3, 2012 8:57 PM   in reply to Droptix

    I have a situation slightly different but similar enough in nature not to require a new thread.  While I'm using Acrobat X on Win7, I think most everything I'm doing remains consistent in Acrobat 9, and it may help shed some common light.

     

    In my case, I am trying to call  "colorConvertPage" (the Javascript API version Iorsenth recommends, above, and not the "PDDocColorConvertPage" exposed by the C++ API) from a VBscript via  the JSObject.  My problem is that I cannot figure out how to correctly configure and pass the two arrays of "colorConvertAction" objects that the JavaScript method requires as its second and third parameters.

     

    The purpose of the script is to merge large numbers of TIFFs into multiple PDFs, combining them based on a filename schema.  After each document is created, I want the script to convert all of the page images to sRGB before saving.

     

    I have been able to implement Mr. Kremer's method of creating a native Acrobat folder-level JavaScript to perform the color conversion and then calling that from the VBscript.  The Javascript is taken almost verbatim from the example given under the "getColorConvertAction" method in the Acrobat X JavaScript API Reference, only changing the profile to sRGB and then adding the loop through all the pages and a slight modification of Karl's code to add the function as a menu item under Edit:

     

    function ConvertAllTosRGB()

    {

        // Get a color convert action

        var toRGB = this.getColorConvertAction();

        // Set up the action for a conversion to RGB

        toRGB.matchAttributesAny = -1;

        toRGB.matchSpaceTypeAny = ~toRGB.constants.spaceFlags.AlternateSpace;

        toRGB.matchIntent = toRGB.constants.renderingIntents.Any;

        toRGB.convertProfile = "sRGB IEC61966-2.1";

        toRGB.convertIntent = toRGB.constants.renderingIntents.Document;

        toRGB.embed = true;

        toRGB.preserveBlack = false;

        toRGB.useBlackPointCompensation = true;

        toRGB.action = toRGB.constants.actions.Convert;

        // Convert the first each page of the document

     

        for(var i = 0; i < this.numPages; i++) {

            var result = this.colorConvertPage(i,[toRGB],[]);

        }

    }

     

    // add the menu item

    app.addMenuItem({

         cName: "ConvertAllTosRGB",

         cUser: "Convert Doc To sRGB",

         cParent: "Edit",

         cExec: "ConvertAllTosRGB();",

         cEnable: "event.rc = (event.target != null);"

    });

     

    Once that .js is dropped into one of Acrobat's script folders, the function can be successfully called from vbs with "jso..ConvertAllTosRGB" (where "jso" is a JSObject successfully created by a call to a PDDoc's .GetJSObject method.)

     

    Where I'm running into trouble is in trying to replicate that JavaScript function within the VBscript itself (which would be more convenient than having to distribute two separate scripts.)  I can successfully create a colorConvertAction object, however I can't seem to successfully cast that object as one in an array to pass to colorConvertPage (nor create the empty array to pass as the third parameter.)  The pertinent portions of the scipt are something like...

     

    Dim AcroApp

    Dim AVDoc

    Dim PDDoc

    Dim jso

    Dim toRGB

    Dim i

    Dim result

     

    Set AcroApp = CreateObject("AcroExch.App")

    Set AVDoc = CreateObject("AcroExch.AVDoc")

     

    If AVDoc.Open("c:\path\some.tif"), "") Then

         Set PDDoc = AVDoc.GetPDDoc

         Set jso = PDDoc.GetJSObject

         Set toRGB = jso.getColorConvertAction

     

         With toRGB

              .matchAttributesAny = -1

              .matchSpaceTypeAny = Not .constants.spaceFlags.AlternateSpace

              .matchIntent = .constants.renderingIntents.Any

              .convertProfile = "sRGB IEC61966-2.1"

              .convertIntent = .constants.renderingIntents.Document

              .embed = True

              .preserveBlack = False

              .useBlackPointCompensation = True

              .action = .constants.actions.Convert

         End With

     

         For i = 0 to PDDoc.GetNumPages - 1

              result = jso.colorConvertPage(i, ????, ????)

              MsgBox(result)

         Next

     

    End If

     

    Everything up to the call to jso.colorConvertPage works fine.  However, as stated, no method that I've tried to pass the toRGB object as an array member (as called for by the second "actions" parameter) has worked.  These include declaring the variable as an array and using the Array function.  Nor have I been able to pass an empty array for the third "inkActions" parameter without throwing an error.

     

    Interestingly, passing the toRGB object itself, directly, as both parameters does not raise any errors.  However, the function then returns a null value and no conversion actually takes place.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 4, 2012 6:26 PM   in reply to Droptix

    Hi Droptix,

     

    I'm glad you got it working.  What method did you wind up using?

     

    Yes, the JavaScript function could certainly be made much more versatile by rewriting it to accept the conversion profile and other of the colorConvertAction's properties as arguments.  I was mainly just doing a quick retool of the SDK documentation example by way of illustration.

     

    The thing I'm trying to ascertain in the VBscript is whether or not it is possible to successfully call colorConvertPage from an external automation script using the JSObject bridge (i.e., accomplish the same thing that the JavaScript example does without having to first install the folder level script.)

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 7, 2012 12:47 PM   in reply to Droptix

    Hi Droptix,

     

    I tested your code, and yes, it did work for me (with the exception noted below.)  I see that my mistake was trying to call colorConvertPage as a method of the JSObject, itself, rather than as a method of a JavaScript Doc opened through the JSObject.

     

    I'm not quite certain what you're trying to do with the goto loop in your batch file.  Do you have multiple .pdf files that need converting, or is it just that single file with multiple pages?

     

    In any event, given the single call to GrayConversion2.vbs in the .bat, the following simple modifications to your .vbs should work (but, again, see the exception, afterward.)  Modifications are in red:

     

    Set argv = Wscript.Arguments

    inFile = argv(0)
    outFile = argv(1)
    arrPages = Split(argv(2), ",")

    Set App = CreateObject("AcroExch.App")
    Set PDDoc = CreateObject("AcroExch.PDDoc")

    ' open the PDF to get access to the JavaScript object
    PDDoc.Open(inFile)

    Set jso = PDDoc.GetJSObject

    ' now open the file again, but using JSObject
    Set doc = jso.app.openDoc(inFile)

    ' do color conversion
    Set target = doc.getColorConvertAction
    target.matchAttributesAny = -1
    target.matchSpaceTypeAny = Not target.constants.spaceFlags.AlternateSpace
    target.matchIntent = target.constants.renderingIntents.Any
    target.convertProfile = "Dot Gain 15%"
    target.convertIntent = target.constants.renderingIntents.Document
    target.embed = True
    target.preserveBlack = False
    target.useBlackPointCompensation = True
    target.action = target.constants.actions.Convert

    ' convert array of Pages (adjusting for 0 first page) to gray

    For i = 0 to Ubound(arrPages)

         doc.colorConvertPage arrPages(i)-1, Array(target), Array()

    Next

    ' save converted document to `outFile`
    doc.saveAs(outFile)

    doc.closeDoc(True)

    ' close document
    PDDoc.Close

    ' exit and close Acrobat
    App.Exit

    ' clean up
    Set PDDoc = Nothing
    Set App = Nothing

     

    That works when run from the .bat as you have it, except:  The call to colorConvertPage fails on the fifth call for me each time.  I'm using a test .pdf that I created, and I've tried varying the order and number of the pages to be converted.  It doesn't appear to be anything specific to the content of the .pdf.  After four successful calls (with the conversion visiblly completed,) the method fails on the fifth call and gives a "Server threw an exception" error.  I must then hard-quit Acrobat from the Task Manager to be able to test the script again. 

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (1)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points