Link to home
Start Free TrialLog in
Avatar of CatsSolutions

asked on

Get graphic object from InDesign using COM object in C#

I am looking at automating some functions in InDesign using a WPF app in C# using the COM library. I have managed to open a document, get text frames and update text within the document.

I would like to swap out images within graphic frames but I am having trouble doing so, I can't even get the graphic object.

I am using the below code to try and get graphic objects from the document:

Type type = Type.GetTypeFromProgID("InDesign.Application");
Application app = (Application)Activator.CreateInstance(type, true);

 //Set units to mm
app.ViewPreferences.HorizontalMeasurementUnits = idMeasurementUnits.idMillimeters;
app.ViewPreferences.VerticalMeasurementUnits = idMeasurementUnits.idMillimeters;

//Open Document
app.Open(@"C:\document.indd", true, idOpenOptions.idOpenOriginal);
Document doc = app.ActiveDocument;

Page pg = doc.Pages[1];
Graphics gs = (Graphics)pg.AllGraphics;

Open in new window

but I keep getting this error:

An unhandled exception of type 'System.InvalidCastException' occurred in InDesignTest.exe

Additional information: 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)).

Has anyone used InDesign successfully with C#, any help would be appreciated.
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

What type is pg.AllGraphics?
If you added a COM reference to the project, then I am curious why you are using Type.GetTypeFromProgID?

Type type = Type.GetTypeFromProgID("InDesign.Application");
Application app = (Application)Activator.CreateInstance(type, true);

You should be able to create an instance of the InDesign.Application with early-binding, instead of late-binding.
Avatar of CatsSolutions


I will admit that I copied most of this code from the internet to give me a start as I have only just started working with the InDesign COM and every sample I have seen uses the late-binding.

I have just tried creating a new instance of InDesign.Application and I get a class not registered error, Using late binding is working up until trying to get the graphics.

The pg.Allgraphics returns a COM object.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This has helped a lot, thank you.