SPLITTING MERGED DATA PDFS

While on the subject of using Excel as a data source for InDesign, there is an interesting automation that involves Acrobat. This has to do with Acrobat’s unique feature that allows a single PDF to be split into individual files based on document parameters. One, parameter in particular is to use bookmarks. This is where InDesign comes in: creating bookmarks to identify split locations.

Think of a document where you may have many similar sections created using Data Merge and you wish to create a separate PDF for each. Assigning a bookmark to the first data element for each section (data set) can be used by Acrobat to split the PDF.

CREATING BOOKMARKS IN INDESIGN

A popular method for creating Bookmarks in InDesign manually is to use Table of Contents. The key to the process is to style the placeholder for each data element using a paragraph style. In the data merge process each placeholder is mapped to an Excel data item. It is the paragraph style that identifies the page element to be bookmarked. For our example, the Name style was used.

TABLE OF CONTENTS

One advantage of using Table of Contents is that the resulting list of bookmarks can be veriied prior to exporting the document to PDF. Identify the paragraph style for the top level data item and make sure Create PDF Bookmarks is checked when the table of contents is created.

…Creating bookmarks in Table of Contents

The resulting Table of Contents can be placed on the pasteboard.

…Verify Table of Contents for bookmarks

The merged document with its Table of Contents thus created is exported to PDF, again making sure that Bookmarks is checked.

…Settings for Export to PDF

From there, the Split function in Acrobat is used to split the document into separate files based on the bookmarks.

…Steps to Split PDF

SCRIPTED SOLUTIONS

FOR THOSE WHO WRITE SCRIPTS TO AUTOMATE ADOBE INDESIGN, THERE ARE OTHER OPTIONS. THE DESIRED SOLUTION MAY USE ACROBAT TO SPLIT THE DOCUMENT WITH A SCRIPT TO CREATE THE BOOKMARKS FOR THE MERGED DATA DOCUMENT. LET’S EXPLORE THIS OPTION.

Bookmark Script

The user will need to identify the paragraph style for bookmarking. With this the script can use Find Text to identify the top level data item. First, verify a document is open in InDesign. The script will use a handler, getDocRef, to identify the document and make sure there are no dialogs open.

set docRef to getDocRef
(*Returns reference to document; errors if no document open or modal state is true*)
on getDocRef()
   tell application "Adobe InDesign CC 2019"
      if modal state then
	  error "Please close dialogs before running script"
      end if
      if (count of documents) = 0 then
	  error "Requires an open document."
      end if
      set docRef to document 1
   end tell
   return docRef
end getDocRef

Next the script will get a list of paragraph styles in the document and have the user select the one to use for the bookmarks. This will require two handlers: getParagraphStyle that calls getChoice.

set promptStr to "Select paragraph style for bookmarks"
set docRef to getDocRef()
set styleRef to getParagraphStyle (docRef, promptStr)
(*Gets list of paragraph styles and calls getChoice to have user choose from the list*)
on getParagraphStyle(docRef, promptStr)
   tell application "Adobe InDesign CC 2019"
      tell docRef
	 set styleNames to name of paragraph styles
	 set styleName to my getChoice(styleNames, promptStr)
	 set styleRef to paragraph style styleName
      end tell
      return styleRef
   end tell
end getParagraphStyle
(*Returns user choice from list; errors if no choice made*)
on getChoice(parastyles, promptStr)
   set styleChoice to choose from list parastyles with prompt promptStr
   if styleChoice is false then
      error "Requires a selection"
   end if
   set styleName to item 1 of styleChoice
   return styleName
end getChoice 

The real workhorse for the script is a handler we have named createBookmarks. It requires the reference to the document and the reference to the paragraph style selected.

