Skip to content

Instantly share code, notes, and snippets.

@adamzuckerman
Created February 3, 2016 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamzuckerman/766523ec7d0bd7f533e3 to your computer and use it in GitHub Desktop.
Save adamzuckerman/766523ec7d0bd7f533e3 to your computer and use it in GitHub Desktop.
JS to create JS to position PDF fields on a new document after copying
// Get the number of widgets associated with a field
function getWidgetCount(fieldName) {
var field = getField(fieldName);
if (!field) {
return 0;
}
if (field.page.length > 0) {
return field.page.length;
}
return 1;
}
// Build a script that sets all of field's positions on the page
console.println("var f;");
for (var pageNumber = 0; pageNumber < numPages; pageNumber++)
{
for (var fieldNumber = 0; fieldNumber < numFields; fieldNumber ++)
{
var fld = getField(getNthFieldName(fieldNumber));
var widgetCount = getWidgetCount(fld.name);
for (var widgetNumber = 0; widgetNumber < widgetCount; widgetNumber++)
{
var instField = getField(fld.name + "." + widgetNumber);
console.println("f = getField(\"" + instField.name + "\");");
console.println("f.rect = [" + instField.rect + "];");
}
}
}
@adamzuckerman
Copy link
Author

Purpose

The purpose of this script is to create another script to assist with positioning entry fields copied from one PDF document to another.

Usage

  • Select and copy the fields to be copied on your source document.
  • Paste the fields onto your target document.
  • Copy the above script into the Javascript Console (Ctrl-J to toggle the visibility of the window).
  • Execute the script (highlight all of the script to be executed and press Ctrl-M).
  • The above script will create script lower in the Console. Copy the created script.
  • On the target document, open the Javascript Console.
  • Paste the code created above into the Console. Use the same process to execute the code as above. This will move the copied fields to the same location as they were in on the source document.

Background

A widget is an instance of a field on the document. For example, you have a PDF document that contains a field named ContactFirstName that is copied on multiple pages. Each instance/widget has a number associated with it on the Field list (e.g., ContactFirstName#0). It can be referenced in Javascript by using getField() passing in the name of the field (ContactFirstName) appended with a period (.) and the instance number (e.g., var field = getField("ContactFirstName.1"); would retrieve the 2nd instance of the field in the document).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment