These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

creating a unique ID in a LiveCycle PDF using JavaScript

thfeldman67
Registered: Apr 25 2008
Posts: 23
Answered

Does anyone have a methodology for creating a unique identifier within a PDF (created in LiveCycle) using JavaScript - one that has virtually no risk of being duplicated even when using the PDF on another workstation? If so, please let me know - thanks!

(I've seen some examples for JavaScript in general - but I need something that will work from within a PDF and not just a web broswer.)

Trevor Feldman
BD

My Product Information:
LiveCycle Designer, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
LiveCycle Designer's JavaScript object supports the JavaScript "Date" object, so one can obtain the milliseconds since January 1, 1970 UTC and LiveCycle Designer's "Time()" function that returns the number of milliseconds since the epoch date for LiveCycle Designer.

George Kaiser

thfeldman67
Registered: Apr 25 2008
Posts: 23
FYI, here is a version of code I found that creates a UUID - setup to run as a LiveCycle Scripting Object - this does what I wanted (just call the function getUUID):


/*
uuid.js - Version 0.1
JavaScript Class to create a UUID like identifier

Copyright (C) 2006, Erik Giberti (AF-Design), All rights reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA

The latest version of this file can be downloaded from
http://www.af-design.com/resources/javascript_uuid.php

HISTORY:
6/5/06 - Initial Release
5/2/08 - Modified for use in a LiveCycle Scripting Object.
*/

function getUUID(){
// JavaScript Version of UUID implementation.
//
// Copyright 2006 Erik Giberti, all rights reserved.
//
// Loose interpretation of the specification DCE 1.1: Remote Procedure Call
// described at http://www.opengroup.org/onlinepubs/009629399/apdxa.htm#tagtcjh_37
// since JavaScript doesn't allow access to internal systems, the last 48 bits
// of the node section is made up using a series of random numbers (6 octets long).
//
var dg = UUID.timeInMs(new Date(1582, 10, 15, 0, 0, 0, 0));
var dc = UUID.timeInMs(new Date());
var t = dc - dg;
var h = '-';
var tl = UUID.getIntegerBits(t,0,31);
var tm = UUID.getIntegerBits(t,32,47);
var thv = UUID.getIntegerBits(t,48,59) + '1'; // version 1, security version is 2
var csar = UUID.getIntegerBits(UUID.randrange(0,4095),0,7);
var csl = UUID.getIntegerBits(UUID.randrange(0,4095),0,7);

// since detection of anything about the machine/browser is far to buggy,
// include some more random numbers here
// if nic or at least an IP can be obtained reliably, that should be put in
// here instead.
var n = UUID.getIntegerBits(UUID.randrange(0,8191),0,7) +
UUID.getIntegerBits(UUID.randrange(0,8191),8,15) +
UUID.getIntegerBits(UUID.randrange(0,8191),0,7) +
UUID.getIntegerBits(UUID.randrange(0,8191),8,15) +
UUID.getIntegerBits(UUID.randrange(0,8191),0,15); // this last number is two octets long
return tl + h + tm + h + thv + h + csar + csl + h + n;
}

//
// GENERAL METHODS (Not instance specific)
//

// Pull out only certain bits from a very large integer, used to get the time
// code information for the first part of a UUID. Will return zero's if there
// aren't enough bits to shift where it needs to.
function getIntegerBits(val,start,end){
var base16 = UUID.returnBase(val,16);
var quadArray = new Array();
var quadString = '';
var i = 0;
for(i=0;i= base) var output = UUID.returnBase(MSD,base) + convert[LSD];
else var output = convert[MSD] + convert[LSD];
}
return output;
}

// This is approximate but should get the job done for general use.
// It gets an approximation of the provided date in milliseconds. WARNING:
// some implementations of JavaScript will choke with these large numbers
// and so the absolute value is used to avoid issues where the implementation
// begin's at the negative value.
function timeInMs(d){
var ms_per_second = 100; // constant
var ms_per_minute = 6000; // ms_per second * 60;
var ms_per_hour = 360000; // ms_per_minute * 60;
var ms_per_day = 8640000; // ms_per_hour * 24;
var ms_per_month = 207360000; // ms_per_day * 30;
var ms_per_year = 75686400000; // ms_per_day * 365;
return Math.abs((d.getUTCFullYear() * ms_per_year) + (d.getUTCMonth() * ms_per_month) + (d.getUTCDate() * ms_per_day) + (d.getUTCHours() * ms_per_hour) + (d.getUTCMinutes() * ms_per_minute) + (d.getUTCSeconds() * ms_per_second) + d.getUTCMilliseconds());
}

// pick a random number within a range of numbers
// int c randrange(int a, int b); where a <= c <= b
function randrange(min,max){
var num = Math.round(Math.random() * max);
if(num < min){
num = min;
} else if (num > max) {
num = max;
}
return num;
}

// end of UUID class file
eopi
Registered: Dec 14 2009
Posts: 12
gkaiseril wrote:
LiveCycle Designer's JavaScript object supports the JavaScript "Date" object, so one can obtain the milliseconds since January 1, 1970 UTC and LiveCycle Designer's "Time()" function that returns the number of milliseconds since the epoch date for LiveCycle Designer.
I like this idea; and being a complete noob can someone briefly explain how to implement. Do I create a Date field and when the person taking the survey selects the accurate date then that will serve as the Unique ID?

Or, do I create a Time data field for Current time (i.e. Milliseconds since 1970) or something like SysTime; SysDate (i.e. System date)?

Any help will be greatly appreciated. I'll keep plugging away in case I stumble upon the answer.

Thx..
EOPI
eopi
Registered: Dec 14 2009
Posts: 12
thfeldman67 wrote:
Does anyone have a methodology for creating a unique identifier within a PDF (created in LiveCycle) using JavaScript - one that has virtually no risk of being duplicated even when using the PDF on another workstation? If so, please let me know - thanks!(I've seen some examples for JavaScript in general - but I need something that will work from within a PDF and not just a web broswer.)

Trevor Feldman
BD
http://www.acrobatusers.com/forums/aucbb/viewtopic.php?pid=60135#p60135

For completeness, here is my post about another post that gives Paul Guerette's lecture on Email, Lock file, Attachement and [b]Unique Ids[/b].

It works.

The number in the [b]Text field[/b] changes when I open and re-open.