Automated forms using Javascript, XML and InDesign

What better way to create nice looking, efficient forms than by using Indesign. Today, the XML data available can be inserted with a script and creation, and printing and customizing can be controlled using Javascript.

Start with Master Pages. Create a master page for a docket, a packing slip or an invoice. Label it with a short description and you can have one template with all your required forms!

To apply a master using Javascript, use this code when running from the scripts menu in Indesign:

app.activeDocument.pages.item(0).appliedMaster = app.activeDocument.masterSpreads.item(templatename).

In the FullSWITCH or PowerSWITCH environment, the Indesign configuration uses $doc in place of app.activeDocument to refer to the template that SWITCH opens:

$doc.pages.item(0).appliedMaster = $doc.masterSpreads.item(templatename).

Start with this, and you can have one script that creates a completed form with the click of a mouse!

Javascript can apply a template to a page, and then fill in the blanks with XML data – shipping information, ordered items, item numbers, quantities. A single line to import the XML file from the Javascript is as follows:

app.activeDocument.importXML(File(thePathtotheXMLFile)).

$doc. is used again in the FullSWITCH and PowerSWITCH environments.

Importing XML can seem tricky, so for your first time, try manually importing a sample of the XML file you are using. For this to work, the file needs to be the same XML setup for all files being used.

Under the View menu, select Show Structure if it is not on the left of your screen. Under the drop down menu in the structure window, select Import XML and select your XML file.

The structure of the XML file can be dragged from the current window into a textbox on the page. Under View ->Structure, select Show Tagged Frames. When you drag an XML element into a textbox, it will appear highlighted with the colour of the elements tag. Select the data inside the frame, and delete it – the box should still show the tag colour. This means the next time an XML file with that element is imported, the data will go into that box.

Next, set up paragraph styles for your text so you can map the individual parts to the styles you want. In the structure window’s drop down menu, select Map Tags to Styles. You can select the element and specify the paragraph style you want.

Keep in mind, this works for a single element, such as an order date or order number, but if you want a group of items such as a shipping address, they will most likely be grouped into a higher level element.

In the structure window, you can select Show Text Snippets from the drop down window and the data inside the XML file appears next to each element.

You can add a complex element as well. For example, if you have an element named ShipTo, and attached to it is a complete name and address, you can drag the whole ShipTo element into a textbox. To do this, follow these simple steps:

1. Select View->Structure->Show Tag Markers. Each element will have a set of coloured square parentheses around it.
2. Format the text and set it up the way you prefer.
3. Delete the text in the square parentheses, but leave the parentheses in place. Notice that when you do this, the data that was attached to the element in the structure window disappears when the text is deleted, leaving an empty element.

So far, we have been working with single occurring items, but if you have something that changes from job to job, like ordered items, you will want them to generate a line for each item.

Using the method above, drag an item that may contain things like quantity, description, code or price into a textbox. Format the line using a paragraph style with tabs, and add a hard return at the end of the line. Delete the text above it, leaving the coloured parentheses as placeholders.

Indesign will remember previous settings when importing the XML file. Make sure to check Mode: Merge Content and Clone when repeating text elements. When you run your script on the open template file, it will apply the master and import your data.

Related Articles

14 COMMENTS

  1. Dear Andrea,
    I am not a programmer and this is probably where my problem is, but I tried using your Javascript code and can’t seem to get it to see my XML file. I am working on a Mac and I tried to link (thePathtotheXMLFile) and I’ve tried everything from where the XML file is compared to the InDesign file. Too starting the path right from my hard drive. Can you give me some insight into what I might be doing wrong?
    Sincerely,
    Trevor

  2. венера в рыбах у мужчины в 10 доме к чему снится парик во сне рассорка на дождь,
    дождик для фотозоны – купить
    гадание на воде с бумагой что делать с бывшей женой мужа магия

  3. неліктен адам түсінде жылан жеуді армандайды?
    шөбере қолынан май жалау, шөбере туралы
    әндер скачать асылжан абдулла – журек текст, асылжан абдулла – жүрек сипаттау
    мәтіні, сипаттау мәтіні мысалдар 3 сынып

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Fatal error: Uncaught TypeError: Cannot access offset of type string on string in /var/www/easywp-plugin/wp-nc-easywp/vendor/wpbones/wpbones/src/Database/WordPressOption.php:141 Stack trace: #0 /var/www/easywp-plugin/wp-nc-easywp/plugin/Http/Varnish/VarnishCache.php(296): WPNCEasyWP\WPBones\Database\WordPressOption->set() #1 /var/www/wptbox/wp-includes/class-wp-hook.php(308): WPNCEasyWP\Http\Varnish\VarnishCache->doPurge() #2 /var/www/wptbox/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters() #3 /var/www/wptbox/wp-includes/plugin.php(517): WP_Hook->do_action() #4 /var/www/wptbox/wp-includes/load.php(1124): do_action() #5 [internal function]: shutdown_action_hook() #6 {main} thrown in /var/www/easywp-plugin/wp-nc-easywp/vendor/wpbones/wpbones/src/Database/WordPressOption.php on line 141