set testList to createBookmarks(docRef, styleRef)
(*Creates bookmarks by setting hyperlink page destinations using find to get list of paragraphs for bookmarks*)
on createBookmarks(docRef, styleRef)
   set testList to {}
   tell application "Adobe InDesign CC 2019"
      set find text preferences to nothing
      set change text preferences to nothing
      set applied paragraph style of find text preferences to styleRef
      tell docRef
	 delete bookmarks
	 set foundSet to find text
	 if foundSet is {} then
	     error "No text items found"
	 end if
	 repeat with eachItem in foundSet --length of itemList
	    set frameRef to item 1 of parent text frames of eachItem
	    set pageRef to parent page of frameRef
	    set destContent to contents of contents of eachItem
	    if not (exists hyperlink page destination destContent) then
		set markDest to make hyperlink page destination with properties {destination page:pageRef, name:destContent}
	    else
		set markDest to hyperlink page destination destContent
	    end if
	    if not (exists bookmark destContent) then
		make bookmark with properties {name:destContent, destination:markDest}
	    end if
	    set end of testList to destContent
	 end repeat
      end tell
      set find text preferences to nothing
      set change text preferences to nothing
   end tell
   return testList
end createBookmarks

We have the handler return a list of the paragraph contents just for verification. You could just alert the user with a count of the list items. With this, the top of the script can read as follows:

set promptStr to "Select paragraph style for bookmarks"
try
   set docRef to getDocRef()
   set styleRef to getParagraphStyle(docRef, promptStr)
   set testList to createBookmarks(docRef, styleRef)
   activate
      display alert ("Bookmarks created: " & length of testList) giving up after 5
on error errStr
   activate
   display alert "Error: " & errStr
end try

You could have the script go on to export the file to PDF. To make the above script complete you may also want to add a handler to set the Find Text options just to make sure these are set as needed for your document.

SCRIPT TO CREATE INDIVIDUAL PDFS

Using bookmarks may be the way to go especially if each data merge data set requires more than one page. However, if you do have a simple document where each page contains the entire data merge set you could just Export to PDF and use Create Separate PDF Files. Should you want the files to be named the same as the contents for the top data item (as was done manually above), a script similar to the following can be used. Our sample script requires that the text frame holding the data items be named “data”.

set promptstr to "Select paragraph style for data file name reference"
set testList to {}
tell application "Adobe InDesign CC 2019"
   set docRef to document 1
   set nameList to name of paragraph styles of docRef
   set styleChoice to my getChoice(nameList, promptstr)
   tell docRef
      set docPath to file path as string
      set styleRef to paragraph style styleChoice
   end tell
   repeat with i from 1 to number of pages of docRef
      set pageRef to page i of docRef
      set pageName to name of pageRef
      set frameRef to text frame "data" of pageRef
      set paraList to (paragraphs of frameRef where applied paragraph style is styleRef)
      set textContent to (text 1 thru -2 of item 1 of paraList)
      set pathStr to docPath & textContent & ".pdf"
      set fileName to textContent & ".pdf"
      set acrobat compatibility of PDF export preferences to acrobat 5
      set page range of PDF export preferences to pageName
      set include structure of PDF export preferences to false
      set pdf display title of PDF export preferences to display file name
	 export docRef format PDF type to pathStr
   end repeat
end tell
(*Returns user choice from list; errors if no choice made*)
on getChoice(parastyles, promptstr)
   set styleChoice to choose from list parastyles with prompt promptstr
   if styleChoice is false then
	error "Requires a selection"
   end if
   set styleName to item 1 of styleChoice
   return styleName
end getChoice

Hint: For naming the resulting files, you might consider using a non-printing data item in the merged document to identify the name of the PDfs created. Just place this data item on the pasteboard with part of its text frame touching the page.

ONWARD AND UPWARD

Make one of the scripts above your own by adding Find Text options and/or other functionality as needed. Plan your data merge documents to include file naming (see Hint above).

Disclaimer:
Scripts provided are for demonstration and educational purposes. No representation is made as to their accuracy or completeness. Readers are advised to use the code at their own risk.