Skip to main content
edward_fedeli
Participant
April 14, 2018
Question

How to automate InDesign data merge with C#

  • April 14, 2018
  • 3 replies
  • 5955 views

I am trying to implement a solution where we can automate the data merge process offered by InDesign (see here: Merge data to create form letters, envelopes, or mailing labels in Adobe InDesign ).  I am currently using the COM interop classes from the SDK to achieve this, but I having trouble actually instantiating the classes required to actually execute the data merge.

I have an existing CSV file I will be using to conduct the import, and I have an existing InDesign file that can accept the mapping.

Here's what I have so far in terms of process:

  • Create an instance of the InDesign application (hiding the window)
  • Open the desired InDesign document
  • Provide the path to the CSV import file (not exactly sure where to do this)
  • Actually execute the data merge, and output the resulting PDF(s) (I think this is via either the DataMerge.exporFile or DataMerge.mergeRecords method, but not sure how to access those methods)
  • Retrieve the resulting PDF(s)

Basically, what is not clear in their SDK is the following:

  • What method do I use to supply the CSV import file for the data merge?
  • How do I actually instantiate the DataMerge class, once I have an existing Document object?
  • What's the difference between the DataMerge.exportFile or DataMerge.mergeRecords methods?

Also, we are planning to use similar logic on the InDesign server.  Will the same process from above be valid on InDesign server?

Thanks,

This topic has been closed for replies.

3 replies

Srishti Bali
Legend
May 29, 2018

moved to InDesign Scripting

Colin Flashman
Community Expert
Community Expert
April 15, 2018

I agree with Rob, this is probably better answered in the InDesign Scripting forum, if one of the Mods could move this topic, that'd be grand.

In the meantime, I have written dozens of data merge scripts and make many of them freely available via my website www.colecandoo.com. Data Merge is easily scripted through the flavour of Javascript that InDesign uses, ESTK. Perhaps one of my scripts may already fulfil the task you are after.

FWIW, here is a very basic implementation of a javascript that will perform a data merge on an open document:

#target "InDesign"

       var myDoc = app.activeDocument;

        var myExport = File(app.activeDocument.filePath + "/" + app.activeDocument.name.split(".indd")[0] + ".pdf");

with(myDoc.dataMergeProperties.dataMergePreferences) 

            { 

            recordSelection = RecordSelection.ALL_RECORDS; 

            } 

        app.dataMergeOptions.removeBlankLines = true;

                myDoc.dataMergeProperties.exportFile(myExport, "[High Quality Print]", );

If the answer wasn't in my post, perhaps it might be on my blog at colecandoo!
rob day
Community Expert
Community Expert
April 14, 2018

You should ask in the SDK forum.

InDesign SDK

But are you developing a plugin? If you just need automation Javascript might be easier.

InDesign Scripting

Here's the JS API

InDesign ExtendScript API (10.0)