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  | 
Tuesday, April 18, 2006 1:12:45 PM (Romance Daylight Time, UTC+02:00)
Muy interesante. Realmente por esto es importante .NET. Todo esto se podría haber conseguido con otras tecnologías mucho antes, pero parece que los diseñadores de programas en el mundo no-windows han estado bastante cegarrutos en este respecto...

Saludos!
diego.
Tuesday, January 23, 2007 7:32:06 AM (Romance Standard Time, UTC+01:00)
do u have further examples??? on this subject ??? or where can u get more resources for this very interesting stuff.

Philip
Monday, June 18, 2007 9:06:48 AM (Romance Daylight Time, UTC+02:00)
Hi,

Did you have any problems on Image handling for Indesign? Can you post some of your code if you've done some Image manipulation? You're post will be greatly appreciated. Thanks
ralph
Monday, June 18, 2007 10:32:48 AM (Romance Daylight Time, UTC+02:00)
Ralph,

Sorry, but I haven't done anything with images with InDesign, only text stuff.
Friday, September 19, 2008 11:48:58 AM (Romance Daylight Time, UTC+02:00)
Hi,

can you give me a hint how to import the cs3 library so that the needed com wrapper is generated? What else do I need? The CS3 SDK? Which files to import?
Are there further resources on that topic out there?
Thanks a lot!
Alex
alex
All comments require the approval of the site owner before being displayed.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview
Copyright © 2020 Manuel Abadia. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.