Swapping paragraphs during Data Merge
October 13, 2012 Leave a comment
A recent post on the adobe forums raised an old question which I’d been happy to consign to the too-hard basket, and that question was how to import text as part of a data merge. While text can’t be imported into a merge à la importing pictures into a data merge (i.e. can import image files such as JPG, EPS, PDF, TIFF… but not text files such as DOC, RTF etc) I knew there had to be a workaround.
The workaround came in the form of using a field from the data merge to trigger a GREP style. To demonstrate this, I have created an example in the form of this case study:
An electronics store is preparing a mailing of its statements, along with a with compliments slip which is specifically targeted to each customer. The store has prepared a database with an ID to correspond with the statements, the client’s first name, and a code that corresponds to the campaign, which happens to be:
- Good payers
- Online shoppers
- Recently filled out an in-store survey
- Long time customers
- Dormant customers
- Due for a phone upgrade
Each merge has its own text, so the typical way of handling this would be to perform six separate merges, but because the with compliments slips have to go with statements that are printed in ID order and the client does NOT WANT the ID to appear on the with compliments slip, the merge must be prepared once and in order, so this is not an option.
Instead, I have created an InDesign file with two layers – a base with the static data, and then a layer that I have labelled 1 (to represent the good payers). For now, the text for the first campaign is added, along with the letters |xxxx| at the start of each paragraph, and the text is formatted using a paragraph style called changingpara1 – so far, it contains nothing out of the ordinary… yet. This will make more sense soon.
The other campaigns are literally duplicates of layer “1” but named appropriately (2,3,4,5,6) and contain their appropriate content. For the ease of creation, only the base and layer being made are viewed at any time. Until the data is imported, I’m just using first names.
Using the data merge palette, the data “namesdb.txt” is imported, and the |xxxx| portions are replaced with |<<number>>| but still retain their “|” at the start and end of the field code. Again, for ease of working with the layers, only the base and layer being made are viewed at any time.
As Heston Blumenthal would say… this is where the magic happens. With no text selected, two character styles need to be created:
- Horizontal scale to -1, and
- no fill nor stroke
- Black fill only
Now, viewing only layer “1”, the text box is selected and the paragraph style assigned to it is changed, namely:
- No fill nor stroke, and
- GREP styles
apply Black style,
to text: ^\|1\|.+
(this will find the start of the line and |1| followed by everything in the paragraph)
apply Hide style,
to text: ^\|1\|
(this will find the start of the line and the |1| only so it can be hidden)
But something unexpected has happened… the type has disappeared! This should happen… so far… pushing on, view only layer two, click on the textbox and in the paragraph styles palette, duplicate changingpara1 BUT change its name to changingpara2 and change the GREP styles this way:
apply Black style,
to text: ^\|2\|.+
(this will find the start of the line and |2| followed by everything in the paragraph)
apply Hide style,
to text: ^\|2\|
(this will find the start of the line and the |2| only so it can be hidden)
Repeat this for the remaining layers, so that layer “3” text has the “changingpara3” paragraph style that, in turn, has the GREP styles which show/hide text with the letters |3| at the start of the paragraph, and so on for the other layers.
After doing this, make sure all the layers are visible and turn the “preview” on the Data Merge palette. Suddenly, the text seems to come to life, responding to the values in the “numbers” field.
The picture above represents the records in the data merge and how they change from record to record. Alternatively, watch the video of how a similar campaign was assembled:
As usual, all files associated with this demonstration are available in the merged PDF file which can be downloaded here.