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:
but I keep getting this error:
Has anyone used InDesign successfully with C#, any help would be appreciated.
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;
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-F8305706C8 B8}' 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.
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("In Design.App lication") ;
Application app = (Application)Activator.Cre ateInstanc e(type, true);
You should be able to create an instance of the InDesign.Application with early-binding, instead of late-binding.
Type type = Type.GetTypeFromProgID("In
Application app = (Application)Activator.Cre
You should be able to create an instance of the InDesign.Application with early-binding, instead of late-binding.
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This has helped a lot, thank you.