Avatar of CatsSolutions
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.
C#Page Layout Software

Avatar of undefined
Last Comment
CatsSolutions

8/22/2022 - Mon
Kyle Abrahams

What type is pg.AllGraphics?
Bob Learned

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.
CatsSolutions

ASKER
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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Bob Learned

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
CatsSolutions

ASKER
This has helped a lot, thank you.