We help IT Professionals succeed at work.
Get Started

Get graphic object from InDesign using COM object in C#

Last Modified: 2014-05-02
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.
Watch Question
Most Valuable Expert 2012
Top Expert 2008
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE