4 Replies Latest reply on Nov 23, 2009 12:57 AM by Melzi94270

    [InDesign C# Script] Problem with textframe.place(imageName)

    Melzi94270

      Hi everyboy !

       


      I am beginning with the InDesign Scripting in C#, and the documentation is very poor. I have a problem and I can't find the solution.

       


      I have to place an image into a textframe on my document's page.

       

      I create my textframe without any problem :

       

                 myDocument.Layers.Add();
              InDesign.Layer lastLayer = (InDesign.Layer) myDocument.Layers[myDocument.Layers.Count];
              InDesign.TextFrame myPicture = myPage.TextFrames.Add(lastLayer, InDesign.idLocationOptions.idUnknown, myPage);

              myPicture.GeometricBounds = new string[4] { myY, myX, myY2, myX2 };

       

      Then I try to place my Image, but I can't find which type of object is returned by .Place méthode :

       

      First try :

      InDesign.Graphics myGraphic =  (InDesign.Graphic) myPicture.Place(pathName, false);

      And I have got this error :

      {System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'InDesign.Graphics'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{C85A4AB3-9492-4C40-8A7B-F8305706C8B8}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
         at Service.createPhotograph(XmlNode photo, Page myPage) in c:\Documents and Settings\melaniep\My Documents\Visual Studio 2005\WebSites\WebSite6\App_Code\Service.cs:line 204}

       

      And I saw this tutorial in javascript for placing images  at http://www.adobe.com/devnet/indesign/pdfs/indesigncs4_scriptingtutorial.pdf


      //Display a standard open file dialog box to select a graphic file.
      var myGraphicFile = File.openDialog("Choose a graphic file");
      //If a graphic file was selected, and if you didn't press Cancel,
      //place the graphic file on the page.
      if((myGraphicFile != "")&&(myGraphicFile != null)){
      var myGraphic = myDocument.pages.item(0).place(myGraphicFile);
      //Since you can place multiple graphics at once, the place method
      //returns an array. To get the graphic you placed, get the first
      //item in the array (JavaScript arrays start with item 0).
      myGraphic = myGraphic[0];

       

       

      So I tried :

       

      InDesign.Graphics myGraphics =  (InDesign.Graphics) myPicture.Place(pathName, false);
                  InDesign.Graphic myGraphic = (InDesign.Graphic) myGraphics[0];

      But I have got the same error.

       

      Someone have an idea? I am really lost, and I don't know what to do :'(

       

       

       

       

      Another little question.

      My picture is smaller than my textFrame.

      And I want my picture fits to the texframe.

      MyGraphic.HorizontalScale = widthOfTextFrame

      MyGraphic.VerticalScale = heightOfTextFrame

      Does it works? Or Am I in the wrong way?

       

      Thank you very much (and sorry for my bad english, I am french )