Blog Home  Home Feed your aggregator (RSS 2.0)  
Using Adobe InDesign with C# - Manuel Abadia's ASP.NET stuff
# Tuesday, April 18, 2006

When I was making the layout for the book last month I had to do a lot of repetitive tasks. I was on a hurry to finish the book so I didn’t have a chance to look at InDesign scripting capabilities. However, a few days after helping my friend I told him that a process like making the layout of a book could be automated a lot with scripting in order to avoid repetitive tasks.

When I started reading about InDesign scripting I thought that I had to do it using JavaScript. I didn’t have to learn another language so that was good. However, working with JavaScript is always slower than working with other languages (maybe because the lack of good tools?).

However I was surprised when I read that all InDesign features were exposed as a COM object. I could leverage the power of Visual Studio 2005 to do everything without having to do it in JavaScript!

To use InDesign from C# I had to import the InDesign Type Library and Visual Studio generated a COM wrapper for it. That was it! Of course, not all was perfect or straightforward. There were some things to consider:

• All InDesign collections start at index 1 instead of 0
• A lot of methods return object instead of a strongly typed object so a lot of casting is needed.
• There are times when you don’t know exactly what kind of object you get when calling a method and inspecting it in the debugger doesn’t help. Fortunately this guy used the compiler services to load all InDesign types and try to convert the object to all types until a match was found. This is very helpful when working with InDesign.
• There were times that setting the content of a paragraph or some text didn’t work as expected but setting it indirectly (calling a find/replace method for example) worked fine.
• The documentation is minimal. Sometimes I wonder why the MSDN documentation is obscure and reduced in some classes but you have to see InDesign scripting reference:

If you find a description with more than four words it’s your lucky day. It seems that the documentation is completely generated by a tool similar to GhostDoc and then a few properties and methods have been comented.

And the description of the classes is amazing:
Story: A story.
TrapPreset: A trapping preset.


If someone wants to start using InDesign with C# an example could be useful:

private int RemoveCRFromFootnotes()


            // gets the InDesign App

            Type inDesignAppType = Type.GetTypeFromProgID("InDesign.Application.CS2");

            InDesign.Application myInDesign = (InDesign.Application)Activator.CreateInstance(inDesignAppType);


            string footNotesFixed = "";


            InDesign.Document myDocument = null;


            // gets the current active document   

            try {

                myDocument = myInDesign.ActiveDocument;

            } catch {

                throw new Exception("You need to have an active document to run this script");



            // iterates through all the stories in the current document

            for (int i = 1; i <= myDocument.Stories.Count; i++) {

                InDesign.Story currentStory = (InDesign.Story)myDocument.Stories[i];


                // iterates trough all the FootNotes in the current story

                for (int fn = 1; fn < currentStory.Footnotes.Count; fn++) {

                    InDesign.Footnote currentFootNote = (InDesign.Footnote)currentStory.Footnotes[fn];


                    // goes to the last parragraph in the FootNote

                    if (currentFootNote.Paragraphs.Count > 0) {

                        try {

                            InDesign.Paragraph currentParagraph = (InDesign.Paragraph)currentFootNote.Paragraphs[currentFootNote.Paragraphs.Count];


                            // if ends with a carriage return, remove it

                            string contents = currentParagraph.Contents as string;

                            if (contents != null) {

                                if (contents.EndsWith("\r")) {

                                    // assigning content deletes the footnote number but search and replace works

                                    currentParagraph.Search(currentParagraph.Contents, false, true, contents.Substring(0, contents.Length - 1));


                                    footNotesFixed += " " + currentFootNote.Index.ToString();




                        } catch {






            return footNotesFixed;



This sample removes the carriage return at the end of the footnotes that word usually lefts.

Tuesday, April 18, 2006 8:57:19 AM (Romance Daylight Time, UTC+02:00)  #    Comments [5]   Microsoft .NET Framework  | 
Copyright © 2020 Manuel Abadia. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.