6 Replies Latest reply on Dec 30, 2010 5:56 AM by Christian Kirchhoff

    CS3 and Delphi: Place pdf into indesign

    Christian Kirchhoff Level 1

      Hello,

       

      I am developing a script with Deplhi  that automates the creation of documents.

       

      I would like to be able to place pdf documents in an InDesign document. I adapted a script that I found in this thread: http://forums.adobe.com/message/3013174

       

      I only had to define the proper types for the variables, cause Delphi doesn't allow untyped variables. There was one line in particular that I had to modify thoroughly:

      myPDFPage = myPage.place(File(myPDFFile), [0,0])[0];

      The PlacePoint parameter had to be an array of double, and the missing parameters had to be set. Eventually, the following code worked:

      var
        APoint:      array of double;

      begin
        ...
        SetLength(APoint, 2);
        APoint[0] := 0;
        APoint[1] := 0;
        myPage.place(Filename, APoint, ActiveDocument.Layers[1], false, false);
        ...

      In the original script, the result of the place function is assigned to the variable myPDFPage, and I am having troubles to get this assigment working. Often the TypeLib uses OleVariants and you have to figure out by ourself which type(s) are allowed and have to be used, and which type cast(s).

       

      In the original script this code follows:

      myFirstPage = myPDFPage.pdfAttributes.pageNumber;

      The only interfaces in the TypeLib that have a property called pdfAttributes are PDF and ImportedPage. But neither of those types work if I define pdfAttributes to be of that type. I am not sure whether the fact that place seems to return an array adds additional problems. Most of the times, arrays from in TypeLib are based in the index 1, not zero.

       

      But I tried

       

      var
        APoint:      array of double;
        myPDFPage:   PDF;
      begin
        ...
       
      myPDFPage := PDF(myPage.place(Filename, APoint, ActiveDocument.Layers[1], false, false)[0]);
        ...

      and

      var
        APoint:      array of double;
        myPDFPage:   ImportedPage;
      begin
        ...
       
      myPDFPage := ImportedPage(myPage.place(Filename, APoint, ActiveDocument.Layers[1], false, false)[0]);
        ...

      and the same with and array index of 1 instead of 0, and without any index. But none of it would be compiled without an error.

       

      Doies anybody know how that command line has to be or which type myPDFPage has to have?

       

      And more generally: How could I determine the possibla types of a parameter, variable or return type that is an OleVariant in the TypeLib?

       

      I found some InDesign CS3 scripting specific pdfs here: http://www.adobe.com/products/indesign/scripting/, but no complete reference of all functions and types.

       

      Best regards,

       

      Christian Kirchhoff

        • 1. Re: CS3 and Delphi: Place pdf into indesign
          Kasyan Servetsky Level 5

          Hi Christian

           

          I am not sure if anybody else uses Delphi on this forum.

          But here is what I get in JS -- let's run a simple test:

          var doc = app.activeDocument;
          var myPDFFile = new File("~/Desktop/Sample.pdf");
          var page = doc.pages[0];
          app.pdfPlacePreferences.pageNumber = 1;
          var a1 = page.place(myPDFFile, [0,0]);
          var a2 = a1[0];

           

          a1 returns an array of PDF objects, but you declare it as array of double. AFAIK this is always a single element array since you can't place more than one pdf-page at a time.
          a2.constructor.name;
          Result: Array

           

          a2 returns object of class PDF
          a2.constructor.name;
          Result: PDF

           

          Kasyan

          • 2. Re: CS3 and Delphi: Place pdf into indesign
            Christian Kirchhoff Level 1

            Thanks very much, Kasyan, for this analysis.

             

            However, the array of double is not the return result, but the PlacePoint paramater (the [0, 0] in your code).

             

            I already tried to use the type PDF for the return value, and I will try it again.

             

            Best regads,

             

            Christian

            • 3. Re: CS3 and Delphi: Place pdf into indesign
              Kasyan Servetsky Level 5
              However, the array of double is not the return result, but the PlacePoint paramater (the [0, 0] in your code).

              Oops! I didn't notice this -- see Delphi syntax for the first time.

              I'm sure that myPDFPage should definitely be declared as PDF not  ImportedPage.

               

              Kasyan

              1 person found this helpful
              • 4. Re: CS3 and Delphi: Place pdf into indesign
                Christian Kirchhoff Level 1

                No problem, I am gonna re-check PDF as the object type.

                • 5. Re: CS3 and Delphi: Place pdf into indesign
                  Christian Kirchhoff Level 1

                  In order to make the code compilable, I had to do a double typecast. The result of the place function - in the Typelib - is OleVariant, and I couldn't use the snytax place(...)[0]; directly.

                   

                  So I declared a variable myPDFPages as an array of PDF, and assigned the entiry result of the place function to that.

                   

                  The new code is:

                  var
                    APoint:      array of double;
                   
                  myPDFPages:  array of PDF;
                    myPDFPage:   PDF;
                  begin
                    ...
                   
                  myPDFPages := myPage.place(Filename, APoint, ActiveDocument.Layers[1], false, false);
                    myPDFPage := PDF(myPDFPages[0]);
                    ...

                  But: allthough the code compiles, the line myPDFPages :=... throws an error (after the pdf is inserted in InDesign).

                   

                  If I just use execute the place command without assingning the result to anything, it works without an error. But the code I copied from the aforementioned forum thread uses the result, and so want I.

                   

                  I'll do some further testing on this.

                   

                  Best regards

                   

                  Christian

                  • 6. Working with OleVariants in Delphi
                    Christian Kirchhoff Level 1

                    In case anyone runs into a similar problem: The returned OleVariant is a variable which is a dispatchable interface. Those variables cannot be casted into some type, and during compiling the use of any alleged method or property of (what is believed to be) the correct interface is not checked.

                     

                    The following code is working:

                    var
                      APoint:      array of double;
                     
                    myPDFPage:   Variant; //interface PDF
                      myPDFPages:  OleVariant; //interface PDFs

                    begin
                      ...
                     
                    myPDFPages := myPage.place(Filename, APoint, ActiveDocument.Layers[1], false, false);
                      myPDFPage := myPDFPages.Item[1];

                      ...
                      myFirstPage := myPDFPage.pdfAttributes.pageNumber
                      ...

                     

                    So though myPDFPages is just an OleVariant, I can use the Item property if the interface PDFs in order to get an item. The variable that receives that item (myPDFPage ) has to be of type Variant. It realizes the PDF interface, and thus I can work with that interface's properties and methods, e.g. pdfAttributes.

                     

                    Best regards,

                     

                    Christian