Converting Acrobat checkboxes to radio buttons

Submitted by davidc on Sun, 19/06/2016 - 12:29

Acrobat's automatic form generation is atrocious. It inevitably gets almost everything wrong, meaning you have to recreate it by hand. It boggles the mind that it's had the ability to create forms for 20 years now, and still has no way of automatically creating the form from a Word document without a buggy and and half-hearted attempt at auto-detection. You'd think at the very least it would be able to turn Word form elements into PDF form elements, but no.

Even Echosign is slightly better, allowing you to put {{bracketed}} code called text tags into your document that it will automatically turn into signature fields. It has a rather awkward syntax, and the code is not separated from the layout, resulting in terrible hacks to reduce the visual impact on the document, but at least you can automate it to an extent.

Instead, for a PDF form you are left with automatic detection and then having to clean up its many mistakes - and repeating this process every time you change the source document.

Acrobat will detect circular objects (e.g. a circle from the Wingdings font) and turn them into radio buttons. One thing that particularly infuriated me is that it will only make them radio buttons if you have laid out your form in one exact particular way, and using whitespace text rather than tables for alignment. Otherwise it'll turn them into circular-shaped checkboxes, which isn't at all what you want. Forcing people to lay out their documents in a prescribed way is also a ridiculous design choice.

Of course, there's also no way to turn a checkbox into a radio button inside Acrobat, so I had to come up with the following piece of code to do so (based on a piece of code that does it the other way around). Hopefully this will be useful to others.

It goes through all the form fields in the document looking for checkboxes, deletes them and then creates a radio button in its place. If you want to keep some checkboxes, you'll need to hack this code to exclude them. It's not perfect - the generated radio buttons may not be the exact same size as your other radio buttons, and you will definitely need to amend their names and submit values afterwards - but it saves having to recreate them all from scratch.

Here's the ridiculous method you have to follow to actually execute this code:

  1. From the tools "menu", choose Action Wizard at the bottom.
  2. On the toolbar, choose "New Custom Command".
  3. On the left hand side, choose "Execute Javascript".
  4. Change the label and tooltip to, for example, "Convert checkboxes to radio buttons".
  5. Click "Command Options" and paste in the code below.
  6. Click OK and OK to save. This will save the custom command between Acrobat sessions, at least.
  7. You'll now see "Convert checkboxes to radio buttons" on the right hand toolbar when you are in the Action Wizard mode.
  8. If it doesn't work, go back to the Tools screen, choose JavaScript, and then choose Debugger. Run the command again and you should see the problem in the JavaScript console.

And here's the code:

function processCB(f) {
	console.println("Doing " + f.name);
 
	var theName = f.name;
	var theStyle = f.style;
	var theDisplay = f.display;
	var thePage = f.page;
	var theRect = f.rect;
 
	this.removeField(f.name);
 
	var newName = theName.substring(theName.indexOf("_")+1);
 
	var fn = this.addField({
			cName: newName,
			cFieldType: "radiobutton",
			nPageNum: thePage,
			oCoords: theRect
	});
 
	fn.exportValues = [ theName ];
	fn.style = theStyle;
	fn.display = theDisplay
}
 
 
// iterate over all form fields and look for checkboxes
for (var i = 0; i < this.numFields; i++) {
	var f = this.getField(this.getNthFieldName(i));
 
	if (f.type == "checkbox") {
		processCB(f);
          --i;
	}
}