Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


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

Community Beginner ,
Nov 20, 2009 Nov 20, 2009

Copy link to clipboard

Copied

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 )

TOPICS
Scripting

Views

2.6K

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

correct answers 1 Correct answer

Adobe Employee , Nov 20, 2009 Nov 20, 2009
To scale the image, look at "fitting" and fitOptions.Bob

Likes

Translate

Translate
Guide ,
Nov 20, 2009 Nov 20, 2009

Copy link to clipboard

Copied

Are you also beginning with C# ? Then please reconsider, and follow tried paths with JavaScript - plenty more support around here. You could at least write a draft of your program, and only if it works move it to C#.

To get further on your issue, I'd follow the idea outlined here:

http://msdn.microsoft.com/en-us/library/ms173183(VS.80).aspx

If that does not work, I'm sorry - last time I used C# is years ago, and targeting some pocket PC. No InDesign involved.

Dirk

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
Adobe Employee ,
Nov 20, 2009 Nov 20, 2009

Copy link to clipboard

Copied

Place returns an Array, not a Graphic. Even then the next question is an array of what?

C# uses strong typing. Strong typing can be very difficult to use when scripting InDesign as many of the return values can vary depending on the function call and the content of the document. VBScript is a much better choice.

The pain level of InDesign scripting with C# will be very high.

Bob

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
Adobe Employee ,
Nov 20, 2009 Nov 20, 2009

Copy link to clipboard

Copied

To scale the image, look at "fitting" and fitOptions.

Bob

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 Beginner ,
Nov 23, 2009 Nov 23, 2009

Copy link to clipboard

Copied

Thank you very much for your answers.

First yes, I think doing it in javascript is more easily. (no strong type and more documentation than c# scripting).

But I am coding a web service, and javascript is for client side so I chose the C# option.

I will try to continue with C#. I have still to convert my xml TLF to an indesign text and a datagrid to an indesign table, with all styles and color.

Only the TLF xml afraid me.

Thanks you again for your answer and have a good day

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
LEGEND ,
Aug 05, 2021 Aug 05, 2021

Copy link to clipboard

Copied

But InDesign is also a client side app. It can't run on a server. Unless... you are actually developing for InDesign Server? If you are using InDesign Server, you might look into Server Side JavaScript, which is a thing (though I've never used it, I'm stuck in the dark ages and so I use Perl).

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
LEGEND ,
Aug 05, 2021 Aug 05, 2021

Copy link to clipboard

Copied

LATEST

A little more info, which may or may not be right: you may assume that to use JavaScript implies you are working through a JavaScript engine in a browser. But JavaScript is now widely implemented by many kinds of app, not just browsers. For example, if you use JavaScript in a PDF opened in Acrobat, then the JavaScript engine is part of Acrobat. Similarly, using InDesign automation through JavaScript does not use a browser-hosted JavaScript.  I may be getting on shaky ground here, but I believe the actual JavaScript engine is resident, not in InDesign, but in shared CC component called CEP, while Adobe refer to it as ExtendScript. Anyway, here is a very old (but no doubt still relevant) document on automating InDesign Server with JavaScript aka ExtendScript. http://wwwimages.adobe.com/content/dam/acom/en/devnet/indesign/sdk/cs6/server/ids_scriptingguide.pdf

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 ,
Aug 05, 2021 Aug 05, 2021

Copy link to clipboard

Copied

Try this, 

 

var myGraphics = (InDesign.Graphic) myPicture.Place(pathName, false);

InDesign.Graphics myGraphic = myGraphics[1];

//myGraphic : here is your placed photo

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