Click here to Skip to main content
13,191,400 members (64,872 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

19.1K views
1 bookmarked
Posted 5 Nov 2010

Create an Adobe InDesign Document with c#

, 7 Nov 2010
Rate this:
Please Sign up or sign in to vote.
Sample code to get you started
I tested some libraries that create InDesign Interchange XML-documents, but was not happy with the results.

So if you want to create an InDesign document with C#, you need to
- install Adobe InDesign
- reference "Adobe InDesign CSx Type Library" in your application

Because I didn't find a good documentation, here's some code to get you started as well.

// Create application instance
Type type = Type.GetTypeFromProgID("InDesign.Application");
Application application = (Application)Activator.CreateInstance(type);
 
// Set unit type
application.ViewPreferences.HorizontalMeasurementUnits = idMeasurementUnits.idMillimeters;
application.ViewPreferences.VerticalMeasurementUnits = idMeasurementUnits.idMillimeters;
 
// Create new document
application.Documents.Add(true, application.DocumentPresets.FirstItem());
 
// Get active document and change some settings
Document document = application.ActiveDocument;
document.DocumentPreferences.FacingPages = false;
document.DocumentPreferences.PageWidth = 210;
document.DocumentPreferences.PageHeight = 297;
 
// Get first page (already created) and set margins
Page page = (Page)document.Pages[1];
page.MarginPreferences.Top = 10;
page.MarginPreferences.Bottom = 10;
page.MarginPreferences.Left = 20;
page.MarginPreferences.Right = 10;
 
// Create rectangle and fit an image into it
Rectangle rectangle = page.Rectangles.Add(document.Layers.FirstItem(), idLocationOptions.idUnknown, page);
rectangle.GeometricBounds = new[] { 20, 30, 120, 130 };
rectangle.Place(@"c:\temp\sample.png", false);
rectangle.Fit(idFitOptions.idContentToFrame);
 
// Create second page and set margins
page = document.Pages.Add(idLocationOptions.idUnknown, document);
page.MarginPreferences.Top = 10;
page.MarginPreferences.Bottom = 10;
page.MarginPreferences.Left = 20;
page.MarginPreferences.Right = 10;
 
// Create a text frame and add some text
TextFrame textFrame = page.TextFrames.Add(document.Layers.FirstItem(), idLocationOptions.idUnknown, page);
textFrame.GeometricBounds = new[] { 20, 30, 120, 130 };
textFrame.Contents = "line1\rline2\rline3";
IEnumerator paragraphs = textFrame.Paragraphs.GetEnumerator();
for (int j = 0; j < textFrame.Paragraphs.Count; j++)
{
	paragraphs.MoveNext();
	Paragraph paragraph = ((Paragraph)paragraphs.Current);
	paragraph.Justification = idJustification.idCenterAlign;
	paragraph.PointSize = 15 - j;
	paragraph.FontStyle = "Regular";
	paragraph.AppliedFont = "Verdana";
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

#andy
Switzerland Switzerland
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralWould be very useful to make this into an article. Also what... Pin
Hal201230-Nov-10 18:04
memberHal201230-Nov-10 18:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171017.1 | Last Updated 7 Nov 2010
Article Copyright 2010 by #andy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid