thst
 Total Messages 1
|
| Subject: | duplicating forms with specific naming conventions / renaming fields |
Hi,
we recently solved an issue that required us to duplicate a page with specific form names.
This is not possible with the template approach as you with templates you need to live with the formnames as the copy function creates them for you.
We needed to copy form fields with a nameing scheme that used a running index in the fieldnames.
For whom it may concern, I am posting our script for you to solve a similar issue.
Have fun with the code or move to Trash :-)
Regards,
Thomas
Attachment did not work correctly, here is the script:
/*
* This code is (C) 2011 Thomas Strauß/SRS-Management GmbH
* You may use this code for whatever you find it suitable as long as this copyright
* message stays intact.
*
* This code is delivered As-Is, without warranty and without any promised
* functionality and withot any liability.
* If it does not work for you, or breaks your stuff - don't blame me.
*
* The code will walk through all form fields and find those on the sourcePageStart.
* It will copy all fieldnames to an array for later use.
* It will extract the sourcepage to another file
* It will start renaming the first fields.
* it will import the page from the extracted file and rename the fields.
*
* To rename a field, the new name is generated from the existing name,
* The field is created with the new name at the same rect as the existing field.
* All properties of the field as listed in "props" are copyied from existing to new.
* If any field property is not available on the source field, acrobat throws an exception
* that is silently ignored.s
*/
function PumpPageCopy() {
var props = [ "alignment", "bgColor", "borderColor", "borderStyle",
"borderWidth", "buttonAlignX", "buttonAlignY", "buttonFitBounds",
"buttonPosition", "buttonScaleHow", "buttonScaleWhen",
"calcOrderIndex", "charLimit", "comb", "commitOnSelChange",
"currentValueIndices", "defaultStyle", "defaultValue", "display",
"doNotScroll", "doNotSpellCheck", "editable", "exportValues",
"fgColor", "fileSelect", "fillColor", "hidden", "highlight",
"lineWidth", "multiline", "multipleSelection", "password",
"radiosInUnison", "readonly", "required", "richText", "richValue",
"rotation", "strokeColor", "style", "submitName", "textColor",
"textFont", "textSize", "userName", "value" ];
app."start of pumppage");
console.clear();
var copyCount = 5;
var sourcePageStart = 2;
var sourcePageEnd = sourcePageStart;
var insertPageIdx = sourcePageStart-1;
var re = /\.pdf$/i;
var tmpFileBase = this.documentFileName.replace(re, "");
var tmpFile = "/D/" + tmpFileBase + "_pages_" + sourcePageStart + "-" + sourcePageEnd + ".pdf";
try {
var numF = this.numFields;
var count = 0;
var fields = new Array();
var aField;
var pageIdx;
for(i = 0; i<numF; i++) {
aField = this.getField(this.getNthFieldName(i));
pageIdx = aField.page + 1;
if( pageIdx == (sourcePageStart) ) {
//console.println("found field to rename: " + aField.name);
fields[count] = aField.name; // we keep only the name!
count++;
}
}
app."found " + count + " fields, now extracting pages to " + tmpFile);
console.clear();
if( sourcePageStart == sourcePageEnd ) {
this.extractPages( {nStart: sourcePageStart-1, cPath: tmpFile});
}
else {
this.extractPages( sourcePageStart-1, sourcePageEnd-1, tmpFile);
}
var offset = 0;
for(page = 0; page <=copyCount; page++) {
console.println("pumping page " + page);
var newname;
var newField;
for(i = 0; ia<count; i++) {
aField = this.getField(fields[i]);
// start of creating the new name
newname = fields[i];
var matches = /Position([0-9]+)(.*)/.exec(newname);
if( matches ) {
index = Number( RegExp.$1 );
newname = "Position" + (offset + index) + RegExp.$2;
}
else {
newname = "p" + page + "." + newname;
}
// end of creating the new name
newField = this.addField(newname, aField.type, insertPageIdx, aField.rect);
for(var name in props)
{
try
{
newField[props[name]] = aField[props[name]];
}
catch(Exception) { }
}
this.removeField(aField.name);
}
offset+=20;
console.println(page + " done. Document has now " + this.numPages + " number of pages");
this.saveAs({cPath: "/D/" + tmpFileBase + "-iter_" + page + ".pdf"});
if( page < copyCount) {
this.insertPages({nPage: insertPageIdx, cPath: tmpFile});
insertPageIdx += sourcePageEnd - sourcePageStart + 1;
}
}
app."finished, please remove temporary file " + tmpFile);
}
catch(e) {
console.println("Exception:" + e);
}
}
Posted: 27 May 2011 03:34 PM Originally Posted: 27 May 2011 03:10 PM |
|